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

numeric-unit-parse-kit

v0.1.0

Published

Parse generic numeric values with units into structured numbers, units, diagnostics, and normalized strings.

Readme

numeric-unit-parse-kit

License: MPL-2.0 CI

Parse numeric values with units such as 12px, 50 gold, -1.5turn, 100%, or 0 into structured results with readable diagnostics.

numeric-unit-parse-kit is a small clean-room toolkit for forms, config editors, import tools and design-token utilities that need to validate a single number plus an optional unit. It is deliberately narrower than a CSS parser, expression parser or unit converter.

Links: Demo · GitHub

Package quality

  • TypeScript types are generated from the source.
  • ESM-only package with no runtime dependencies.
  • Marked as side-effect free for bundlers.
  • Browser-friendly implementation with no Node-only APIs.
  • CI runs npm ci, typecheck, build, and test.
  • Tested on Node.js 20 and 22 with GitHub Actions.

Publication status

This package is currently a GitHub preview and is queued for npm publication. The browser demo is available now, and the install command below is the command to use once the npm package is published.

Install after npm publication

npm install numeric-unit-parse-kit

Quick Start

import {
  createNumericUnitParser,
  formatNumericUnit,
  isNumericUnit,
  parseNumericUnit
} from "numeric-unit-parse-kit";

const parsed = parseNumericUnit("  50 gold  ", {
  allowedUnits: ["gold", "silver"],
  requireUnit: true
});

if (parsed.ok) {
  parsed.value.amount;
  // 50

  parsed.value.unit;
  // "gold"

  parsed.value.normalized;
  // "50gold"
}

isNumericUnit("12px", { allowedUnits: ["px", "rem"] });
// true

formatNumericUnit({ amount: 12, unit: "gold" }, { separator: " " });
// "12 gold"

const cssLength = createNumericUnitParser({
  allowedUnits: ["px", "rem", "%"],
  requireUnit: true
});

cssLength.isValid("1.5rem");
// true

Why this package

Small numeric-unit inputs show up in many places: design tokens, spacing controls, game configs, quota settings, billing quantities, virtual currencies, custom measurements and import columns.

Boolean validation is often not enough. This package returns stable issue codes and keeps the parsed value small:

const result = parseNumericUnit("12bananas", {
  allowedUnits: ["px", "rem"]
});

result.issues;
// [
//   {
//     code: "unit-not-allowed",
//     message: "Unit \"bananas\" is not in the allowed unit list.",
//     input: "12bananas",
//     normalizedInput: "12bananas",
//     unit: "bananas"
//   }
// ]

API

parseNumericUnit(input, options?)

Returns a discriminated result:

type ParseNumericUnitResult =
  | { ok: true; value: NumericUnit; issues: [] }
  | { ok: false; value: null; issues: NumericUnitIssue[] };

type NumericUnit = {
  amount: number;
  unit: string;
  raw: string;
  normalized: string;
};

Example:

const result = parseNumericUnit("-1.5turn", {
  allowedUnits: ["deg", "rad", "turn"],
  allowNegative: true
});

isNumericUnit(input, options?)

Boolean shortcut for parseNumericUnit(input, options).ok.

isNumericUnit("100%", { allowPercent: true });
// true

formatNumericUnit(value, options?)

Formats { amount, unit } back to a string.

formatNumericUnit({ amount: 1.2345, unit: "px" }, { maximumFractionDigits: 2 });
// "1.23px"

formatNumericUnit({ amount: 12, unit: "gold" }, { separator: " " });
// "12 gold"

By default, formatNumericUnit({ amount: 0, unit: "px" }) returns "0". Pass { unitlessZero: false } when the unit should be preserved.

createNumericUnitParser(defaultOptions?)

Creates a small parser object that reuses the same defaults across a form, importer or config validator. Per-call options override the defaults.

const cssLength = createNumericUnitParser({
  allowedUnits: ["px", "rem"],
  allowPercent: false,
  requireUnit: true
});

cssLength.parse("12px");
cssLength.isValid("12rem");
cssLength.format({ amount: 12, unit: "px" });

cssLength.isValid("50%", {
  allowPercent: true,
  allowedUnits: ["px", "%"]
});
// true

Options

Parse options

| Option | Default | Description | | --- | --- | --- | | allowedUnits | none | Optional list of accepted units. Empty unit values are still allowed unless requireUnit rejects them. | | caseSensitiveUnits | true | Compare allowedUnits using exact casing by default. | | requireUnit | false | Reject values without units. | | allowUnitlessZero | true | Allow 0 when requireUnit is enabled. | | allowNegative | true | Allow negative numeric amounts. | | allowPercent | true | Allow % as a unit. | | trim | true | Trim surrounding whitespace before parsing. |

Format options

| Option | Default | Description | | --- | --- | --- | | unitlessZero | true | Format zero as 0 even when a unit is present. | | maximumFractionDigits | none | Round the amount with Intl.NumberFormat. | | separator | "" | String placed between amount and unit. |

Diagnostics

Issue codes are stable and intended for UI messages, logs or localization:

  • empty
  • invalid-type
  • invalid-syntax
  • non-finite
  • negative-not-allowed
  • unit-required
  • unit-not-allowed
  • percent-not-allowed

Limits

This package parses one numeric value and one unit. It does not:

  • parse formulas such as calc(100% - 1rem);
  • parse ranges such as 1-3px;
  • parse compound dimensions such as 10px 20px;
  • convert between units;
  • validate CSS grammar;
  • infer whether a unit makes sense for a domain.

Use it as a small validation layer before domain-specific checks.

License

MPL-2.0