Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -23,7 +23,13 @@ module.exports = (time, smallest, digits) => {
smallest = null;
}

let num = isNumber ? time : utils.nano(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 = '';
let prev;

Expand Down
136 changes: 122 additions & 14 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
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() {
assert.throws(() => pretty(), /expected an array or number in nanoseconds/);
});

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() {
Expand All @@ -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');
Expand Down Expand Up @@ -65,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() {
Expand Down Expand Up @@ -93,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() {
Expand All @@ -109,18 +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(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() {
Expand All @@ -143,6 +220,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() {
Expand Down Expand Up @@ -188,6 +275,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');
});
});

Expand Down
22 changes: 13 additions & 9 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +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 = {
Expand All @@ -8,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) {
Expand Down