Skip to content

Commit

Permalink
fixup: reformat for easier comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
BridgeAR committed Sep 23, 2019
1 parent 1dcb3d1 commit 1c5e1b1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
53 changes: 23 additions & 30 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,45 +533,38 @@ function timeLogImpl(self, name, label, data) {
return true;
}

function pad(value) {
return `${value}`.padStart(2, '0');
}

function formatTime(ms) {
let end = 3;
const values = [];
let hours = 0;
let minutes = 0;
let seconds = 0;

if (ms >= kHour) {
end = -1;
values.push(`${Math.floor(ms / kHour)}h`);
ms = Math.floor(ms % kHour);
}
if (ms >= kMinute) {
if (end === 3) {
end = 0;
}
values.push(`${Math.floor(ms / kMinute)}min`);
ms = ms % kMinute;
}
if (ms >= kSecond) {
if (end === -1) {
values.push(`${String(Number((ms / kSecond).toFixed(1)))}s`);
} else {
values.push(`${Math.floor(ms / kSecond)}s`);
if (end === 3) {
end = 1;
if (ms >= kMinute) {
if (ms >= kHour) {
hours = Math.floor(ms / kHour);
ms = ms % kHour;
}
minutes = Math.floor(ms / kMinute);
ms = ms % kMinute;
}
ms = ms % kSecond;
}
if (end !== -1) {
values.push(`${String(Number(ms.toFixed(end)))}ms`);
seconds = ms / kSecond;
}

let res = values.pop();
if (values.length) {
res = `${values.pop()} and ${res}`;
if (hours !== 0 || minutes !== 0) {
[seconds, ms] = seconds.toFixed(3).split('.');
const res = hours !== 0 ? `${hours}:${pad(minutes)}` : minutes;
return `${res}:${pad(seconds)}.${ms} (${hours !== 0 ? 'h:m' : ''}m:ss.mmm)`;
}
while (values.length !== 0) {
res = `${values.pop()}, ${res}`;

if (seconds !== 0) {
return `${seconds.toFixed(3)}s`;
}
return res;

return `${Number(ms.toFixed(3))}ms`;
}

const keyKey = 'Key';
Expand Down
11 changes: 6 additions & 5 deletions test/parallel/test-console-formatTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ const assert = require('assert');

assert.strictEqual(formatTime(100.0096), '100.01ms');
assert.strictEqual(formatTime(100.0115), '100.011ms');
assert.strictEqual(formatTime(1500.04), '1s and 500ms');
assert.strictEqual(formatTime(1500.056), '1s and 500.1ms');
assert.strictEqual(formatTime(60300.3), '1min and 300ms');
assert.strictEqual(formatTime(4000457.4), '1h, 6min and 40.5s');
assert.strictEqual(formatTime(3601017.4), '1h and 1s');
assert.strictEqual(formatTime(1500.04), '1.500s');
assert.strictEqual(formatTime(1000.056), '1.000s');
assert.strictEqual(formatTime(60300.3), '1:00.300 (m:ss.mmm)');
assert.strictEqual(formatTime(4000457.4), '1:06:40.457 (h:mm:ss.mmm)');
assert.strictEqual(formatTime(3601310.4), '1:00:01.310 (h:mm:ss.mmm)');
assert.strictEqual(formatTime(3213601017.6), '892:40:01.018 (h:mm:ss.mmm)');

0 comments on commit 1c5e1b1

Please sign in to comment.