The units module contains the units object and set of specific conversion functions.
The units object coordinates unit conversion for state variables. It provides a central way of naming units, providing unit abbreviations, and converting between dynanically specified units.
units is available for import from common/mvcs/exports.js.
Units are specified in string. The strings are usually related to the unit abbreviation for convenience, but are not identical (e.g., 'kg' vs 'kg.'). Unit strings may be added dynamically but many are built in. By default, the abbreviation is the unit string followed by a '.'.
The following list is the pre-defined units strings alaong with their abbreviations.
'cm' - centimeters'm' - meters'km' - kilometers'in' - inches'ft' - feet'sm' - statute miles'nm' - nautical miles'nmpl' - nautical miles per liter'nmpg' - nautical miles per gallon'hpa' - hectopascals'inhg' - inches of Mercury'mps' - meters per second'mph' - miles per hour'km' - kilometers'kt' - knots'dC' - °C (abbreviation: °C)'dF' - °F (abbreviation: °F)'sec' - seconds'min' - minutes'hr' - hours'l' - liters'gal' - gallons'kg' - kilograms'lb' - poundsAll exported values are available from common/units.js.
The units module provides a set of conversion functions that provide a simpler interface to popular conversions. Conversion functions all take a single numeric argument and return a numeric value.
Constants are available for import from common/mvcs/exports.js.
unitsimport {units} from 'common/mvcs/export.js';
The units object contains the properties and methods used to define and manage units.
units propertiesdefaultUnitsAn object that maps unit-type strings (e.g. 'weight', 'fuel') to the default unit string used by the application for that type. defaultUnits is read directly when looking up the default unit for a type, and is written by the app at startup to establish which units are in use.
The initial value is an empty object {}. The app populates it before any unit-aware computation takes place. For example:
units.defaultUnits = {weight: 'lb', fuel: 'gal', distance: 'nm'};
getCurrentUnits() reads from defaultUnits by default, so setting entries here is sufficient for apps that do not need dynamic unit switching.
units methodsaddConversion()addConversion(from, to, fn)
Add a new conversion. The conversion units are specified as from and to. fn is a function that, given a value in from units, will return the value in to units. If the unit strings are not already present, they will be recorded in the internal list of available unit strings. Here's an example
units.addConversion('ft', 'm', ftToM);
Once a conversion is added, it is available for use via the convert() method.
fromtofnvalue argument in from units and returns the equivalent value in to units. If a string, it must be an existing unit string for which conversions from from to fn and fn to to already exist. The addConversion() method will then compose the two existing conversion functions to make the function that converts from units to to units.convert()convert(value, from, to)
Return value converted from units from to units to. If the to and from unit strings are identical the value is returned.
valuefromtoA Number. The Converted value.
fmtAbbrev()fmtAbbrev(unitStr)
Return the abbreviation string for the unitstr unit string. By default, the abbreviation is the unit string followed by a '.'.
unitStrA String.
getCurrentUnits()getCurrentUnits(unitType)
Returns the current unit string for the given unitType. By default this returns units.defaultUnits[unitType], making it equivalent to a direct lookup. Apps that support dynamic unit switching (e.g., an in-flight toggle between 'lb' and 'kg') may replace this property with a function that returns the currently active unit for the type.
unitType'weight', 'fuel'). Must match a key set in defaultUnits.A unit string, or undefined if the type has not been registered.
isUnitString()isUnitString(str)
Return true if str is an existing unit string.
strA Boolean.
setAbbrev()setAbbrev(unitStr, abbrev)
Add a custom abbreviation for unitStr. The abbrev string will override any existing abbreviations. By default, the abbreviation is the unit string followed by a '.'.
unitStrabbrevtoCurrentUnits()toCurrentUnits(unitType, value)
Converts value from the default units for unitType to the currently active units for that type, as returned by getCurrentUnits(). If the current units are the same as the default units, value is returned unchanged.
unitType'weight', 'fuel'). Must match a key set in defaultUnits.valuedefaultUnits[unitType] units.A Number. The value converted to the current units for unitType.