@iamsquare/convert-units
v2.0.0
Published
Convert between quantities in different units
Downloads
11
Maintainers
Readme
iamsquare/convert-units
A handy utility for converting between quantities in different units.
Installation
npm i @iamsquare/convert-units
Usage
@iamsquare/convert-units
is a functional port of convert-units
with extended type support.
You start by instancing a Converter
:
import { Converter } from '@iamsquare/convert-units';
const converter = new Converter({ measuresData: { ... }});
If you want to use all measure types, just instance Converter
like so:
import { allMeasures } from '@iamsquare/convert-units';
const converter = new Converter({ measuresData: allMeasures }); // TMeasures => AllMeasures, TSystems => AllSystems, TUnitTypes => AllUnitType
If you want to use a subset of all measure type you must declare TMeasures
, TSystems
and TUnitTypes
explicitly (typescript can't infer types on its own if they get too complex. e.g.: two measure types with different systems):
import {
Converter,
area,
AreaSystems,
AreaUnits,
distance,
DistanceSystems,
DistanceUnits,
} from '@iamsquare/convert-units';
type Measures = 'area' | 'distance';
type Systems = AreaSystems | DistanceSystems;
type Units = AreaUnits | DistanceUnits;
const converter = new Converter<Measures, Systems, Units>({
area,
distance
});
Here's how you convert metric units for volume:
import { convert } from '@iamsquare/convert-units';
convert(converter, 'l', 'ml', 1);
// 1000
Jump from imperial to metric units the same way:
convert(converter, 'lb', 'kg', 1);
// 0.4536... (tested to 4 significant figures)
Just be careful not to ask for an impossible conversion!
convert(converter, 'oz', 'fl-oz', 1);
// throws exception -- you can't go from mass to volume!
You can ask to select the best unit for you. Optionally you can explicitly exclude orders of magnitude or specify a cutoff number for selecting the best representation.
convertToBest(converter, {}, 'mm', 12000);
// { value: 12, unitType: 'm' } (the smallest unit with a value above 1)
convertToBest(converter, { exclude: ['m'] }, 'mm', 12000);
// { value: 1200, unitType: 'cm' } (the smallest unit excluding meters)
convertToBest(converter, { cutOffNumber: 10 }, 'mm', 900);
// { value: 900, unitType: 'cm' } (the smallest unit with a value equal to or above 10)
convertToBest(converter, { cutOffNumber: 10 }, 'mm', 1000);
// { value: 10, unitType: 'm' } (the smallest unit with a value equal to or above 10)
You can get a list of the measurement types supported with measures
measures(converter);
// [ 'distance', 'mass', 'volume', ... ]
If you ever want to know the possible conversions for a unit, just use possibilities
import { possibilities } = from '@iamsquare/convert-units';
possibilities(converter, 'l');
// [ 'ml', 'l', 'tsp', 'Tbs', 'fl-oz', 'cup', 'pnt', 'qt', 'gal' ]
possibilities(converter, 'kg');
// [ 'mcg', 'mg', 'g', 'kg', 'oz', 'lb' ]
You can also get the possible conversions for a measure:
possibilities(converter, 'mass');
// [ 'mcg', 'mg', 'g', 'kg', 'oz', 'lb', 'mt', 't' ]
You can also get the all the available units:
possibilities(converter);
// [ 'mm', 'cm', 'm', 'in', 'ft-us', 'ft', 'mi', ... ];
To get a detailed description of a unit, use describe
describe(converter, 'kg');
{
unitType: 'kg',
measure: 'mass',
system: 'metric',
name: {
singular: 'SINGULAR_KILOGRAM',
plural: 'PLURAL_KILOGRAM'
}
}
To get detailed descriptions of all units, use list
.
list(converter);
[
{
unitType: 'kg',
measure: 'mass',
system: 'metric',
name: {
singular: 'SINGULAR_KILOGRAM',
plural: 'PLURAL_KILOGRAM'
}
},
...
]
You can also get detailed description of all units for a measure:
list(converter, 'mass');
[
{
unitType: 'kg',
measure: 'mass',
system: 'metric',
name: {
singular: 'SINGULAR_KILOGRAM',
plural: 'PLURAL_KILOGRAM'
}
},
...
]
NOTE: By default the i18n module is not active so until a translation dictionary is provided list
and describe
will return the raw dictionary key instead.
Documentation
For a more in-depth documentation take a look here
Supported Units
| Measure | Metric | Imperial | Other | |:-:|-|-|-| | Area | mm2, cm2, m2, ha, km2 | in2, ft2, ac, mi2 | - | | Acceleration | g-force, m/s2 | - | - | | Angle | - | - | deg, rad, grad, arcmin, arcsec | | Apparent Power | - | - | VA, mVA, kVA, MVA, GVA | | Charge | - | - | c, mC, μC, nC, pC | | Current | - | - | A, mA, kA | | Digital | - | - | b, Kb, Mb, Gb, Tb, B, KB, MB, GB, TB | | 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 | - | | Length | nm, μm, mm, cm, m, km | in, yd, ft-us, ft, fathom, mi, nMi | - | | 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 | psi, ksi | - | | 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.
You can check the list of these enums in the documentation
.
Want More?
Adding new measurement types is easy. Check one of these definitions
or unit tests
to see how they are implemented. When you're done you can import them just like any other measure provided by the library:
import {
Converter,
distance,
DistanceSystems,
DistanceUnits,
} from '@iamsquare/convert-units';
import {
customMeasure,
CustomMeasureSystems,
CustomUnits
} from './customMeasure';
type Measures = 'customMeasure' | 'distance';
type Systems = CustomMeasureSystems | DistanceSystems;
type Units = CustomUnits | DistanceUnits;
const converter = new Converter<Measures, Systems, Units>({
customMeasure,
distance
});
Feel free to open a PR if you think that your custom definition should be added to the library!
i18n
Each Converter
instance can be initialized with an additional translations
property.
For example if you want to use all the measure types and their respective translations, just instance Converter
like so:
import { Converter, allMeasures, allTranslations } from '@iamsquare/convert-units';
const converter = new Converter({ measureData: allMeasures, translations: allTranslations });
Or if you only need a subset of measures:
import {
Converter,
area,
areaTranslations,
AreaSystems,
AreaUnits,
distance,
distanceTranslations,
DistanceSystems,
DistanceUnits
} from '@iamsquare/convert-units';
type Measures = 'area' | 'distance';
type Systems = AreaSystems | DistanceSystems;
type Units = AreaUnits | DistanceUnits;
const converter = new Converter<Measures, Systems, Units>({ measureData: { area, distance }, translations: { ...areaTranslations, ...distanceTranslations }});
The translationModule
inside the Converter
class exposes three methods:
converter.translationModule.setTranslations({ ... })
converter.translationModule.mergeTranslations({ ... })
Both accept a JSON containing key-value pairs, where the key is a valid translationKey
and the value is a string
. The first one overwrites the current translations dictionary, the second merges the two dictionaries together.
converter.translationModule.resetTranslations();
Resets the translations dictionary to its original state (when the class has been instanced).
converter.translationModule.getTranslationByKey(key);
Returns the translation for a given key (or the key itself in case it's undefined in the dictionary).
Dependencies
This library depends on
ramda
andramda-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.