Unitclass

new Unit<K>(list: UnitList<K>, key: K, { to, ...options }: UnitProps<K>)
ParamType
listUnitList<K>
UnitList this unit belongs to. required
keyK
String key for this unit, e.g. kilometer. required
propsUnitProps<K>
Props to configure this unit (conversions and formatting options). required
Return
Unit<K>
Represent a single unit of measure within a UnitList, e.g. kilometer.
PropertyType
.listUnitList<K>
UnitList this unit belongs to. required readonly
.keyK
String key for this unit, e.g. kilometer required readonly
.optionsReadonly<UnitFormatOptions>
Possible options for formatting these units. readonly

Represent a single unit of measure within a UnitList, e.g. kilometer.

  • Knows how to convert amounts to/from other units in its list and to format them for display.

Examples

LENGTH_UNITS.require("kilometer").to(5, "meter") // 5000

Methods

Go

Unit.to()method

Convert an amount from this unit to another unit.

to(amount: number, targetKey?: K): number
Go

Unit.from()method

Convert an amount from another unit to this unit.

from(amount: number, sourceKey?: K): number
Go

Unit.format()method

Format an amount with a given unit of measure, e.g. 12 kg or 29.5 l

format(amount: number, options?: UnitFormatOptions): string