npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

css-unit-manager

v1.0.2

Published

A lightweight JavaScript utility for CSS unit conversions.

Downloads

14

Readme

CSSUnitManager

A TypeScript utility for converting between CSS units dynamically.

Installation

npm install css-unit-manager

Usage

import unit, { CSSUnitManager } from "css-unit-manager";

// Create a new instance
const unitManager = new CSSUnitManager(16, "px");

// Convert to another unit
const remValue = unitManager.toUnit("rem").toString(); // "1rem"

// Add a custom unit
CSSUnitManager.addUnit("myUnit", {
  px: 2, // 1 myUnit = 2px
  em: 0.125, // 1 myUnit = 0.125em
});

// Parse from string
const parsedUnit = unit("20px");

// Perform arithmetic operations
const result = unitManager.add(10).multiply(2).toString(); // "52px"

// Reset to initial value
unitManager.reset();

// Convert to a fixed number of decimals
unitManager.toFixed(2);

// Clamp value between a range
unitManager.clamp(10, 50);

// Output as string
console.log(unitManager.toString()); // "16px"

API

Constructor

new CSSUnitManager(value: number, unit: string);
  • value: The numeric value.
  • unit: The CSS unit (e.g., px, em, rem, vw, vh, etc.).

Static Methods

  • CSSUnitManager.isValidUnit(unit: string): boolean: Check if the unit is valid.
  • CSSUnitManager.addUnit(unit: string, conversionRates: { [key: string]: number | (() => number) }): void: Add a custom unit with conversion rates.
  • CSSUnitManager.fromString(cssString: string): CSSUnitManager: Parse a CSS string (e.g., "20px") into an instance of CSSUnitManager.

Instance Methods

  • .toUnit(targetUnit: string): CSSUnitManager: Convert the current value to another unit.
  • .toFixed(digits: number): void: Round the value to a fixed number of decimal places.
  • .add(value: number, unit?: string): this: Add another value (optionally with a different unit).
  • .subtract(value: number, unit?: string): this: Subtract another value (optionally with a different unit).
  • .multiply(factor: number): this: Multiply the value by a factor.
  • .divide(factor: number): this: Divide the value by a factor.
  • .clamp(min: number, max: number): this: Clamp the value between min and max.
  • .min(value: number, unit?: string): this: Set the value to the minimum of the current and the provided value.
  • .max(value: number, unit?: string): this: Set the value to the maximum of the current and the provided value.
  • .normalize(min?: number, max?: number): this: Normalize the value between min and max (default: 0 to 100).
  • .toAuto(): string: Set the value to "auto".
  • .reset(): this: Reset the value to the initial one.
  • .toString(): string: Return the value as a string with the unit.
  • .toNumber(): number: Return the value as a number.
  • .isEqual(other: CSSUnitManager): boolean: Check if two instances are equal (after unit conversion).