0.4.6 • Published 3 years ago

@cbevins/uom v0.4.6

Weekly downloads
25
License
MIT
Repository
github
Last release
3 years ago

npm.io uom

UOM is a units-of-measure parser, compiler, and converter.

npm.io Installation

> npm install @cbevins/uom

npm.io Example Usage

// Import the Uom singleton
import { Uom } from '@cbevins/uom'

// Determine if two units-of-measure terms are convertible
Uom.convertible('m', 'ft') // returns true
Uom.convertible('m', 's') // returns false

// Convert a value from one units-of-measure into another units-of-measure
Uom.convert(1.23, 'm', 'ft') // returns 1.23 / 0.3048
Uom.convert(1.23, 'ft', 'm') // returns 1.23 * 0.3048

// Determine a units-of-measure quantity type
Uom.quantity('ha') // returns 'area'
Uom.quantity('J') // returns 'energy, work, heat OR moment of force'

Along with the tables of valid units names recognized by the Uom Parser (see next section), that's all you need to know to effectively use this package.


npm.io Units Names

The Parser recognizes the following unit names and aliases:

SI Base Units

NameAliases
dlratio, ''
mmeter, metre, meters, metres
kgkilogram, kilograms, kgs
ssec, secs, second, seconds
oCCelsius, celsius, centigrade,
Aampere, amp, amperes, amps
cdcandela, candelas
molmole, moles, mols

SI derived units with special names and symbols

NameAliases
radradian, radians, rads
srsteridian, steridians, srs
HzHertz, hertz, hz
NNewton, Newtons, newton, newtons
PaPascal, Pascals, Pas, pascal, pascals
JJoule, Joules, Js, joule, joules, j
WWatt, Watts, Ws, watt, watts, w
Ccoulomb, coulombs, Coulomb, Coulombs
Vvolt, volts, v, Volt, Volts
Ffarad, farads, Farad, Farads
Ohmohm, ohms, Ohms,
Ssiemens, Siemens
Wbweber, webers, Weber, Webers
Ttesla, teslas, Tesla, Teslas
Hhenry, henrys, Henry, Henrys
lmlumen, lumens
lxlux
Bqbecquerel, Becquerel, becquerels, Becquerels
Gygray, grays, Gray, Grays
Svsievert, sieverts, Sievert, Sieverts
katkatal, katals

Non-SI units accepted for use with SI units

NameAliases
Length
mmmillimeter, millimeters, millimetre, millimetres
cmcentimeter, centimeters, centimetre, centrimetres
dmdecimeter, decimeters, decimetre, decimetres
kmkms, kilometer, kilometre, kilometers, kilometres
au'astronomical unit'
Time
msmillisec, millisecs, millisecond, milliseconds
minminute, minutes
hhour, hours, hr, hrs
dayd, days
yyear, years
Temperature
oKK,
Plane, Phase Angle
degreedegrees, deg, degs, o, °
'
"
Area
hahectare, hectares
Volume
lL litre, liter, litres, liters
Mass
gmgram, grams, ms, g
tonnet, tonnes
Energy
eVelectronvolt, eVs
MJmegaJ, mJ, mj, megaJoules
Dimensionless
percent%, pct, pph
pptpartsPerThousand
ppmpartsPerMillion

US Customary units

NameAliases
Length
ftfoot, feet
ininch, inches
rdrod, rods
ydyard, yards
chchain, chains
mimile, miles
Mass
lbpound, pounds, lbs
ozounce, ounces, ozs
tontons shortton shorttons
Temperature
oF
Area
acacre, acres
Energy
BtuBtus, btu, btus (mean Btu)
Btu_ibtu_i (international table Btu, after 1956)
Btu_xbtu_x (mean Btu)
Btu_39btu_39 (Btu at 39)
Btu_59btu_59 (Btu at 59)
Btu_60btu_60 (Btu at 60)
Btu_tcbtu_tc (thermochemical Btu)
Slope
slope

npm.io How Uom Works

UOM Parser

The UOM Parser takes a units-of-measure string such as 'lbs/ft2' and decomposes it into an array of coefficient, unit, power triplets. Units name aliases are replaced with their canonical keys. For example:

Parser InputParser Output
'pounds/foot2'[[1, 'lb', 1], [1, 'ft', -2]]
'tons/acre'[[1, 'ton', 1], [1, 'ac', -1]]
'Joules s-1'[[1, 'J', 1], [1, 's', -1]]
'W'[[1, 'W', 1]

Unit Strings

Unit strings passed as input into the Parser must be composed of:

  • one or more unit terms optionally separated by a '/'
  • where a unit term is
    • a unit name or alias, and
    • an optional power integer that is optionally signed.

In other words, any continuous series of letters or any continuous series of (optionally signed) numbers are parsed into a single token, whether or not they are separated by any whistespace characters.

The following valid unit strings:

  • 'lb/ft2'
  • 'lb / ft2'
  • 'lb1 / ft2'
  • 'lb1/ ft2'
  • 'lb1 /ft2'
  • 'lb+1ft-2'
  • 'pound 1 foot -2'
  • 'feet-2 pounds+1'
  • 'feet-2pounds+1'

all produce equivalent Parser results:

  • [[1, 'lb', 1], [1, 'ft', -2]], or
  • [[1, 'ft', -2], [1, 'lb', -1]]

an array of triplets where:

  • the first element is a coefficient,
  • the second is a units term, and
  • the third element is an exponent

UOM Compiler

The UOM Compiler takes the Parser output and recursively decomposes each of its triplets into Systeme International base units, powers, and coefficients (a sort of units signature). Some examples include:

Parser Output (Compiler Input)Compiler Output (Units Signature)
[[1, 'lb', 1], [1, 'ft', -2]][[0.45359237, 'kg', 1], [0.09290304, 'm', -2]
[[1, 'ton', 1], [1, 'ac', -2]][[907.18474, 'kg', 1], [4046.8564224, 'm', -2]
[[, 'J', 1], [1, 's', -1]][1, 'J', 1], [1, 's', -1]
[1, 'W', 1][1, 'J', 1], [1, 's', -1]

UOM Converter

The UOM Converter uses the Parser and Compiler to convert a quantity amount between units-of-measure

Converter.convert(amount, fromUnits, intoUnits)

where fromUnits and intoUnits are strings composed of 1 or more units terms such as

convert(1.23, 'Btus/ft/s', 'W')

The Converter first checks if the term (i.e., Btu/ft/s) already exists in its cache of pre-compiled unit signatures. If not, it parses and compiles the string into its units signature and stores it in the cache. It then uses the units signature to perform the conversion.

0.4.6

3 years ago

0.4.5

3 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.1

3 years ago

0.4.2

3 years ago

0.4.0

3 years ago