パッケージの詳細

units-converter-ts

ZviadMestvi1.4kMIT2.0.1

A simple utility library for converting units

units, convert, conversion, units-converter

readme

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

<summary>Acceleration</summary>

- gal - g-force - m/s2

<summary>Angle</summary>

- rev - rad - deg - grad - arcmin - arcsec

<summary>Apparent Power</summary>

- mVA - VA - kVA - MVA - GVA

<summary>Area</summary>

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

<summary>Charge</summary>

- abC - c - mC - μC - nC - stC - pC - e

<summary>Current</summary>

- stA - mA - A - abA - kA

<summary>Digital</summary>

- b - Kb - Mb - Gb - Tb - B - KB - MB - GB - TB

<summary>Energy</summary>

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

<summary>Force</summary>

- N - kN - lbf

<summary>Frequency</summary>

- mHz - Hz - kHz - MHz - GHz - THz - rpm - deg/s - rad/s

<summary>Illuminance</summary>

- lx - ft-cd

<summary>Length</summary>

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

<summary>Mass</summary>

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

<summary>Pace</summary>

- min/km - s/m - min/mi - s/ft

<summary>Power</summary>

- mW - W - kW - MW - GW

<summary>Pressure</summary>

- Pa - hPa - kPa - bar - MPa - torr - psi - ksi

<summary>Reactive Energy</summary>

- mVARh - VARh - kVARh - MVARh - GVARh

<summary>Reactive Power</summary>

- mVAR - VAR - kVAR - MVAR - GVAR

<summary>Speed</summary>

- km/h - m/s - ft/s - mi/h - knot

<summary>Temperature</summary>

- C - K - F - R

<summary>Time</summary>

- ns - mu - ms - s - min - h - d - week - fortnight - month - year

<summary>Voltage</summary>

- mV - V - kV

<summary>Volume</summary>

- 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

<summary>Volume Flow Rate</summary>

- 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