Units Module Reference

Table of contents


Introduction

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.


Unit strings

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.

Distance

Fuel efficiency

Pressure

Speed

Temperature

Time

Volume

Weight

Constants

Distance

Volume

Weight


Exported identifiers and functions

All exported values are available from common/units.js.

Conversion functions

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.

Angle

Distance

Fuel efficiency

Pressure

Speed

Temperature

Time

Volume

Weight

Constants

Constants are available for import from common/mvcs/exports.js.

Distance

Volume

Weight


units

Syntax

import {units} from 'common/mvcs/export.js';

The units object contains the properties and methods used to define and manage units.


units properties

defaultUnits

An 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 methods

addConversion()

Syntax

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.

Parameters


convert()

Syntax

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.

Parameters

Return value

A Number. The Converted value.


fmtAbbrev()

Syntax

fmtAbbrev(unitStr)

Return the abbreviation string for the unitstr unit string. By default, the abbreviation is the unit string followed by a '.'.

Parameters

Return value

A String.


getCurrentUnits()

Syntax

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.

Parameters

Return value

A unit string, or undefined if the type has not been registered.


isUnitString()

Syntax

isUnitString(str)

Return true if str is an existing unit string.

Parameters

Return value

A Boolean.


setAbbrev()

Syntax

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 '.'.

Parameters


toCurrentUnits()

Syntax

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.

Parameters

Return value

A Number. The value converted to the current units for unitType.