Class: Unit

baja. Unit


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 Unit instance.

Type:

<static> NULL :baja.Unit

Null Unit instance (same as DEFAULT).

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
scalar number

a scalar value to convert

params object
Properties
Name Type Argument Default Description
fromUnits baja.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 toUnits with unitConversion applied.

unitConversion string | number | baja.FrozenEnum <optional>

used to convert between systems of measure.

toUnits baja.Unit | string <optional>

the desired units. If omitted, will be considered to be fromUnits with unitConversion applied.

precision number <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 create Units with
incorrect dimensions that might clash with units specified in the unit
database stationside.

Parameters:
Name Type Argument Default Description
name String

the unit name. Must be unique and may not contain a
semicolon.

symbol String <optional>
name

the unit symbol. If omitted will default to
the unit name. May not contain a semicolon.

dimension baja.Dimension

the unit dimension

scale Number <optional>
1

the unit scale

offset Number <optional>
0

the unit offset

isPrefix Boolean <optional>
false

true if the unit should be prefixed /
displayed on the left of the actual value

Throws:

if name or symbol contain a semicolon, the dimension is omitted,
or if duplicate units are created with the same name but different
dimensions

Returns:
Type
baja.Unit

<static> toDisplayUnits( [cx])

Get the desired unit as specified by the input context.

Parameters:
Name Type Argument Description
cx Object <optional>
Properties
Name Type Argument Description
units baja.Unit <optional>
unitConversion baja.FrozenEnum | number | string <optional>

accepts a
baja:UnitConversion enum, ordinal, or tag. If omitted,
baja.getUnitConversion() (the user-configured unit conversion) will be
used.

showUnits boolean <optional>
Since:
  • Niagara 4.8
See:
Returns:

resolves the desired display unit as
specified. If showUnits is false or the units are not specified or
NULL, resolves null to 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
toUnit baja.Unit
scalar number
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 decodeFromString directly.

Parameters:
Name Type Argument Description
str string
batch baja.comm.Batch <optional>

optional batch to use

Inherited From:
Returns:

may return the Simple instance
directly, or a Promise resolving to same - so wrap in Promise.resolve()
if unsure.

Type
baja.Simple | Promise.<baja.Simple>

decodeFromString(str)

Parse a baja.Unit from a String.

Parameters:
Name Type Description
str String
Overrides:
Throws:

if string is malformed or contains invalid unit parameters

Returns:
Type
baja.Unit

encodeToString()

Encode a baja.Unit to a String.

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
is Array.<String> <optional>

An optional array of filters to add to the
agent query.

batch baja.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) for Facets encoding.

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
cx Object <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 newCopy method.

Please note that Simples are immutable so they don't
allocate a new instance.

Parameters:
Name Type Argument Description
exact Boolean <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
unitConversion baja.FrozenEnum | number | string

accepts a
baja:UnitConversion enum, 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