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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@quantities/core

v2.0.0

Published

TypeScript library for quantity calculation and unit conversion

Readme

@quantities/core

TypeScript library for quantity calculation and unit conversion.

Install

npm install @quantities/core
bun add @quantities/core

Creating quantities

The Quantity object can be called directly or created as a class instance:

new Quantity("1 m");
Quantity("1 m");

Quantity accepts a string, a number with units, or another quantity:

import Quantity from "@quantities/core";

const height = Quantity("6 ft 4 in"); // parsed string
const area = Quantity(1, "sqft"); // value + units
const alsoHeight = Quantity(height); // another quantity

For hot loops, the QuantityClass named export is faster because it skips the proxy overhead. Use it if raw performance matters to you:

import { QuantityClass } from "@quantities/core";

const quantity = new QuantityClass("1 m");

Converting

Quantity("2500 m/h").to("ft/s"); // → Quantity("2.2783610382035575 ft/s")
Quantity("100 cm").toBase(); // → Quantity("1 m")
Quantity("96.5 cm").toPrecision("5 cm"); // → Quantity("95 cm")

For repeated conversions between the same units, build a reusable swift converter (much faster than .to() per call):

const toFahrenheit = Quantity.swiftConverter("tempC", "tempF");

toFahrenheit(20); // → 68
toFahrenheit([0, 100]); // → [32, 212]

Output

Quantity(1, "sqft").to("m2").format(); // → "0.09290304 m2"
Quantity("6 ft 4 in").to("cm").toString(); // → "193.04 cm"
Quantity("2 m").scalar; // → 2 (numeric value, in current units)
Quantity(2).toFloat(); // → 2 (unitless quantities only)

Pass a custom formatter to format(), or a precision/units qualifier to toString():

Quantity("1.234 m").toString("cm", 1); // → "123.4 cm"
Quantity("2 m").format((scalar, units) => `${scalar.toFixed(2)} ${units}`);

Arithmetic

Quantity("2.5 m").add("3 cm"); // → Quantity("2.53 m")
Quantity("2.5 m").subtract("3 cm"); // → Quantity("2.47 m")
Quantity("2 m").multiply(3); // → Quantity("6 m")
Quantity("6 m").divide("2 s"); // → Quantity("3 m/s")
Quantity("2 m").pow(2); // → Quantity("4 m2")
Quantity("9 cm^2").root(2); // → Quantity("3 cm")
Quantity("8 m3").cbrt(); // → Quantity("2 m")
Quantity("1 m/s").inverse(); // → Quantity("1 s/m")
Quantity("-3 m").abs(); // → Quantity("3 m")
Quantity("-3 m").sign(); // → -1

Most have short aliases: sub, mul, div, pow/exponent, invert, sqrt/cbrt.

Comparison

Quantity("100 cm").equals("1 m"); // → true
Quantity("1 m").lessThan("2 m"); // → true
Quantity("1 m").compareTo("2 m"); // → -1 (i.e. "less")
Quantity("1 m").isSame("1 m"); // → true (identical units, not just equal value)
Quantity("1 m").isSame("100 cm"); // → false

Quantity.min("10 hours", "720 min"); // → Quantity("10 hours")
Quantity.max(Quantity("5 m"), "3 m"); // → Quantity("5 m")

Predicates & introspection

Quantity("1 m").isCompatible("1 ft"); // → true
Quantity("1 m/s").isInverse("1 s/m"); // → true
Quantity("1 m").units(); // → "m"
Quantity("1 m/s").kind(); // → "speed"

Quantity("20 tempC").isTemperature(); // → true
Quantity("1 m").isBase(); // → true
Quantity("5").isUnitless(); // → true

Quantity.getKinds(); // → ["...", ...]
Quantity.getUnits("length"); // → ["...", ...]
Quantity.getAliases("m"); // → ["...", ...]

Custom units & registries

Core ships with the default unit registry. To add your own units or build an isolated registry, use @quantities/registry, and @quantities/units for ready-made specialized definitions.

Extensions

Register extra instance methods on the prototype via Quantity.extend(). See @quantities/to-compound for compound output ("1 kg 200 g").

License

MIT