UNPKG

5.74 kBMarkdownView Raw
1convert-units
2=============
3
4[![Build Status](https://travis-ci.org/ben-ng/convert-units.png)](https://travis-ci.org/ben-ng/convert-units) [![Downloads](https://img.shields.io/npm/dm/convert-units.svg)](https://www.npmjs.com/package/convert-units)
5
6A handy utility for converting between quantities in different units.
7
8Installation
9-----
10
11```
12npm install convert-units --save
13```
14
15Usage
16-----
17
18`convert-units` has a simple chained API that is easy to read.
19
20Here's how you move between the metric units for volume:
21
22```js
23var convert = require('convert-units')
24
25convert(1).from('l').to('ml')
26// 1000
27```
28
29Jump from imperial to metric units the same way:
30
31```js
32convert(1).from('lb').to('kg')
33// 0.4536... (tested to 4 significant figures)
34```
35
36Just be careful not to ask for an impossible conversion:
37
38```js
39convert(1).from('oz').to('fl-oz')
40// throws -- you can't go from mass to volume!
41```
42
43You 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.
44```js
45convert(12000).from('mm').toBest()
46// 12 Meters (the smallest unit with a value above 1)
47
48convert(12000).from('mm').toBest({ exclude: ['m'] })
49// 1200 Centimeters (the smallest unit excluding meters)
50
51convert(900).from('mm').toBest({ cutOffNumber: 10 });
52// 900 Centimeters (the smallest unit with a value equal to or above 10)
53
54convert(1000).from('mm').toBest({ cutOffNumber: 10 })
55// 10 Meters (the smallest unit with a value equal to or above 10)
56```
57
58You can get a list of the measurement types supported with `.measures`
59
60```js
61convert().measures()
62// [ 'length', 'mass', 'volume' ]
63```
64
65If you ever want to know the possible conversions for a unit, just use `.possibilities`
66
67```js
68convert().from('l').possibilities()
69// [ 'ml', 'l', 'tsp', 'Tbs', 'fl-oz', 'cup', 'pnt', 'qt', 'gal' ]
70
71convert().from('kg').possibilities()
72// [ 'mcg', 'mg', 'g', 'kg', 'oz', 'lb' ]
73```
74
75You can also get the possible conversions for a measure:
76```js
77convert().possibilities('mass')
78// [ 'mcg', 'mg', 'g', 'kg', 'oz', 'lb', 'mt', 't' ]
79```
80
81You can also get the all the available units:
82```js
83convert().possibilities()
84// [ 'mm', 'cm', 'm', 'in', 'ft-us', 'ft', 'mi', 'mcg', 'mg', 'g', 'kg', 'oz', 'lb', 'mt', 't', 'ml', 'l', 'tsp', 'Tbs', 'fl-oz', 'cup', 'pnt', 'qt', 'gal', 'ea', 'dz' ];
85```
86
87To get a detailed description of a unit, use `describe`
88
89```js
90convert().describe('kg')
91/*
92 {
93 abbr: 'kg'
94 , measure: 'mass'
95 , system: 'metric'
96 , singular: 'Kilogram'
97 , plural: 'Kilograms'
98 }
99*/
100```
101
102To get detailed descriptions of all units, use `list`.
103
104```js
105convert().list()
106/*
107 [{
108 abbr: 'kg'
109 , measure: 'mass'
110 , system: 'metric'
111 , singular: 'Kilogram'
112 , plural: 'Kilograms'
113 }, ...]
114*/
115```
116
117You can also get detailed descriptions of all units for a measure:
118
119```js
120convert().list('mass')
121/*
122 [{
123 abbr: 'kg'
124 , measure: 'mass'
125 , system: 'metric'
126 , singular: 'Kilogram'
127 , plural: 'Kilograms'
128 }, ...]
129*/
130```
131
132Supported Units
133---------------
134### Length
135* mm
136* cm
137* m
138* in
139* ft-us
140* ft
141* mi
142
143### Area
144* mm2
145* cm2
146* m2
147* ha
148* km2
149* in2
150* ft2
151* ac
152* mi2
153
154### Mass
155* mcg
156* mg
157* g
158* kg
159* oz
160* lb
161* mt
162* t
163
164### Volume
165* mm3
166* cm3
167* ml
168* l
169* kl
170* m3
171* km3
172* tsp
173* Tbs
174* in3
175* fl-oz
176* cup
177* pnt
178* qt
179* gal
180* ft3
181* yd3
182
183### Volume Flow Rate
184* mm3/s
185* cm3/s
186* ml/s
187* cl/s
188* dl/s
189* l/s
190* l/min
191* l/h
192* kl/s
193* kl/min
194* kl/h
195* m3/s
196* m3/min
197* m3/h
198* km3/s
199* tsp/s
200* Tbs/s
201* in3/s
202* in3/min
203* in3/h
204* fl-oz/s
205* fl-oz/min
206* fl-oz/h
207* cup/s
208* pnt/s
209* pnt/min
210* pnt/h
211* qt/s
212* gal/s
213* gal/min
214* gal/h
215* ft3/s
216* ft3/min
217* ft3/h
218* yd3/s
219* yd3/min
220* yd3/h'
221
222### Temperature
223* C
224* F
225* K
226* R
227
228### Time
229* ns
230* mu
231* ms
232* s
233* min
234* h
235* d
236* week
237* month
238* year
239
240### Frequency
241* Hz
242* mHz
243* kHz
244* MHz
245* GHz
246* THz
247* rpm
248* deg/s
249* rad/s
250
251### Speed
252* m/s
253* km/h
254* m/h
255* knot
256* ft/s
257
258### Pace
259* s/m
260* min/km
261* s/ft
262* min/km
263
264### Pressure
265* Pa
266* hPa
267* kPa
268* MPa
269* bar
270* torr
271* psi
272* ksi
273
274### Digital
275* b
276* Kb
277* Mb
278* Gb
279* Tb
280* B
281* KB
282* MB
283* GB
284* TB
285
286### Illuminance
287* lx
288* ft-cd
289
290### Parts-Per
291* ppm
292* ppb
293* ppt
294* ppq
295
296### Voltage
297* V
298* mV
299* kV
300
301### Current
302* A
303* mA
304* kA
305
306### Power
307* W
308* mW
309* kW
310* MW
311* GW
312
313### Apparent Power
314* VA
315* mVA
316* kVA
317* MVA
318* GVA
319
320### Reactive Power
321* VAR
322* mVAR
323* kVAR
324* MVAR
325* GVAR
326
327### Energy
328* Wh
329* mWh
330* kWh
331* MWh
332* GWh
333* J
334* kJ
335
336### Reactive Energy
337* VARh
338* mVARh
339* kVARh
340* MVARh
341* GVARh
342
343### Angle
344* deg
345* rad
346* grad
347* arcmin
348* arcsec
349
350### Want More?
351
352Adding new measurement sets is easy. Take a look at [`lib/definitions`](https://github.com/ben-ng/convert-units/tree/master/lib/definitions) to see how it's done.
353
354License
355-------
356Copyright (c) 2013-2017 Ben Ng and Contributors, http://benng.me
357
358Permission is hereby granted, free of charge, to any person
359obtaining a copy of this software and associated documentation
360files (the "Software"), to deal in the Software without
361restriction, including without limitation the rights to use,
362copy, modify, merge, publish, distribute, sublicense, and/or sell
363copies of the Software, and to permit persons to whom the
364Software is furnished to do so, subject to the following
365conditions:
366
367The above copyright notice and this permission notice shall be
368included in all copies or substantial portions of the Software.
369
370THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
371EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
372OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
373NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
374HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
375WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
376FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
377OTHER DEALINGS IN THE SOFTWARE.