Babel-powered unit magick
;// Explicit syntax;// Implicit syntax;
Here, two syntaxes are demonstrated; explicit and implicit!
;;console; // => 1073.741824console; // => 9132.5
The explicit syntax makes use of an
in function to convert the original unit into another supplied unit. Passing a valid Unit object of the same type to
in will return the result as a
;;console; // => 50000000console; // => 86400000
Implicit syntax will convert the number to the default unit of the type of unit you're using. For instance, Data units implicitly convert to bits, and Duration units implicitly convert to milliseconds.
Shoutout to Glen Maddern for the idea behind implicit syntax
Configure Babel to transform function bind syntax by installing
babel-plugin-transform-function-bind with your favourite Node package management tool, and then adding the plugin to your
Custom unit types
A base Unit constructor exists, which you can extend to create your own classes of unit. All you need to do is extend it!
For instance, here's an implementation of an Illuminance unit type (a unit unlikely to be included with Metrick 😜);
;// No overrides are necessary; we're using classes to check for conversion compatibility!// SI base unit, implicit unitconst lux = ;const footcandle = 10763910417::lux;// pluralise for better readabilityconst footcandles = footcandle;const lumenPerSquareInch = 15500031::lux;const phot = 10000::lux;
Each type of unit exposes a constructor to create a compatible unit type. The constructors accept two forms of defining the relationship between units;
Numberargument, indicating the multiplier between the unit and the base unit
Functions, each accepting one numeric argument and transform to and from the base unit
For instance, here's an implementation of .beats as a Duration unit, using the multiplier argument;
;// each .beat is 1/1000th of a day, Durations are measured in secondsconst dotBeat = 1 / 1000::days;// pluralise for better readabilityconst dotBeats = dotBeat;console; // => 1.4400000000000002console; // => 17.280000000000005
As an example of providing Functions, here's an implementation of a Gas Mark temperature conversion;
;const gasMark =temperature * 14 + 39415temperature - 39415 / 14;console; // => 135console; // => 191
Currently, Metrick includes units for Data and Duration.
Note: All built-in units are exported with their singular and plural names - here we only list plural for brevity.
bits(SI base unit, implicit unit)
seconds(SI base unit)
metres(SI base unit, implicit unit)
Note: Built-in temperature units are exported both with and without
degrees prefix - here we only list without for brevity.
kelvin(SI base unit, implicit unit)
The library also includes a main entry point at
index.js, which exposes all the available parts of the library. This is not recommended as if, for example, you Webpack the whole library, you'll include conversions you may not be using!
;;console // => trueconsole; // => true
Wait, how does this work?
This is making use of the function bind syntax extension. This extension adds a
This syntax extension is experimental - it's not even in any specification, merely a proposal which happens to have a transform available in Babel.
Likewise, this is technically a bit of an abuse of this; implicit syntax takes advantage of the fact that the bind operator implicitly makes a call to the
bind method of what is supposed to be a supplied function, and instead returns the implicit unit conversion in these cases.
Also, technically, the function bind proposal suggests that the syntax check that the function passed is callable, but Babel's transform does not test this. Metrick instead uses an object which happens to have a
bind method, so this particular approach may break in more strict, future versions of the transformation - but hopefully not!