Unit

Usage

Creation

const myUnit = new Unit("meter", {
baseName: "length",
aliases: ["m", "meters"],
prefixes: 'decimal'
})

Specifiers

You can specify the prefix using the specifier(...) function.

const nanoUnit = myUnit.specifier(UnitPrefix.NANO);

Hierarchy (view full)

Constructors

Properties

UNITS: Map<string, Unit> = ...
UNIT_BASES: Map<string, string> = ...
UNKNOWN: Unit = ...

Accessors

Methods

  • Convert a value in the current unit to a target unit

    Type Parameters

    Parameters

    • value: T

      Value to convert

    • target: string | Unit

      Target unit

    Returns T

    Converted unit

  • Convert a value from a specific unit to a target unit

    Type Parameters

    Parameters

    • value: T

      Value to convert

    • from: string | Unit

      Source unit

    • to: string | Unit

      Target unit

    Returns T

    Converted unit

  • Find a unit by its name

    Parameters

    • name: string

      Unit name

    • Optional baseName: string

      Optional base name to specific result

    Returns Unit

    Unit if found

  • Get a unit from JSON

    Type Parameters

    Parameters

    • json: any

      JSON object

    Returns T

    Unit if found

  • Register a new unit

    Parameters

    • unit: Unit

      Unit to register

    • override: boolean = false

      Override an existing unit with the same name

    Returns Unit

    Registered unit