Package Exports
- @iamsquare/convert-units
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@iamsquare/convert-units) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
iamsquare/convert-units
A handy utility for converting between quantities in different units.
Installation
npm i @iamsquare/convert-unitsUsage
@iamsquare/convert-units is a functional port with extended type support of https://www.npmjs.com/package/convert-units.
Here's how you convert metric units for volume:
import { convert } from '@iamsquare/convert-units';
convert('l', 'ml', 1);
// 1000Jump from imperial to metric units the same way:
convert('lb', 'kg', 1);
// 0.4536... (tested to 4 significant figures)Just be careful not to ask for an impossible conversion!
convert('oz', 'fl-oz', 1);
// throws exception -- you can't go from mass to volume!You can get a list of the measurement types supported with measures
measures();
// [ 'length', 'mass', 'volume', ... ]If you ever want to know the possible conversions for a unit, just use possibilities
import { possibilities } = from '@iamsquare/convert-units';
possibilities('l');
// [ 'ml', 'l', 'tsp', 'Tbs', 'fl-oz', 'cup', 'pnt', 'qt', 'gal' ]
possibilities('kg');
// [ 'mcg', 'mg', 'g', 'kg', 'oz', 'lb' ]You can also get the possible conversions for a measure:
possibilities('mass');
// [ 'mcg', 'mg', 'g', 'kg', 'oz', 'lb', 'mt', 't' ]You can also get the all the available units:
possibilities();
// [ 'mm', 'cm', 'm', 'in', 'ft-us', 'ft', 'mi', ... ];To get a detailed description of a unit, use describe
describe('kg');{
unitType: 'kg',
measure: 'mass',
system: 'metric',
singular: 'Kilogram',
plural: 'Kilograms'
}To get detailed descriptions of all units, use list.
list();[
{
unitType: 'kg',
measure: 'mass',
system: 'metric',
singular: 'Kilogram',
plural: 'Kilograms'
},
...
]You can also get detailed description of all units for a measure:
list('mass');[
{
unitType: 'kg',
measure: 'mass',
system: 'metric',
singular: 'Kilogram',
plural: 'Kilograms'
},
...
]Supported Units
| Measure | Metric | Imperial | Other |
|---|---|---|---|
| Acceleration | m/s2, g0, g-sun, g-mercury, g-venus, g-mars, g-saturn, g-jupiter, g-neptune, g-pluto, g-moon | - | - |
| Angle | - | - | deg, rad, grad, arcmin, arcsec |
| Apparent Power | - | - | VA, mVA, kVA, MVA, GVA |
| Area | mm2, cm2, m2, ha, km2 | in2, ft2, ac, mi2 | - |
| Charge | - | - | c, mC, μC, nC, pC |
| Current | - | - | A, mA, kA |
| Digital | - | - | b, kb, Mb, Gb, Tb, Pb, kib, Mib, Gib, Tib, Pib, B, kB, MB, GB, TB, PB, kiB, MiB, GiB, TiB, PiB |
| Distance | nm, μm, mm, cm, m, km | in, yd, ft-us, ft, fathom, mi, nMi | - |
| Each | - | - | ea, dz |
| Energy | - | - | Wh, mWh, kWh, MWh, GWh, J, kJ |
| Force | N, kN | lbf | - |
| Frequency | - | - | Hz, mHz, kHz, MHz, GHz, THz, rpm, deg/s, rad/s |
| Illuminance | lx | ft-cd | - |
| Mass | mcg, mg, g, kg, mt | oz, lb, t | - |
| Pace | s/m, min/km | s/ft, min/mi | - |
| Parts-Per | - | - | ppm, ppb, ppt, ppq |
| Pieces | - | - | pcs, bk-doz, cp, doz-doz, doz, gr-gr, gros, half-dozen, long-hundred, ream, scores, sm-gr, trio |
| Power | W, mW, kW, MW, GW, PS | Btu/s, ft-lb/s, hp | - |
| Pressure | Pa, hPa, kPa, MPa, bar, torr, mmHg (@ 0°C) | psi, ksi, inHg (@ 32°F) | - |
| Reactive Energy | - | - | VARh, mVARh, kVARh, MVARh, GVARh |
| Reactive Power | - | - | VAR, mVAR, kVAR, MVAR, GVAR |
| Speed | m/s, km/h | mph, knot, ft/s | - |
| Temperature | C, K | F, R | - |
| Time | - | - | ns, mu, ms, s, min, h, d, week, month, year |
| Voltage | - | - | V, mV, kV |
| 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 | - |
NOTE: these units are exported as enums so that you don't have to remember which unit is which. Also, if you're using this library in a plain javascript environment these are useful to avoid annoying typos. To see a list of these enums take a look in here.
Want More?
Adding new measurement sets is easy. Take a look at
src/definitions
to see how it's done.
Dependencies
This library depends on
ramdaandramda-extension.
These imports are not tree-shaken, so if you're bundling this library in a project please refer to your bundler's documentation. Note that Rollup and Webpack should handle tree-shaking without extra-effort.
Roadmap
Check the kanban board here
Development
If you want to help remember to run the command npm run setup:dev.
It will install all dependencies and husky so that code will get tested and linted before every commit.
License
Copyright (c) 2021 Marco Cesi, https://iamsquare.it
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.