UNPKG

metric-prefix

Version:

Stringify big.js numbers using only few significant digits and a metric prefix

43 lines (40 loc) 2.03 kB
const test = require('tape') const { prefix } = require('../') const closePrefix = require('../')({ delimiter: '' }) const watt = require('../')({ unit: 'W' }) const unit = 'Hz' // some example unit test('big numbers', t => { t.equal(prefix(7), '7.00', 'no conversion') t.equal(prefix(12, { unit }), '12.0 Hz', 'custom unit') t.equal(prefix(999), '999', 'no conversion') t.equal(prefix(1001, { precision: 4 }), '1.001 k', 'custom precision') t.equal(prefix(1009, { delimiter: '' }), '1.00k', 'custom delimiter') t.equal(watt(2500), '2.50 kW', 'custom defaults') t.equal(prefix(12345), '12.3 k', 'kilo converstion') t.equal(prefix(999999), '999 k', 'floor result') t.equal(prefix(1e6), '1.00 M', 'mega converstion') t.equal(prefix(1e6 + 9, { unit, precision: 7 }), '1.000009 MHz', 'mega converstion') t.equal(prefix(1e6 + 9, { unit, precision: 6 }), '1.00000 MHz', 'mega converstion') t.equal(closePrefix(1.1e9), '1.10G', 'custom defaults') t.equal(closePrefix(1.1e9, { unit }), '1.10GHz', 'custom defaults') t.equal(prefix(-10.9e9), '-10.9 G', 'giga converstion') t.equal(prefix(100e15), '100 P', 'peta converstion') t.equal(prefix('100000000000000000000000000'), '100 Y', 'yotta conversion') t.equal(prefix(1e35), '100 V', 'veca conversion') t.equal(prefix(1e36), '1.00 ?', 'out of range') t.end() }) test('small numbers', t => { t.equal(prefix(0), '0.00', 'zero conversion') t.equal(prefix(0, { precision: 1 }), '0', 'zero conversion') t.equal(prefix(0.7), '700 m', 'milli conversion') t.equal(prefix(0.01), '10.0 m', 'milli conversion') t.equal(prefix(-0.001), '-1.00 m', 'milli conversion') t.equal(prefix(0.001239), '1.23 m', 'milli conversion') t.equal(prefix(0.0001), '100 µ', 'mycro conversion') t.equal(prefix(0.00099999), '999 µ', 'mycro conversion') t.equal(prefix('0.000000000000000000000000000000001'), '1.00 v', 'vocta conversion') t.equal(prefix(1e-33), '1.00 v', 'vocta conversion') t.equal(prefix(9.99e-34), '999 ?', 'out of range') t.end() })