What is convert-units?
The convert-units npm package is a utility for converting between various units of measurement. It supports a wide range of units across different categories such as length, mass, volume, temperature, and more.
What are convert-units's main functionalities?
Length Conversion
This feature allows you to convert between different units of length. In the code sample, 1 meter is converted to feet.
const convert = require('convert-units');
const result = convert(1).from('m').to('ft');
console.log(result); // 3.28084
Mass Conversion
This feature allows you to convert between different units of mass. In the code sample, 1 kilogram is converted to pounds.
const convert = require('convert-units');
const result = convert(1).from('kg').to('lb');
console.log(result); // 2.20462
Volume Conversion
This feature allows you to convert between different units of volume. In the code sample, 1 liter is converted to fluid ounces.
const convert = require('convert-units');
const result = convert(1).from('l').to('fl-oz');
console.log(result); // 33.814
Temperature Conversion
This feature allows you to convert between different units of temperature. In the code sample, 100 degrees Celsius is converted to Fahrenheit.
const convert = require('convert-units');
const result = convert(100).from('C').to('F');
console.log(result); // 212
Speed Conversion
This feature allows you to convert between different units of speed. In the code sample, 60 kilometers per hour is converted to miles per hour.
const convert = require('convert-units');
const result = convert(60).from('km/h').to('mph');
console.log(result); // 37.2823
Other packages similar to convert-units
convert-units
A similar package that provides unit conversion functionalities. It supports a wide range of units and categories, making it a versatile choice for various conversion needs.
unit-converter
This package offers a simple API for converting between different units of measurement. It is less comprehensive than convert-units but can be a good choice for basic conversions.
units-converter
A package that provides conversion functions for a variety of units. It is similar to convert-units but may have a different set of supported units and categories.
convert-units
A handy utility for converting between quantities in different units.
Installation
npm install convert-units --save
Usage
convert-units
has a simple chained API that is easy to read.
Here's how you move between the metric units for volume:
var convert = require('convert-units')
convert(1).from('l').to('ml')
Jump from imperial to metric units the same way:
convert(1).from('lb').to('kg')
Just be careful not to ask for an impossible conversion:
convert(1).from('oz').to('fl-oz')
You can ask convert-units
to select the best unit for you. You can also optionally explicitly exclude orders of magnitude or specify a cut off number for selecting the best representation.
convert(12000).from('mm').toBest()
convert(12000).from('mm').toBest({ exclude: ['m'] })
convert(900).from('mm').toBest({ cutOffNumber: 10 });
convert(1000).from('mm').toBest({ cutOffNumber: 10 })
You can get a list of the measurement types supported with .measures
convert().measures()
If you ever want to know the possible conversions for a unit, just use .possibilities
convert().from('l').possibilities()
convert().from('kg').possibilities()
You can also get the possible conversions for a measure:
convert().possibilities('mass')
You can also get the all the available units:
convert().possibilities()
To get a detailed description of a unit, use describe
convert().describe('kg')
To get detailed descriptions of all units, use list
.
convert().list()
You can also get detailed descriptions of all units for a measure:
convert().list('mass')
Supported Units
Length
Area
Mass
Volume
- mm3
- cm3
- ml
- l
- kl
- m3
- km3
- 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'
Temperature
Time
- ns
- mu
- ms
- s
- min
- h
- d
- week
- month
- year
Frequency
- Hz
- mHz
- kHz
- MHz
- GHz
- THz
- rpm
- deg/s
- rad/s
Speed
Pace
Pressure
Digital
Illuminance
Parts-Per
Voltage
Current
Power
Apparent Power
Reactive Power
Energy
Reactive Energy
Angle
Want More?
Adding new measurement sets is easy. Take a look at lib/definitions
to see how it's done.
License
Copyright (c) 2013-2017 Ben Ng and Contributors, http://benng.me
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.