Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Unit

Represents a standard measure that is used to express amounts.

A unit is always part of one single Group (attribute: Unit.group). It is always defined in relation to the base unit of its group.

The exact relation is defined in the converter functions Unit.fromBase and Unit.toBase. Thereby fromBase(val: number) => number defines the conversion from the base unit to this and toBase(val: number) => number vice versa. As a result of this simple concept, all units of a group are convertible into each other.

The 4th important property of each unit is its Unit.format. This defines the unit's shape. It allows firstly the parser to recognize the unit in a string and secondly it provides the basis for the .format() method of the Convertible.

Lastly every unit is part of a unit system (attribute: Unit.system). This meta information is used for the Convertible's .asBest() method. By default, the Convertible always remains in the same system of units.

Hierarchy

  • Unit

Index

Constructors

constructor

  • new Unit(format: UnitFormat, fromBase: Converter, toBase: Converter, system: string): Unit
  • new Unit(format: UnitFormat, ratio: number, shift: number, system: string): Unit
  • Creates a standard measure that is used to express amounts.

    Parameters

    • format: UnitFormat

      the unit's format, holds the unit's different symbols (e.g. m, metre, meters for the unit meter)

    • fromBase: Converter
    • toBase: Converter
    • system: string

      the unit system the unit is belonging to

    Returns Unit

  • Creates a standard measure that is used to express amounts.

    Using this constructor function one doesn't need to specify functions describing how to convert the unit to the group's base unit and vice versa.

    Instead one only specifies the mathematical relation the the base unit - in the form of a ratio and a shift.

    Example: You want to define the unit Fahrenheit and you have already defined the base unit Kelvin. You know the formula to convert a value from Fahrenheit to Kelvin is: °F * 5/9 + (45967/180) = °K. Using the other constructor function you had to write the following:

    new Unit({...}, (val) => (val - (45967/180)) / 5/9, (val) => val * 5/9 + (45967/180), ...);
    

    As you see there is repetitive code. Couldn't one save one of the two converter functions? Lets see how we create the unit using this constructor.

    new Unit({...}, 5/9, 45967/180, ...)
    

    5/9 is the ratio between the units, 45967/180 the shift. Much easier, isn't it? And no repetitive code!

    Parameters

    • format: UnitFormat

      the unit's format, holds the unit's different symbols (e.g. m, metre, meters for the unit meter)

    • ratio: number
    • shift: number
    • system: string

      the unit system the unit is belonging to

    Returns Unit

Properties

Readonly format

format: UnitFormat

The unit's {@link UnitFormat}. Holds the unit's unique symbols, which allow the parser to recognize the unit in a string. Added to that it provides the basis for the .format() method of the Convertible.

Readonly fromBase

fromBase: Converter

Converts a value from the group's base unit to this unit.

param

value in the group's base unit

returns

value in this unit

Readonly system

system: string

The unit system the unit is belonging to. E.g. imperial, metric, ...

This meta information is used for the Convertible's .asBest() method. By default, the Convertible always remains in the same system of units.

Readonly toBase

toBase: Converter

Converts a value from this unit to the group's base unit.

param

value in this unit

returns

value in the group's base unit

Accessors

group

  • The unit's Group. Units belonging to the same group are convertible into each other.

    Returns Group

Methods

computeNotations

  • computeNotations(): string[]
  • Creates a string array holding all this unit's different notations.

    Returns string[]

    a string array holding all this unit's different notations

possibilities

  • possibilities(): string[]
  • Returns an array of units to which this unit can be converted.

    Returns string[]

    an array of units to which this unit can be converted

toString

  • toString(): string
  • Returns the unit's standard string representation - therefore the most common symbol. For example 'm' is returned for the unit meter.

    Returns string

    the unit's standard string representation

Generated using TypeDoc