thingtalk-units
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

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

Unit Base Unit Physical quantity
s (seconds) ms time
min (minutes) ms time
h (hours) ms time
day (days) ms time
week (weeks) ms time
mon (business month = 30 days) ms time
year (business year = 365 days) ms time
km (kilometers) m distance
mm (millimeters) m distance
cm (centimeters) m distance
mi (miles) m distance
in (inches) m distance
kmph (kilometers per hour) mps speed
mph (miles per hour) mps speed
g (grams) kg mass
lb (US pound) kg mass
oz (US ounce) kg mass
bar (bar) Pa pressure
psi (pounds per square inch) Pa pressure
mmHg (millimeters of mercury) Pa pressure
inHg (inches of mercury) Pa pressure
F (Fahrenheit) C temperature
K (Kelvin) C temperature
kJ (kilojoule) kcal energy
KB (kilobyte) byte data size
KiB (kibibyte) byte data size
MB (megabyte) byte data size
MiB (mebibyte) byte data size
GB (gigabyte) byte data size
GiB (gibibyte) byte data size
TB (terabyte) byte data size
TiB (tebibyte) byte data size
kW (kilowatt) W power

Readme

Keywords

none

Package Sidebar

Install

npm i thingtalk-units

Weekly Downloads

238

Version

0.2.1

License

BSD-3-Clause

Unpacked Size

68.6 kB

Total Files

17

Last publish

Collaborators

  • gcampax
  • sileixu