From 68d89fb0b270bef59c68b98cc3636182f03b1474 Mon Sep 17 00:00:00 2001 From: Jakka Prihatna Date: Fri, 25 Jul 2025 09:12:59 +0700 Subject: [PATCH 1/6] test: add test for bigint --- test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test.js b/test.js index ef7b313..cf8456c 100644 --- a/test.js +++ b/test.js @@ -26,6 +26,12 @@ describe('pretty', function() { assert(typeof pretty(nano(time)) === 'string'); }); + it('should support bigint:', function() { + const start = process.hrtime.bigint(); + const time = process.hrtime.bigint(); + assert.equal(typeof pretty(time - start), 'string'); + }); + it('should support time increment as second arg:', function() { const time = [1200708, 795428088]; assert.equal(pretty(time, 'w'), '2w'); From 2151cb5426b21abd0f560a8224b909caf17f49d1 Mon Sep 17 00:00:00 2001 From: Jakka Prihatna Date: Fri, 25 Jul 2025 09:20:32 +0700 Subject: [PATCH 2/6] feat: support passing time as bigint --- index.js | 4 +++- utils.js | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d768ae3..a1bd612 100644 --- a/index.js +++ b/index.js @@ -23,7 +23,9 @@ module.exports = (time, smallest, digits) => { smallest = null; } - let num = isNumber ? time : utils.nano(time); + const val = typeof time === 'bigint' ? utils.hrTime(time) : time; + + let num = Array.isArray(val) ? utils.nano(val) : val; let res = ''; let prev; diff --git a/utils.js b/utils.js index b84a63f..81c3cb1 100644 --- a/utils.js +++ b/utils.js @@ -1,3 +1,5 @@ +exports.hrTime = (time) => [Number(BigInt(time) / BigInt(1e9)), Number(BigInt(time) % BigInt(1e9))]; + exports.nano = time => +time[0] * 1e9 + +time[1]; exports.scale = { From 44609056ef17f288e10c0b9f743fa32a5bac063f Mon Sep 17 00:00:00 2001 From: Jakka Prihatna Date: Fri, 25 Jul 2025 09:24:25 +0700 Subject: [PATCH 3/6] test: add more tests for passing time as bigint --- test.js | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- utils.js | 2 ++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/test.js b/test.js index cf8456c..5449847 100644 --- a/test.js +++ b/test.js @@ -3,7 +3,7 @@ require('mocha'); const assert = require('assert'); const pretty = require('.'); -const { nano, regex } = require('./utils'); +const { bigInt, nano, regex } = require('./utils'); describe('pretty', function() { it('should throw an error when invalid args are passed:', function() { @@ -71,6 +71,25 @@ describe('pretty', function() { assert.equal(pretty(nano([0, 795946488]), 'ms', 2), '795.95ms'); assert.equal(pretty(nano([0, 795946488]), 'μs'), '795ms 946μs'); assert.equal(pretty(nano([0, 795428088]), 'μs'), '795ms 428μs'); + + assert.equal(pretty(bigInt(time), 'w'), '2w'); + assert.equal(pretty(bigInt(time), 'd'), '1w 6d'); + assert.equal(pretty(bigInt(time), 'h'), '1w 6d 22h'); + assert.equal(pretty(bigInt(time), 'm'), '1w 6d 21h 32m'); + assert.equal(pretty(bigInt(time), 's'), '1w 6d 21h 31m 49s'); + assert.equal(pretty(bigInt(time), 'ms'), '1w 6d 21h 31m 48s 795ms'); + assert.equal(pretty(bigInt(time), 'μs'), '1w 6d 21h 31m 48s 795ms 428μs'); + assert.equal(pretty(bigInt(time), 'ns'), '1w 6d 21h 31m 48s 795ms 428μs 88ns'); + assert.equal(pretty(bigInt([0, 795428088]), 'ms'), '795ms'); + assert.equal(pretty(bigInt([0, 428088]), 'ms'), '0ms'); + assert.equal(pretty(bigInt([0, 428088]), 'ns'), '428μs 88ns'); + assert.equal(pretty(bigInt([0, 528088]), 'ms'), '1ms'); + assert.equal(pretty(bigInt([0, 428088]), 'ms', 1), '0.4ms'); + assert.equal(pretty(bigInt([0, 628088]), 'ms', 1), '0.6ms'); + assert.equal(pretty(bigInt([0, 428088]), 'ms', 2), '0.43ms'); + assert.equal(pretty(bigInt([0, 795946488]), 'ms', 2), '795.95ms'); + assert.equal(pretty(bigInt([0, 795946488]), 'μs'), '795ms 946μs'); + assert.equal(pretty(bigInt([0, 795428088]), 'μs'), '795ms 428μs'); }); it('should round to the closest increment', function() { @@ -99,6 +118,19 @@ describe('pretty', function() { assert.equal(pretty(nano([0, 428088])), '428μs'); assert.equal(pretty(nano([0, 88])), '88ns'); assert.equal(pretty(nano([0, 18])), '18ns'); + + assert.equal(pretty(bigInt([1200708, 795428088])), '2w'); + assert.equal(pretty(bigInt([800708, 795428088])), '1w'); + assert.equal(pretty(bigInt([400708, 795428088])), '5d'); + assert.equal(pretty(bigInt([70708, 795428088])), '20h'); + assert.equal(pretty(bigInt([12708, 795428088])), '4h'); + assert.equal(pretty(bigInt([3708, 795428088])), '1h'); + assert.equal(pretty(bigInt([208, 795428088])), '3m'); + assert.equal(pretty(bigInt([20, 795428088])), '21s'); + assert.equal(pretty(bigInt([0, 795428088])), '795ms'); + assert.equal(pretty(bigInt([0, 428088])), '428μs'); + assert.equal(pretty(bigInt([0, 88])), '88ns'); + assert.equal(pretty(bigInt([0, 18])), '18ns'); }); it('should work when numbers are strings', function() { @@ -127,6 +159,19 @@ describe('pretty', function() { assert.equal(pretty(''+nano(['0', '000000088'])), '88ns'); assert.equal(pretty(''+nano(['0', '000000018'])), '18ns'); assert.equal(pretty(''+nano(['0', '000000001'])), '1ns'); + + assert.equal(pretty(''+bigInt(['1200708', '795428088'])), '2w'); + assert.equal(pretty(''+bigInt(['800708', '795428088'])), '1w'); + assert.equal(pretty(''+bigInt(['400708', '795428088'])), '5d'); + assert.equal(pretty(''+bigInt(['70708', '795428088'])), '20h'); + assert.equal(pretty(''+bigInt(['3708', '795428088'])), '1h'); + assert.equal(pretty(''+bigInt(['208', '795428088'])), '3m'); + assert.equal(pretty(''+bigInt(['20', '795428088'])), '21s'); + assert.equal(pretty(''+bigInt(['0', '795428088'])), '795ms'); + assert.equal(pretty(''+bigInt(['0', '000428088'])), '428μs'); + assert.equal(pretty(''+bigInt(['0', '000000088'])), '88ns'); + assert.equal(pretty(''+bigInt(['0', '000000018'])), '18ns'); + assert.equal(pretty(''+bigInt(['0', '000000001'])), '1ns'); }); it('should round the given number of digits', function() { @@ -149,6 +194,16 @@ describe('pretty', function() { assert.equal(pretty(nano([20, 795428088]), 2), '20.80s'); assert.equal(pretty(nano([0, 795428088]), 2), '795.43ms'); assert.equal(pretty(nano([0, 428088]), 2), '428.09μs'); + + assert.equal(pretty(bigInt([1200708, 795428088]), 2), '1.99w'); + assert.equal(pretty(bigInt([800708, 795428088]), 2), '1.32w'); + assert.equal(pretty(bigInt([400708, 795428088]), 2), '4.64d'); + assert.equal(pretty(bigInt([70708, 795428088]), 2), '19.64h'); + assert.equal(pretty(bigInt([3708, 795428088]), 2), '1.03h'); + assert.equal(pretty(bigInt([208, 795428088]), 2), '3.48m'); + assert.equal(pretty(bigInt([20, 795428088]), 2), '20.80s'); + assert.equal(pretty(bigInt([0, 795428088]), 2), '795.43ms'); + assert.equal(pretty(bigInt([0, 428088]), 2), '428.09μs'); }); it('should support rounding as the third arg:', function() { @@ -194,6 +249,27 @@ describe('pretty', function() { assert.equal(pretty(nano(time), 'μs', 2), '2w 20h 1m 48s 795ms 428.09μs'); assert.equal(pretty(nano(time), 'ns'), '2w 20h 1m 48s 795ms 428μs 88ns'); assert.equal(pretty(nano(time), 'ns', 2), '2w 20h 1m 48s 795ms 428μs 88.00ns'); + + assert.equal(pretty(bigInt(time), 'w'), '2w'); + assert.equal(pretty(bigInt(time), 'w', 1), '2.1w'); + assert.equal(pretty(bigInt(time), 'w', 2), '2.12w'); + assert.equal(pretty(bigInt(time), 'd'), '2w 1d'); + assert.equal(pretty(bigInt(time), 'd', 1), '2w 0.8d'); + assert.equal(pretty(bigInt(time), 'd', 2), '2w 0.83d'); + assert.equal(pretty(bigInt(time), 'h'), '2w 20h'); + assert.equal(pretty(bigInt(time), 'h', 1), '2w 20.0h'); + assert.equal(pretty(bigInt(time), 'h', 2), '2w 20.03h'); + assert.equal(pretty(bigInt(time), 'm'), '2w 20h 2m'); + assert.equal(pretty(bigInt(time), 's'), '2w 20h 1m 49s'); + assert.equal(pretty(bigInt(time), 's', 1), '2w 20h 1m 48.8s'); + assert.equal(pretty(bigInt(time), 's', 2), '2w 20h 1m 48.80s'); + assert.equal(pretty(bigInt(time), 'ms'), '2w 20h 1m 48s 795ms'); + assert.equal(pretty(bigInt(time), 'ms', 2), '2w 20h 1m 48s 795.43ms'); + assert.equal(pretty(bigInt(time), 'μs'), '2w 20h 1m 48s 795ms 428μs'); + assert.equal(pretty(bigInt(time), 'μs', 1), '2w 20h 1m 48s 795ms 428.1μs'); + assert.equal(pretty(bigInt(time), 'μs', 2), '2w 20h 1m 48s 795ms 428.09μs'); + assert.equal(pretty(bigInt(time), 'ns'), '2w 20h 1m 48s 795ms 428μs 88ns'); + assert.equal(pretty(bigInt(time), 'ns', 2), '2w 20h 1m 48s 795ms 428μs 88.00ns'); }); }); diff --git a/utils.js b/utils.js index 81c3cb1..934d4eb 100644 --- a/utils.js +++ b/utils.js @@ -1,5 +1,7 @@ exports.hrTime = (time) => [Number(BigInt(time) / BigInt(1e9)), Number(BigInt(time) % BigInt(1e9))]; +exports.bigInt = (time) => BigInt(+time[0] * 1e9) + BigInt(+time[1]); + exports.nano = time => +time[0] * 1e9 + +time[1]; exports.scale = { From 59ee8bb067948289866771789415357643b1fe01 Mon Sep 17 00:00:00 2001 From: Jakka Prihatna Date: Fri, 25 Jul 2025 09:29:35 +0700 Subject: [PATCH 4/6] test: add test for passing bigint as `n`-suffixed string `([0-9]+n)` --- test.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test.js b/test.js index 5449847..cfd78af 100644 --- a/test.js +++ b/test.js @@ -172,6 +172,32 @@ describe('pretty', function() { assert.equal(pretty(''+bigInt(['0', '000000088'])), '88ns'); assert.equal(pretty(''+bigInt(['0', '000000018'])), '18ns'); assert.equal(pretty(''+bigInt(['0', '000000001'])), '1ns'); + + assert.equal(pretty(nano(['1200708', '795428088'])+'n'), '2w'); + assert.equal(pretty(nano(['800708', '795428088'])+'n'), '1w'); + assert.equal(pretty(nano(['400708', '795428088'])+'n'), '5d'); + assert.equal(pretty(nano(['70708', '795428088'])+'n'), '20h'); + assert.equal(pretty(nano(['3708', '795428088'])+'n'), '1h'); + assert.equal(pretty(nano(['208', '795428088'])+'n'), '3m'); + assert.equal(pretty(nano(['20', '795428088'])+'n'), '21s'); + assert.equal(pretty(nano(['0', '795428088'])+'n'), '795ms'); + assert.equal(pretty(nano(['0', '000428088'])+'n'), '428μs'); + assert.equal(pretty(nano(['0', '000000088'])+'n'), '88ns'); + assert.equal(pretty(nano(['0', '000000018'])+'n'), '18ns'); + assert.equal(pretty(nano(['0', '000000001'])+'n'), '1ns'); + + assert.equal(pretty(''+bigInt(['1200708', '795428088'])+'n'), '2w'); + assert.equal(pretty(''+bigInt(['800708', '795428088'])+'n'), '1w'); + assert.equal(pretty(''+bigInt(['400708', '795428088'])+'n'), '5d'); + assert.equal(pretty(''+bigInt(['70708', '795428088'])+'n'), '20h'); + assert.equal(pretty(''+bigInt(['3708', '795428088'])+'n'), '1h'); + assert.equal(pretty(''+bigInt(['208', '795428088'])+'n'), '3m'); + assert.equal(pretty(''+bigInt(['20', '795428088'])+'n'), '21s'); + assert.equal(pretty(''+bigInt(['0', '795428088'])+'n'), '795ms'); + assert.equal(pretty(''+bigInt(['0', '000428088'])+'n'), '428μs'); + assert.equal(pretty(''+bigInt(['0', '000000088'])+'n'), '88ns'); + assert.equal(pretty(''+bigInt(['0', '000000018'])+'n'), '18ns'); + assert.equal(pretty(''+bigInt(['0', '000000001'])+'n'), '1ns'); }); it('should round the given number of digits', function() { From a854ec1da7b050e2211d467874ff30dbc545404b Mon Sep 17 00:00:00 2001 From: Jakka Prihatna Date: Fri, 25 Jul 2025 09:41:29 +0700 Subject: [PATCH 5/6] feat: support passing bigint as string --- index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index a1bd612..2eca707 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,7 @@ const utils = require('./utils'); module.exports = (time, smallest, digits) => { - const isNumber = /^[0-9]+$/.test(time); + const isNumber = /^[0-9]+n?$/.test(time); if (!isNumber && !Array.isArray(time)) { throw new TypeError('expected an array or number in nanoseconds'); } @@ -23,7 +23,11 @@ module.exports = (time, smallest, digits) => { smallest = null; } - const val = typeof time === 'bigint' ? utils.hrTime(time) : time; + const val = typeof time === 'bigint' + ? utils.hrTime(time) + : isNumber && /n$/.test(time) + ? utils.hrTime(time.slice(0, -1)) + : time; let num = Array.isArray(val) ? utils.nano(val) : val; let res = ''; From 21584cb6494607a27c81f512adf7b0abb7d761d3 Mon Sep 17 00:00:00 2001 From: Jakka Prihatna Date: Fri, 25 Jul 2025 09:55:03 +0700 Subject: [PATCH 6/6] chore: lint, minor edits, update ESLint ecmaVersion to es2020 --- .eslintrc.json | 3 +- test.js | 98 +++++++++++++++++++++++++------------------------- utils.js | 18 +++++----- 3 files changed, 60 insertions(+), 59 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 24b8984..7cda5fb 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -5,13 +5,14 @@ "env": { "browser": false, + "es2020": true, "es6": true, "node": true, "mocha": true }, "parserOptions":{ - "ecmaVersion": 9, + "ecmaVersion": 2020, "sourceType": "module", "ecmaFeatures": { "modules": true, diff --git a/test.js b/test.js index cfd78af..fe19f8c 100644 --- a/test.js +++ b/test.js @@ -11,7 +11,7 @@ describe('pretty', function() { }); it('should throw an error when invalid arrays are passed:', function() { - assert.throws(() => pretty([0,1,2]), /expected an array from process\.hrtime\(\)/); + assert.throws(() => pretty([0, 1, 2]), /expected an array from process\.hrtime\(\)/); }); it('should support hrtime:', function() { @@ -147,57 +147,57 @@ describe('pretty', function() { assert.equal(pretty(['0', '000000018']), '18ns'); assert.equal(pretty(['0', '000000001']), '1ns'); - assert.equal(pretty(''+nano(['1200708', '795428088'])), '2w'); - assert.equal(pretty(''+nano(['800708', '795428088'])), '1w'); - assert.equal(pretty(''+nano(['400708', '795428088'])), '5d'); - assert.equal(pretty(''+nano(['70708', '795428088'])), '20h'); - assert.equal(pretty(''+nano(['3708', '795428088'])), '1h'); - assert.equal(pretty(''+nano(['208', '795428088'])), '3m'); - assert.equal(pretty(''+nano(['20', '795428088'])), '21s'); - assert.equal(pretty(''+nano(['0', '795428088'])), '795ms'); - assert.equal(pretty(''+nano(['0', '000428088'])), '428μs'); - assert.equal(pretty(''+nano(['0', '000000088'])), '88ns'); - assert.equal(pretty(''+nano(['0', '000000018'])), '18ns'); - assert.equal(pretty(''+nano(['0', '000000001'])), '1ns'); + assert.equal(pretty('' + nano(['1200708', '795428088'])), '2w'); + assert.equal(pretty('' + nano(['800708', '795428088'])), '1w'); + assert.equal(pretty('' + nano(['400708', '795428088'])), '5d'); + assert.equal(pretty('' + nano(['70708', '795428088'])), '20h'); + assert.equal(pretty('' + nano(['3708', '795428088'])), '1h'); + assert.equal(pretty('' + nano(['208', '795428088'])), '3m'); + assert.equal(pretty('' + nano(['20', '795428088'])), '21s'); + assert.equal(pretty('' + nano(['0', '795428088'])), '795ms'); + assert.equal(pretty('' + nano(['0', '000428088'])), '428μs'); + assert.equal(pretty('' + nano(['0', '000000088'])), '88ns'); + assert.equal(pretty('' + nano(['0', '000000018'])), '18ns'); + assert.equal(pretty('' + nano(['0', '000000001'])), '1ns'); - assert.equal(pretty(''+bigInt(['1200708', '795428088'])), '2w'); - assert.equal(pretty(''+bigInt(['800708', '795428088'])), '1w'); - assert.equal(pretty(''+bigInt(['400708', '795428088'])), '5d'); - assert.equal(pretty(''+bigInt(['70708', '795428088'])), '20h'); - assert.equal(pretty(''+bigInt(['3708', '795428088'])), '1h'); - assert.equal(pretty(''+bigInt(['208', '795428088'])), '3m'); - assert.equal(pretty(''+bigInt(['20', '795428088'])), '21s'); - assert.equal(pretty(''+bigInt(['0', '795428088'])), '795ms'); - assert.equal(pretty(''+bigInt(['0', '000428088'])), '428μs'); - assert.equal(pretty(''+bigInt(['0', '000000088'])), '88ns'); - assert.equal(pretty(''+bigInt(['0', '000000018'])), '18ns'); - assert.equal(pretty(''+bigInt(['0', '000000001'])), '1ns'); + assert.equal(pretty('' + bigInt(['1200708', '795428088'])), '2w'); + assert.equal(pretty('' + bigInt(['800708', '795428088'])), '1w'); + assert.equal(pretty('' + bigInt(['400708', '795428088'])), '5d'); + assert.equal(pretty('' + bigInt(['70708', '795428088'])), '20h'); + assert.equal(pretty('' + bigInt(['3708', '795428088'])), '1h'); + assert.equal(pretty('' + bigInt(['208', '795428088'])), '3m'); + assert.equal(pretty('' + bigInt(['20', '795428088'])), '21s'); + assert.equal(pretty('' + bigInt(['0', '795428088'])), '795ms'); + assert.equal(pretty('' + bigInt(['0', '000428088'])), '428μs'); + assert.equal(pretty('' + bigInt(['0', '000000088'])), '88ns'); + assert.equal(pretty('' + bigInt(['0', '000000018'])), '18ns'); + assert.equal(pretty('' + bigInt(['0', '000000001'])), '1ns'); - assert.equal(pretty(nano(['1200708', '795428088'])+'n'), '2w'); - assert.equal(pretty(nano(['800708', '795428088'])+'n'), '1w'); - assert.equal(pretty(nano(['400708', '795428088'])+'n'), '5d'); - assert.equal(pretty(nano(['70708', '795428088'])+'n'), '20h'); - assert.equal(pretty(nano(['3708', '795428088'])+'n'), '1h'); - assert.equal(pretty(nano(['208', '795428088'])+'n'), '3m'); - assert.equal(pretty(nano(['20', '795428088'])+'n'), '21s'); - assert.equal(pretty(nano(['0', '795428088'])+'n'), '795ms'); - assert.equal(pretty(nano(['0', '000428088'])+'n'), '428μs'); - assert.equal(pretty(nano(['0', '000000088'])+'n'), '88ns'); - assert.equal(pretty(nano(['0', '000000018'])+'n'), '18ns'); - assert.equal(pretty(nano(['0', '000000001'])+'n'), '1ns'); + assert.equal(pretty(nano(['1200708', '795428088']) + 'n'), '2w'); + assert.equal(pretty(nano(['800708', '795428088']) + 'n'), '1w'); + assert.equal(pretty(nano(['400708', '795428088']) + 'n'), '5d'); + assert.equal(pretty(nano(['70708', '795428088']) + 'n'), '20h'); + assert.equal(pretty(nano(['3708', '795428088']) + 'n'), '1h'); + assert.equal(pretty(nano(['208', '795428088']) + 'n'), '3m'); + assert.equal(pretty(nano(['20', '795428088']) + 'n'), '21s'); + assert.equal(pretty(nano(['0', '795428088']) + 'n'), '795ms'); + assert.equal(pretty(nano(['0', '000428088']) + 'n'), '428μs'); + assert.equal(pretty(nano(['0', '000000088']) + 'n'), '88ns'); + assert.equal(pretty(nano(['0', '000000018']) + 'n'), '18ns'); + assert.equal(pretty(nano(['0', '000000001']) + 'n'), '1ns'); - assert.equal(pretty(''+bigInt(['1200708', '795428088'])+'n'), '2w'); - assert.equal(pretty(''+bigInt(['800708', '795428088'])+'n'), '1w'); - assert.equal(pretty(''+bigInt(['400708', '795428088'])+'n'), '5d'); - assert.equal(pretty(''+bigInt(['70708', '795428088'])+'n'), '20h'); - assert.equal(pretty(''+bigInt(['3708', '795428088'])+'n'), '1h'); - assert.equal(pretty(''+bigInt(['208', '795428088'])+'n'), '3m'); - assert.equal(pretty(''+bigInt(['20', '795428088'])+'n'), '21s'); - assert.equal(pretty(''+bigInt(['0', '795428088'])+'n'), '795ms'); - assert.equal(pretty(''+bigInt(['0', '000428088'])+'n'), '428μs'); - assert.equal(pretty(''+bigInt(['0', '000000088'])+'n'), '88ns'); - assert.equal(pretty(''+bigInt(['0', '000000018'])+'n'), '18ns'); - assert.equal(pretty(''+bigInt(['0', '000000001'])+'n'), '1ns'); + assert.equal(pretty(bigInt(['1200708', '795428088']) + 'n'), '2w'); + assert.equal(pretty(bigInt(['800708', '795428088']) + 'n'), '1w'); + assert.equal(pretty(bigInt(['400708', '795428088']) + 'n'), '5d'); + assert.equal(pretty(bigInt(['70708', '795428088']) + 'n'), '20h'); + assert.equal(pretty(bigInt(['3708', '795428088']) + 'n'), '1h'); + assert.equal(pretty(bigInt(['208', '795428088']) + 'n'), '3m'); + assert.equal(pretty(bigInt(['20', '795428088']) + 'n'), '21s'); + assert.equal(pretty(bigInt(['0', '795428088']) + 'n'), '795ms'); + assert.equal(pretty(bigInt(['0', '000428088']) + 'n'), '428μs'); + assert.equal(pretty(bigInt(['0', '000000088']) + 'n'), '88ns'); + assert.equal(pretty(bigInt(['0', '000000018']) + 'n'), '18ns'); + assert.equal(pretty(bigInt(['0', '000000001']) + 'n'), '1ns'); }); it('should round the given number of digits', function() { diff --git a/utils.js b/utils.js index 934d4eb..8e9ac1e 100644 --- a/utils.js +++ b/utils.js @@ -12,18 +12,18 @@ exports.scale = { 's': 1e9, 'ms': 1e6, 'μs': 1e3, - 'ns': 1, + 'ns': 1 }; exports.regex = { - 'w': /^(w((ee)?k)?s?)$/, - 'd': /^(d(ay)?s?)$/, - 'h': /^(h((ou)?r)?s?)$/, - 'm': /^(min(ute)?s?|m)$/, - 's': /^((sec(ond)?)s?|s)$/, - 'ms': /^(milli(second)?s?|ms)$/, - 'μs': /^(micro(second)?s?|μs)$/, - 'ns': /^(nano(second)?s?|ns?)$/, + 'w': /^(w((ee)?k)?s?)$/, + 'd': /^(d(ay)?s?)$/, + 'h': /^(h((ou)?r)?s?)$/, + 'm': /^(min(ute)?s?|m)$/, + 's': /^((sec(ond)?)s?|s)$/, + 'ms': /^(milli(second)?s?|ms)$/, + 'μs': /^(micro(second)?s?|μs)$/, + 'ns': /^(nano(second)?s?|ns?)$/ }; exports.isSmallest = function(uom, unit) {