Skip to content

Commit

Permalink
Account for the 0 case in format-duration
Browse files Browse the repository at this point in the history
  • Loading branch information
DingoEatingFuzz committed May 30, 2018
1 parent 9aabe13 commit 83e0b10
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
21 changes: 19 additions & 2 deletions ui/app/utils/format-duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const allUnits = [
export default function formatDuration(duration = 0, units = 'ns') {
const durationParts = {};

// Moment only handles up to millisecond precision.
// Microseconds and nanoseconds need to be handled first,
// then Moment can take over for all larger units.
if (units === 'ns') {
durationParts.nanoseconds = duration % 1000;
durationParts.microseconds = Math.floor((duration % 1000000) / 1000);
Expand All @@ -24,15 +27,22 @@ export default function formatDuration(duration = 0, units = 'ns') {
duration = Math.floor(duration / 1000);
}

const momentDuration = moment.duration(duration, ['ns', 'mms'].includes(units) ? 'ms' : units);
let momentUnits = units;
if (units === 'ns' || units === 'mms') {
momentUnits = 'ms';
}
const momentDuration = moment.duration(duration, momentUnits);

// Get the count of each time unit that Moment handles
allUnits
.filterBy('inMoment')
.mapBy('name')
.forEach(unit => {
durationParts[unit] = momentDuration[unit]();
});

// Format each time time bucket as a string
// e.g., { years: 5, seconds: 30 } -> [ '5 years', '30s' ]
const displayParts = allUnits.reduce((parts, unitType) => {
if (durationParts[unitType.name]) {
const count = durationParts[unitType.name];
Expand All @@ -43,5 +53,12 @@ export default function formatDuration(duration = 0, units = 'ns') {
return parts;
}, []);

return displayParts.join(' ');
if (displayParts.length) {
return displayParts.join(' ');
}

// When the duration is 0, show 0 in terms of `units`
const unitTypeForUnits = allUnits.findBy('suffix', units);
const suffix = unitTypeForUnits.pluralizable ? units.pluralize() : units;
return `0${unitTypeForUnits.pluralizable ? ' ' : ''}${suffix}`;
}
5 changes: 5 additions & 0 deletions ui/tests/unit/utils/format-duration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ test('The units option allows for units coarser than nanoseconds', function(asse
assert.equal(formatDuration(1200, 's'), expectation2, expectation2);
assert.equal(formatDuration(32, 'd'), expectation3, expectation3);
});

test('When duration is 0, 0 is shown in terms of the units provided to the function', function(assert) {
assert.equal(formatDuration(0), '0ns', 'formatDuration(0) -> 0ns');
assert.equal(formatDuration(0, 'year'), '0 years', 'formatDuration(0, "year") -> 0 years');
});

0 comments on commit 83e0b10

Please sign in to comment.