mu-conv
v0.1.0
Published
Standards-based (UCUM) unit conversion in float64, exact rational, and 34-digit decimal; mattwiller/mu compiled to Wasm
Maintainers
Readme
mu-conv
UCUM unit conversion for JavaScript, powered by the mu Go library compiled to WebAssembly.
Converts quantities between commensurable units in three numeric formats: float64, exact rationals, and
fixed-precision decimals at 34 significant digits.
Install
npm install mu-convUsage
import { load } from "mu-conv";
const mu = await load(); // hydrate the embedded data tables, ~7 ms
mu.convert(1, "kg", "[lb_av]"); // 2.2046226218487757 (number)
mu.convertString("1", "kg", "[lb_av]"); // "2.2046226218487757"
mu.convertRational("1", "m", "cm"); // "100" (exact)
mu.convertDecimal("100", "Cel", "[degF]"); // "212"
mu.convertString("1", "KG", "G", { caseInsensitive: true });
mu.validate("kg.m/s2"); // returns undefined; throws on invalid
mu.commensurable("B[W]", "W"); // true: dimensioned special unitconvert takes and returns a JS number (float64); the other functions take values as strings and return strings.
All throw Error on failure.
load() instantiates the bundled module once and memoizes it; concurrent callers share the instance.
A failed load is not cached, so it can be retried.
Numeric formats
convert— numeric fast path using native JS number (float64) valuesconvertString— accepts anythingparseFloatwould; returns Go's'g'shortest-round-trip formatconvertRattional— accepts fraction syntax ("3/2","1.5","1e10") and returns an exact fraction as a stringconvertDecimal— 34 significant digits, rendered in plain decimal notation as a string
License
BSD-3-Clause. The embedded UCUM tables carry their own terms — see
LICENSE-UCUM.md.
