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 and the conversion functions and constants are exported from source/app/common/option/units.js. It is not re-exported from common/mvcs/export.js. Other common modules import it as ../option/units.js, and apps import it as ../common/option/units.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 '.', unless the unit string already ends with a '.'. Abbreviations that differ from that default are noted below.
The following list is the pre-defined unit strings (those registered by addConversion() in units.js) along with their abbreviations.
'cm' - centimeters'm' - meters'in' - inches'ft' - feet'sm' - statute miles'nm' - nautical miles'nmpl' - nautical miles per liter'nmpg' - nautical miles per gallon'gph' - gallons per hour'gpm' - gallons per minute'lph' - liters per hour'pph' - pounds per hour'kgph' - kilograms per hour'fpnm' - feet per nautical mile'pct' - percent (opposite/adjacent) (abbreviation: %)'hpa' - hectopascals'inhg' - inches of Mercury'mps' - meters per second'mph' - miles per hour'kt' - knots'kias' - knots indicated airspeed'mias' - miles per hour indicated airspeed'dC' - °C (abbreviation: °C)'dF' - °F (abbreviation: °F)'C' - °C'F' - °F'dCr' - relative (difference) °C'dFr' - relative (difference) °F'min' - minutes'hr' - hours'l' - liters'gal' - gallons'kg' - kilograms'lb' - pounds (abbreviation: lb.)Two further abbreviations are set for the fuel-weight strings 'lbf' (lb.) and 'kgf' (kg.), although no conversions are currently registered for them. The strings 'km' and 'sec' are not registered unit strings (isUnitString() returns false for them), although conversion functions involving kilometers and seconds exist.
The following conversions are registered with units.addConversion() and can be used with convert(). Pairs marked with a conversion function use it directly. The others are composed from existing conversions.
inhg ↔ hpa.ft ↔ m, ft ↔ in, m ↔ cm, cm ↔ in, ft ↔ cm, m ↔ in, nm ↔ sm.gal ↔ l.lb ↔ kg.nmpg ↔ nmpl.gpm ↔ gph, gph ↔ lph, pph ↔ kgph.kt ↔ mph, kt ↔ mps, mph ↔ mps, kias ↔ mias.dC ↔ dF, C ↔ F, dCr ↔ dFr.hr ↔ min.fpnm ↔ pct.All exported values are available from source/app/common/option/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.
FT_PER_NM).Constants are exported from source/app/common/option/units.js.
FT_PER_M / 100)unitsimport {units} from '../common/option/units.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. If a custom abbreviation has been set with setAbbrev() it is returned. Otherwise, the abbreviation is the unit string followed by a '.', unless the unit string already ends with 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 '.' (unless it already ends with 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.