0.2.1 • Published 4 years ago

thingtalk-units v0.2.1

Weekly downloads
94
License
BSD-3-Clause
Repository
github
Last release
4 years ago

thingtalk-units

Build Status Coverage Status Dependency Status Language grade: JavaScript

A small library to convert and normalize between measurement units. This library is used by the ThingTalk language to support measurements.

The library converts to and from base units of various supported types, and various additional units for each base unit.

Usage

const TTUnits = require('thingtalk-units');

API

function normalizeUnit(unit : String) : String

Convert a unit name to the base unit, which is a standard unit for the same property. This method throws if the unit is invalid. Example:

TTUnits.normalizeUnit('F') === 'C'
TTUnits.normalizeUnit('km') === 'm'

function transformToBaseUnit(value : Number, unit : String) : Number

Convert the value value, expressed in the unit unit, in the same amount in the corresponding base unit. Example:

TTUnits.transformToBaseUnit(1, 'km') === 1000

function transformFromBaseUnit(value : Number, unit : String) : Number

Convert the value value, expressed in the base unit, to the unit unit. This is the inverse operation to transformToBaseUnit(). Example:

TTUnits.transformToBaseUnit(1000, 'km') === 1

const BaseUnit : Array

The list of the supported base units.

Supported Units

Base Units

  • ms: time (milliseconds)
  • m: distance (meters)
  • mps: speed (meters per second)
  • kg: mass (kilograms)
  • Pa: pressure (Pascal)
  • C: temperature (Celsius)
  • kcal: energy (kilocalories)
  • byte: data size (Byte)
  • W: power (Watt)
  • lm: luminous flux (lumen)
  • lx: illuminance (lux)

The choice of base units is mostly historical. Some units follow SI convention (mps, kg, Pa), some follow common usage (kcal, C).

Additional Units

UnitBase UnitPhysical quantity
s (seconds)mstime
min (minutes)mstime
h (hours)mstime
day (days)mstime
week (weeks)mstime
mon (business month = 30 days)mstime
year (business year = 365 days)mstime
km (kilometers)mdistance
mm (millimeters)mdistance
cm (centimeters)mdistance
mi (miles)mdistance
in (inches)mdistance
kmph (kilometers per hour)mpsspeed
mph (miles per hour)mpsspeed
g (grams)kgmass
lb (US pound)kgmass
oz (US ounce)kgmass
bar (bar)Papressure
psi (pounds per square inch)Papressure
mmHg (millimeters of mercury)Papressure
inHg (inches of mercury)Papressure
F (Fahrenheit)Ctemperature
K (Kelvin)Ctemperature
kJ (kilojoule)kcalenergy
KB (kilobyte)bytedata size
KiB (kibibyte)bytedata size
MB (megabyte)bytedata size
MiB (mebibyte)bytedata size
GB (gigabyte)bytedata size
GiB (gibibyte)bytedata size
TB (terabyte)bytedata size
TiB (tebibyte)bytedata size
kW (kilowatt)Wpower