units-converter-ts
v2.0.1
Published
A simple utility library for converting units
Downloads
795
Maintainers
Readme
units-converter-ts
A simple utility library for converting units. Inspired by convert-units and units-converter. I didn't liked the syntax used in these libraries so I decided to make my own one.
Installation
npm install units-converter-ts --save
Usage
The package is simple and straightforward to use. First, you need to import the specific measure:
import { mass } from 'units-converter-ts';
or if you're using Node.js:
const { mass } = require('units-converter-ts');
Then you can use the convert method to convert units from that measurement. The function takes three arguments: the source unit, the target unit and the value to convert, respectively:
mass.convert('kg', 'g', 1);
// 1000
In the example 1 kilogram (kg) is being converted to grams (g) which will return 1000.
You can also import all objects at once like so:
import * as converter from 'units-converter-ts';
or if you're using Node.js:
const converter = require('units-converter-ts');
In this case you would use it like this:
converter.mass.convert('kg', 'g', 1);
// 1000
There is another alternative, if you don't want to use specific measure, you can import convert object and convert any unit. For example:
import { convert } from 'units-converter-ts';
or for Node.js:
const converter = require('units-converter-ts');
and use it like so:
converter.convert.convert('mm', 'm', 1000);
// 1
Examples
The library has other methods as well, here are use cases for all of them.
Get list of measures:
convert.measures();
/// [ 'mass', 'length', 'speed', ... ]
Get list of all available units:
convert.possibilities();
// [ 'mm2', 'deg', 'mC', 'ml', 'l', 'fl-oz', ... ]
Get list of units for specific measure:
power.possibilities();
// [ 'mW', 'W', 'kW', 'MW', 'GW' ]
// OR
convert.possibilities('power');
// [ 'mW', 'W', 'kW', 'MW', 'GW' ]
Get list of units for specific measures:
convert.possibilities('power', 'pace');
// [ "mW", "W", "kW", "MW", "GW", "min/km", "s/m", "min/mi", "s/ft"]
Get list of all unit descriptions
convert.list();
/*
[{
"abbreviation": "gal",
"measure": "acceleration",
"plural": "gals"
"singular": "gal",
"system": "metric",
},
{
"abbreviation": "m/s2",
"measure": "acceleration",
"plural": "metres per second squared"
"singular": "metre per second squared",
"system": "metric",
}
...
]
*/
Get list of unit descriptions for specific measure:
illuminance.list();
/*
[{
"abbreviation": "lx",
"measure": "illuminance",
"plural": "lux"
"singular": "lux",
"system": "metric",
},
{
"abbreviation": "ft-cd",
"measure": "illuminance",
"plural": "foot-candles"
"singular": "foot-candle",
"system": "imperial",
}]
*/
// OR
convert.list('illuminance');
/*
[{
"abbreviation": "lx",
"measure": "illuminance",
"plural": "lux"
"singular": "lux",
"system": "metric",
},
{
"abbreviation": "ft-cd",
"measure": "illuminance",
"plural": "foot-candles"
"singular": "foot-candle",
"system": "imperial",
}]
*/
Get list of units for specific measures:
convert.list('illuminance', 'force');
/*
[{
"abbreviation": "lx",
"measure": "illuminance",
"plural": "lux"
"singular": "lux",
"system": "metric",
},
{
"abbreviation": "ft-cd",
"measure": "illuminance",
"plural": "foot-candles"
"singular": "foot-candle",
"system": "imperial",
}
{
abbreviation: 'N',
measure: 'force',
plural: 'newtons',
singular: 'newton',
system: 'metric',
},
{
abbreviation: 'kN',
measure: 'force',
plural: 'kilonewtons',
singular: 'kilonewton',
system: 'metric',
},
{
abbreviation: 'lbf',
measure: 'force',
plural: 'pound-forces',
singular: 'pound-force',
system: 'imperial',
}]
*/
Get information about specific unit:
convert.describe('kN');
/*
{
abbreviation: "kN",
measure: "force",
plural: "kilonewtons",
singular: "kilonewton",
system: "metric"
}
*/
Units
- gal
- g-force
- m/s2
- rev
- rad
- deg
- grad
- arcmin
- arcsec
- mVA
- VA
- kVA
- MVA
- GVA
- mm2
- cm2
- m2
- are
- ha
- km2
- circ-mil
- circ-inch
- in2
- ft2
- yd2
- ro
- ac
- mi2
- twp
- abC
- c
- mC
- μC
- nC
- stC
- pC
- e
- stA
- mA
- A
- abA
- kA
- b
- Kb
- Mb
- Gb
- Tb
- B
- KB
- MB
- GB
- TB
- J
- kJ
- mWh
- Wh
- kWh
- MWh
- GWh
- Btu-it
- N
- kN
- lbf
- mHz
- Hz
- kHz
- MHz
- GHz
- THz
- rpm
- deg/s
- rad/s
- lx
- ft-cd
- mm
- cm
- m
- km
- in
- hand
- ft
- ft-us
- yd
- fath
- fur
- mi
- nMi
- mg
- g
- kg
- t
- kt
- mt
- gr
- dr
- oz
- lb
- stone
- qr
- slug
- tn
- ton-uk
- min/km
- s/m
- min/mi
- s/ft
- mW
- W
- kW
- MW
- GW
- Pa
- hPa
- kPa
- bar
- MPa
- torr
- psi
- ksi
- mVARh
- VARh
- kVARh
- MVARh
- GVARh
- mVAR
- VAR
- kVAR
- MVAR
- GVAR
- km/h
- m/s
- ft/s
- mi/h
- knot
- C
- K
- F
- R
- ns
- mu
- ms
- s
- min
- h
- d
- week
- fortnight
- month
- year
- mV
- V
- kV
- mm3
- cm3
- ml
- cl
- dl
- l
- kl
- m3
- km3
- krm
- tsk
- msk
- kkp
- glas
- kanna
- tsp
- Tbs
- in3
- fl-oz
- cup
- pnt
- qt
- gal
- ft3
- yd3
- mm3/s
- cm3/s
- ml/s
- cl/s
- dl/s
- l/s
- l/min
- l/h
- kl/s
- kl/min
- kl/h
- m3/s
- m3/min
- m3/h
- km3/s
- tsp/s
- Tbs/s
- in3/s
- in3/min
- in3/h
- fl-oz/s
- fl-oz/min
- fl-oz/h
- cup/s
- pnt/s
- pnt/min
- pnt/h
- qt/s
- gal/s
- gal/min
- gal/h
- ft3/s
- ft3/min
- ft3/h
- yd3/s
- yd3/min
- yd3/h