UnitListclass

new UnitList<K>(units: ImmutableObject<K, UnitProps<K>>)
ParamType
unitsImmutableObject<K, UnitProps<K>>
Set of units keyed by their string key — the first entry becomes the base unit. required
Return
UnitList<K>
Represent a list of related units of measure (e.g. all length units).
PropertyType
.baseUnit<K>
Base unit for this list (the first unit added). required readonly

Represent a list of related units of measure (e.g. all length units).

  • Has a known base unit at .base
  • Can get required units from .unit()
  • Cannot have additional units added after it is created.

Examples

new UnitList({ meter: {}, kilometer: { to: { meter: 1000 } } }).convert(1, "kilometer", "meter") // 1000

Methods

Go

UnitList.convert()method

Convert an amount from a unit in this list to another unit in this list.

convert(amount: number, sourceKey: K, targetKey: K): number
Go

UnitList.require()method

Require a unit from this list.

require(key: K): Unit<K>