Metrick
raw JSON → 0.2.0 verified Sat Apr 25 auth: no javascript
Metrick is a Babel-powered unit conversion library (v0.2.0) that leverages the ES7 function bind syntax (::) for human-readable unit expressions. It supports duration (milliseconds, seconds, minutes, hours, years) and data (bits, bytes, kilobytes, megabytes, gigabytes, etc.) conversions with explicit (unit::unit.in(target)) and implicit (number::unit) syntax. The library requires the Babel plugin transform-function-bind and is designed for Node.js and browser environments. Alternatives like js-quantities or convert-units offer more units or simpler syntax but lack the syntactic sugar of the bind operator. Release cadence is low; last release was 2017.
Common errors
error SyntaxError: Unexpected token : ↓
cause Using :: bind syntax without Babel plugin or in unsupported environment.
fix
Install and configure Babel with @babel/plugin-proposal-function-bind.
error Module not found: Can't resolve 'metrick' ↓
cause Importing from 'metrick' when the package does not export units from the main entry.
fix
Import from subpaths like 'metrick/duration' for unit functions.
error TypeError: seconds.in is not a function ↓
cause Using .in on a number instead of a Unit object. The correct syntax is number::unit.in(targetUnit).
fix
Use the :: operator: 30::seconds.in(milliseconds) instead of 30.seconds.in(milliseconds).
Warnings
deprecated Babel plugin transform-function-bind is deprecated in Babel 7+; use @babel/plugin-proposal-function-bind instead. ↓
fix Replace 'transform-function-bind' with '@babel/plugin-proposal-function-bind' in .babelrc.
breaking The function bind syntax (::) is not supported in Node.js or browsers without Babel; code will fail to parse. ↓
fix Always transpile code with Babel and the function bind plugin before running.
gotcha Importing from 'metrick' (index) only exports Unit class; unit modules are at subpaths like 'metrick/duration'. ↓
fix Use correct subpath imports: import { seconds } from 'metrick/duration'.
gotcha Unit conversions use SI prefixes (e.g., kilograms = 1000 grams), not binary prefixes (e.g., kibibytes). ↓
fix Use 'metrick/data' for data units; note that megabyte = 1e6 bytes, not 2^20.
Install
npm install metrick yarn add metrick pnpm add metrick Imports
- seconds wrong
import { seconds } from 'metrick'correctimport { seconds } from 'metrick/duration' - Unit wrong
import { Unit } from 'metrick'correctimport Unit from 'metrick/unit' - megabytes wrong
const { megabytes } = require('metrick');correctimport { megabytes } from 'metrick/data'
Quickstart
// Ensure babel-plugin-transform-function-bind is installed and configured in .babelrc
import { seconds, minutes, milliseconds } from 'metrick/duration';
import { megabytes, gigabytes } from 'metrick/data';
// Explicit conversion
const msInMinute = 1::minutes.in(milliseconds);
console.log(`1 minute = ${msInMinute} ms`); // 60000
// Implicit conversion (defaults to SI base unit)
const bitsInMegabyte = 1::megabytes;
console.log(`1 megabyte = ${bitsInMegabyte} bits`); // 8000000
// Chain conversions
const gbInMb = 1::gigabytes.in(megabytes);
console.log(`1 GB = ${gbInMb} MB`); // 1000
// Create custom units
import Unit from 'metrick/unit';
const lux = new Unit();
const lumen = new Unit(10.7639::lux);
const brightness = 100::lumen.in(lux);
console.log(`100 lumens = ${brightness} lux`); // 1076.39