Unitclass
Represent a single unit of measure within a UnitList, e.g. kilometer.
new Unit<K>(list: UnitList<K>, key: K, { to, ...options }: UnitProps<K>)These helpers model physical units of measure — mass, length, speed, area, volume, temperature, angles, percentages, and more — and let you convert amounts between units and format them for display. They exist so callers never have to hard-code conversion factors or wrangle Intl.NumberFormat unit options directly.
Things to know:
UnitList is an ImmutableMap of Unit instances. The first entry in the list is always the base unit (e.g. milligram for MASS_UNITS).Unit.to() and Unit.from() both default to the base unit when no target/source key is given.Unit.format() delegates to Intl.NumberFormat for natively supported units (so kilogram is localised automatically) and falls back to a polyfill for custom units like basis-point.import { MASS_UNITS, LENGTH_UNITS, TEMPERATURE_UNITS, SPEED_UNITS } from "shelving/util";
// Via the UnitList directly.
MASS_UNITS.convert(1, "kilogram", "pound"); // ≈ 2.205
// Via a Unit instance.
const km = LENGTH_UNITS.require("kilometer");
km.to(5, "mile"); // ≈ 3.107
km.from(26.2, "mile"); // ≈ 42.165
// Temperature (function-based conversion).
TEMPERATURE_UNITS.convert(100, "celsius", "fahrenheit"); // 212import { MASS_UNITS, VOLUME_UNITS } from "shelving/util";
MASS_UNITS.require("kilogram").format(2.5); // "2.5 kg"
VOLUME_UNITS.require("liter").format(1, { unitDisplay: "long" }); // "1 liter"Represent a single unit of measure within a UnitList, e.g. kilometer.
new Unit<K>(list: UnitList<K>, key: K, { to, ...options }: UnitProps<K>)Represent a list of related units of measure (e.g. all length units).
new UnitList<K>(units: ImmutableObject<K, UnitProps<K>>)
String key for a percentage unit.
MapKey<typeof PERCENT_UNITS>
String key for a point unit.
MapKey<typeof POINT_UNITS>
MapKey<typeof ANGLE_UNITS>
MapKey<typeof MASS_UNITS>
MapKey<typeof LENGTH_UNITS>
MapKey<typeof SPEED_UNITS>
MapKey<typeof AREA_UNITS>
MapKey<typeof VOLUME_UNITS>
MapKey<typeof TEMPERATURE_UNITS>
Percentage units.
Point units.
Angle units.
Mass units.
Length units.
Speed units.
Area units.
Volume units.
Temperature units.