new Unit()
Represents a baja:Unit in BajaScript.
When creating a Simple, always use the make() method instead of
creating a new Object.
Extends
Members
-
<static> DEFAULT :baja.Unit
-
Default
Unitinstance.Type:
-
<static> NULL :baja.Unit
-
Null
Unitinstance (same asDEFAULT).Type:
-
isPrefix
-
Returns true if the unit should be prefixed.
Methods
-
<static> convert(scalar, params)
-
Friendly API to convert a scalar value to a desired unit.
Parameters:
Name Type Description scalarnumber a scalar value to convert
paramsobject Properties
Name Type Argument Default Description fromUnitsbaja.Unit | string <optional>
params.units the units the scalar is currently in. Can be a unit name, or Units instance. If omitted, considered to be
toUnitswithunitConversionapplied.unitConversionstring | number | baja.FrozenEnum <optional>
used to convert between systems of measure.
toUnitsbaja.Unit | string <optional>
the desired units. If omitted, will be considered to be
fromUnitswithunitConversionapplied.precisionnumber <optional>
8 specify how many decimal places the resulting scalar should be rounded to.
- Since:
-
- Niagara 4.8
Returns:
the converted unit.
- Type
- Promise.<number>
Examples
What is 32 Fahrenheit, as shown in metric units?
return baja.Unit.convert(32, { units: 'fahrenheit', unitConversion: 'metric' }) .then(function (result) { console.log(result); // 0 });How many centimeters are in 1 inch?
return baja.Unit.convert(1, { fromUnits: 'inch', toUnits: 'centimeter' }) .then(function (result) { console.log(result); // 2.54 });My data is "12 inches", but the user wants to see metric units. What number should I show them in the UI?
return baja.Unit.convert(12, { fromUnits: 'inch', unitConversion: 'metric' }) .then(function (result) { console.log(result); // 30.48 });My user entered "100 cm", but my station logic is in inches. What number should I store in the station?
return baja.Unit.convert(100, { unitConversion: 'metric', toUnits: 'inch', precision: 2 }) .then(function (result) { console.log(result); // 39.37 }); -
<static> make(name [, symbol], dimension [, scale] [, offset] [, isPrefix])
-
Creates a new instance of
baja.Unit.Note that if calling this function directly, multiple calls with the same
name but different dimensions will result in an error (no messing with
the laws of physics!). Be careful not to manually createUnits with
incorrect dimensions that might clash with units specified in the unit
database stationside.Parameters:
Name Type Argument Default Description nameString the unit name. Must be unique and may not contain a
semicolon.symbolString <optional>
name the unit symbol. If omitted will default to
the unit name. May not contain a semicolon.dimensionbaja.Dimension the unit dimension
scaleNumber <optional>
1 the unit scale
offsetNumber <optional>
0 the unit offset
isPrefixBoolean <optional>
false true if the unit should be prefixed /
displayed on the left of the actual valueThrows:
if name or symbol contain a semicolon, the dimension is omitted,
or if duplicate units are created with the same name but different
dimensionsReturns:
- Type
- baja.Unit
-
<static> toDisplayUnits( [cx])
-
Get the desired unit as specified by the input context.
Parameters:
Name Type Argument Description cxObject <optional>
Properties
Name Type Argument Description unitsbaja.Unit <optional>
unitConversionbaja.FrozenEnum | number | string <optional>
accepts a
baja:UnitConversionenum, ordinal, or tag. If omitted,
baja.getUnitConversion()(the user-configured unit conversion) will be
used.showUnitsboolean <optional>
- Since:
-
- Niagara 4.8
- See:
Returns:
resolves the desired display unit as
specified. IfshowUnitsis false or the units are not specified or
NULL, resolvesnullto indicate that units are not used for display in
this context.- Type
- Promise.<(baja.Unit|null)>
Example
Calculate display units based on facets.
var facetsObj = point.getFacets().toObject(); return baja.Unit.toDisplayUnits(facetsObj) .then(function (displayUnits) { console.log('this point wants to be displayed with units ' + displayUnits); }); -
convertTo(toUnit, scalar)
-
Convert a scalar value from this unit to another.
Parameters:
Name Type Description toUnitbaja.Unit scalarnumber Throws:
-
if the two Units are not of the same Dimension
- Type
- Error
Returns:
the converted scalar value
- Type
- number
-
-
decodeAsync(str [, batch])
-
The string encoding of certain Simples may include Type information, or
other data that may be require asynchronous operations to decode. BOX is
designed to handle these situations when decoding data from the station,
but when user code needs to decode string-encoded Simples directly, prefer
this method as it gives the individual Simple a chance to import Types,
etc. to ensure that the decoded Simple is fully correct.The default implementation just returns
decodeFromStringdirectly.Parameters:
Name Type Argument Description strstring batchbaja.comm.Batch <optional>
optional batch to use
- Inherited From:
Returns:
may return the Simple instance
directly, or a Promise resolving to same - so wrap inPromise.resolve()
if unsure.- Type
- baja.Simple | Promise.<baja.Simple>
-
decodeFromString(str)
-
Parse a
baja.Unitfrom aString.Parameters:
Name Type Description strString - Overrides:
Throws:
if string is malformed or contains invalid unit parameters
Returns:
- Type
- baja.Unit
-
encodeToString()
-
Encode a
baja.Unitto aString.- Overrides:
Returns:
- Type
- String
-
equals(obj)
-
Equality test.
Parameters:
Name Type Description obj- Inherited From:
Returns:
- Type
- Boolean
-
equivalent(obj)
-
Equivalence test.
equivalent()is used to compare if two Objects have equivalent
state, but might not want to return true for equals since it
it has implied semantics for many operations. The default
implementation returns the result of baja.Object#equals.Parameters:
Name Type Description obj- Inherited From:
Returns:
- Type
- Boolean
-
getAgents( [is] [, batch])
-
Returns a promise that resolves to the agent list for this Object.
Parameters:
Name Type Argument Description isArray.<String> <optional>
An optional array of filters to add to the
agent query.batchbaja.comm.Batch <optional>
An optional object used to batch network
calls together.- Inherited From:
- See:
Returns:
A promise that will resolve with the Agent Info.
- Type
- Promise
-
getDataTypeSymbol()
-
Returns the data type symbol (
u) forFacetsencoding.Returns:
- Type
- String
-
getDimension()
-
Returns the unit dimension.
Returns:
- Type
- baja.Dimension
-
getIcon()
-
Return the Object's Icon.
- Inherited From:
Returns:
- Type
- baja.Icon
-
getOffset()
-
Returns the unit offset.
Returns:
- Type
- Number
-
getScale()
-
Returns the unit scale.
Returns:
- Type
- Number
-
getSymbol()
-
Returns the unit symbol.
Returns:
- Type
- String
-
getType()
-
Get the type of this instance.
- Inherited From:
Returns:
- Type
- Type
-
getTypeDisplayName( [cx])
-
Gets the friendly type display name for this object.
Parameters:
Name Type Argument Description cxObject <optional>
a context to be passed down to Type
- Since:
-
- Niagara 4.10
- Inherited From:
- See:
-
- baja.Type#getDisplayName
Returns:
If no context is provided, the type
display name is returned synchronously as a string. If context provided,
the type display name is resolved via a promise as a string.- Type
- Promise.<string> | string
-
getUnitName()
-
Returns the unit name.
Returns:
- Type
- String
-
make()
-
- Overrides:
- See:
Returns:
- Type
- baja.Unit
-
newCopy( [exact])
-
Every value may be cloned using the
newCopymethod.Please note that
Simples are immutable so they don't
allocate a new instance.Parameters:
Name Type Argument Description exactBoolean <optional>
true if an exact copy of the value should be
made (only valid in the Component architecture).- Inherited From:
- See:
Returns:
a copy of the value (or the same instance if the value is a
Simple). -
toDesiredUnit(unitConversion)
-
Get the unit that this unit converts to as specified by the given
conversion.Parameters:
Name Type Description unitConversionbaja.FrozenEnum | number | string accepts a
baja:UnitConversionenum, ordinal, or tag.- Since:
-
- Niagara 4.8
Returns:
- Type
- Promise.<baja.Unit>
Example
return fahrenheitUnit.toDesiredUnit('metric') .then(function (unit) { console.log(unit.getUnitName()); // Celsius }); -
toString()
-
Returns the unit symbol.
- Overrides:
Returns:
- Type
- String
-
valueOf()
-
- Inherited From:
Returns:
the string encoding of the Simple, by default
- Type
- String