@smartput/mass
v0.2.0
Published
Milligram to ton, with the imperial pounds and ounces.
Maintainers
Readme
@smartput/mass
Milligram to ton, with the imperial pounds and ounces.
Metric and avoirdupois in one table, canonical in grams.
Three doors, one table. The kind descriptor is for the
engine, /validate is free functions over JS numbers, /class is an
immutable value class — and all three read the same UnitTable, so a unit
added once is added everywhere. See Validating without the
engine.
Setup
npm add @smartput/massExample
import { parseMass, asMass, formatMass } from "@smartput/mass/validate";
import { Mass } from "@smartput/mass/class";
parseMass("500 g") // {"ok":true,"value":500,"unit":"g","raw":"500"}
asMass("500 g", "mg") // {"ok":true,"value":500000,"unit":"mg","raw":"500000"}
formatMass(parseMass("500 g")) // "500g"
String(Mass.parse("500 g")) // "500g"
// …and the same table through the engine:
engine.evaluate("1 kg + 500 g").formatted // "1.5 kilograms"
engine.evaluate("3 lbs").formatted // "3 pounds"
engine.evaluate("2 t in kg").formatted // "2,000 kilograms"parse and as never throw — they answer { ok: true, … } or
{ ok: false, code, input }. The value class throws on bad input and
Mass.tryParse does not. All three read the one UnitTable below.
Entry points
| Import | Contents |
| --- | --- |
| @smartput/mass | The package root. |
| @smartput/mass/units | The UnitTable: ratios and aliases, with no engine and no Decimal. |
| @smartput/mass/validate | Free functions over JS numbers. Ok \| Err, never a throw. |
| @smartput/mass/class | The immutable value class. |
| @smartput/mass/locale/<id> | One language's words for this kind. 17 ship: en, de, fr, es, pt, it, nl, zh, ja, ar, ru, pl, tr, hi, ko, id, uk. |
Units
Read from the table itself, not typed out — a unit added to
the source appears here on the next bun run docs:packages. Ratios are decimal
strings, which is what lets the engine widen them to Decimal without a float
in between.
MASS_UNITS
| Unit | Ratio to g | Aliases |
| --- | --- | --- |
| g | 1 | g gram grams |
| mg | 0.001 | mg milligram milligrams |
| kg | 1000 | kg kilo kilos kilogram |
| t | 1000000 | t tonne tonnes |
| oz | 28.349523125 | oz ounce ounces |
| lb | 453.59237 | lb lbs pound pounds |
Runtime exports
Type-only exports are erased and do not appear here.
MASS_UNITS · mass
Dependencies
What it costs
Ceilings, not measurements. bun run check-size bundles each entry with
bun build --minify and fails if a row crosses its ceiling or drops more
than 30 % below it — a budget that is only an upper bound reports a vanished
graph as a triumph.
| Import | Minified | Gzipped | | --- | --- | --- | | mass/validate parseMass only | ≤ 1.4 kB | ≤ 750 B |
Generated by scripts/gen-readmes.ts — run bun run docs:readmes. Every
output above was produced by running the line beside it. The full page, with
live demos, is docs/packages/mass.md.
