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

2.0.1 • Public • Published

units-converter-ts

NPM Downloads NPM License

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

Acceleration

  • gal
  • g-force
  • m/s2
Angle

  • rev
  • rad
  • deg
  • grad
  • arcmin
  • arcsec
Apparent Power

  • mVA
  • VA
  • kVA
  • MVA
  • GVA
Area

  • mm2
  • cm2
  • m2
  • are
  • ha
  • km2
  • circ-mil
  • circ-inch
  • in2
  • ft2
  • yd2
  • ro
  • ac
  • mi2
  • twp
Charge

  • abC
  • c
  • mC
  • μC
  • nC
  • stC
  • pC
  • e
Current

  • stA
  • mA
  • A
  • abA
  • kA
Digital

  • b
  • Kb
  • Mb
  • Gb
  • Tb
  • B
  • KB
  • MB
  • GB
  • TB
Energy

  • J
  • kJ
  • mWh
  • Wh
  • kWh
  • MWh
  • GWh
  • Btu-it
Force

  • N
  • kN
  • lbf
Frequency

  • mHz
  • Hz
  • kHz
  • MHz
  • GHz
  • THz
  • rpm
  • deg/s
  • rad/s
Illuminance

  • lx
  • ft-cd
Length

  • mm
  • cm
  • m
  • km
  • in
  • hand
  • ft
  • ft-us
  • yd
  • fath
  • fur
  • mi
  • nMi
Mass

  • mg
  • g
  • kg
  • t
  • kt
  • mt
  • gr
  • dr
  • oz
  • lb
  • stone
  • qr
  • slug
  • tn
  • ton-uk
Pace

  • min/km
  • s/m
  • min/mi
  • s/ft
Power

  • mW
  • W
  • kW
  • MW
  • GW
Pressure

  • Pa
  • hPa
  • kPa
  • bar
  • MPa
  • torr
  • psi
  • ksi
Reactive Energy

  • mVARh
  • VARh
  • kVARh
  • MVARh
  • GVARh
Reactive Power

  • mVAR
  • VAR
  • kVAR
  • MVAR
  • GVAR
Speed

  • km/h
  • m/s
  • ft/s
  • mi/h
  • knot
Temperature

  • C
  • K
  • F
  • R
Time

  • ns
  • mu
  • ms
  • s
  • min
  • h
  • d
  • week
  • fortnight
  • month
  • year
Voltage

  • mV
  • V
  • kV
Volume

  • 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
Volume Flow Rate

  • 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

Package Sidebar

Install

npm i units-converter-ts

Weekly Downloads

85

Version

2.0.1

License

MIT

Unpacked Size

78.2 kB

Total Files

65

Last publish

Collaborators

  • zviadmestvi