2018-05-30 08:03:32 +00:00
|
|
|
import moment from 'moment';
|
2019-03-26 04:24:11 +00:00
|
|
|
import { pluralize } from 'ember-inflector';
|
2018-05-30 08:03:32 +00:00
|
|
|
|
2018-10-17 15:10:16 +00:00
|
|
|
/**
|
|
|
|
* Metadata for all unit types
|
|
|
|
* name: identifier for the unit. Also maps to moment methods when applicable
|
|
|
|
* suffix: the preferred suffix for a unit
|
|
|
|
* inMoment: whether or not moment can be used to compute this unit value
|
|
|
|
* pluralizable: whether or not this suffix can be pluralized
|
|
|
|
* longSuffix: the suffix to use instead of suffix when longForm is true
|
|
|
|
*/
|
2018-05-30 08:03:32 +00:00
|
|
|
const allUnits = [
|
|
|
|
{ name: 'years', suffix: 'year', inMoment: true, pluralizable: true },
|
|
|
|
{ name: 'months', suffix: 'month', inMoment: true, pluralizable: true },
|
|
|
|
{ name: 'days', suffix: 'day', inMoment: true, pluralizable: true },
|
2021-12-28 16:08:12 +00:00
|
|
|
{
|
|
|
|
name: 'hours',
|
|
|
|
suffix: 'h',
|
|
|
|
longSuffix: 'hour',
|
|
|
|
inMoment: true,
|
|
|
|
pluralizable: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'minutes',
|
|
|
|
suffix: 'm',
|
|
|
|
longSuffix: 'minute',
|
|
|
|
inMoment: true,
|
|
|
|
pluralizable: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'seconds',
|
|
|
|
suffix: 's',
|
|
|
|
longSuffix: 'second',
|
|
|
|
inMoment: true,
|
|
|
|
pluralizable: false,
|
|
|
|
},
|
2018-05-30 08:03:32 +00:00
|
|
|
{ name: 'milliseconds', suffix: 'ms', inMoment: true, pluralizable: false },
|
|
|
|
{ name: 'microseconds', suffix: 'µs', inMoment: false, pluralizable: false },
|
|
|
|
{ name: 'nanoseconds', suffix: 'ns', inMoment: false, pluralizable: false },
|
|
|
|
];
|
|
|
|
|
2018-09-25 03:01:42 +00:00
|
|
|
const pluralizeUnits = (amount, unit, longForm) => {
|
|
|
|
let suffix;
|
2018-10-17 15:10:16 +00:00
|
|
|
|
2018-09-25 03:01:42 +00:00
|
|
|
if (longForm && unit.longSuffix) {
|
2018-10-17 15:10:16 +00:00
|
|
|
// Long form means always using full words (seconds insteand of s) which means
|
|
|
|
// pluralization is necessary.
|
2019-03-26 04:24:11 +00:00
|
|
|
suffix = amount === 1 ? unit.longSuffix : pluralize(unit.longSuffix);
|
2018-09-25 03:01:42 +00:00
|
|
|
} else {
|
2018-10-17 15:10:16 +00:00
|
|
|
// In the normal case, only pluralize based on the pluralizable flag
|
2021-12-28 16:08:12 +00:00
|
|
|
suffix =
|
|
|
|
amount === 1 || !unit.pluralizable ? unit.suffix : pluralize(unit.suffix);
|
2018-09-25 03:01:42 +00:00
|
|
|
}
|
2018-10-17 15:10:16 +00:00
|
|
|
|
|
|
|
// A space should go between the value and the unit when the unit is a full word
|
|
|
|
// 300ns vs. 1 hour
|
2018-09-25 03:01:42 +00:00
|
|
|
const addSpace = unit.pluralizable || (longForm && unit.longSuffix);
|
|
|
|
return `${amount}${addSpace ? ' ' : ''}${suffix}`;
|
|
|
|
};
|
|
|
|
|
2018-10-17 15:10:16 +00:00
|
|
|
/**
|
|
|
|
* Format a Duration at a preferred precision
|
|
|
|
*
|
|
|
|
* @param {Number} duration The duration to format
|
|
|
|
* @param {String} units The units for the duration. Default to nanoseconds.
|
|
|
|
* @param {Boolean} longForm Whether or not to expand single character suffixes,
|
|
|
|
* used to ensure screen readers correctly read units.
|
|
|
|
*/
|
2021-12-28 16:08:12 +00:00
|
|
|
export default function formatDuration(
|
|
|
|
duration = 0,
|
|
|
|
units = 'ns',
|
|
|
|
longForm = false
|
|
|
|
) {
|
2018-05-30 08:03:32 +00:00
|
|
|
const durationParts = {};
|
|
|
|
|
2018-05-30 21:30:45 +00:00
|
|
|
// Moment only handles up to millisecond precision.
|
|
|
|
// Microseconds and nanoseconds need to be handled first,
|
|
|
|
// then Moment can take over for all larger units.
|
2018-05-30 08:03:32 +00:00
|
|
|
if (units === 'ns') {
|
|
|
|
durationParts.nanoseconds = duration % 1000;
|
|
|
|
durationParts.microseconds = Math.floor((duration % 1000000) / 1000);
|
|
|
|
duration = Math.floor(duration / 1000000);
|
|
|
|
} else if (units === 'mms') {
|
|
|
|
durationParts.microseconds = duration % 1000;
|
|
|
|
duration = Math.floor(duration / 1000);
|
|
|
|
}
|
|
|
|
|
2018-05-30 21:30:45 +00:00
|
|
|
let momentUnits = units;
|
|
|
|
if (units === 'ns' || units === 'mms') {
|
|
|
|
momentUnits = 'ms';
|
|
|
|
}
|
|
|
|
const momentDuration = moment.duration(duration, momentUnits);
|
2018-05-30 08:03:32 +00:00
|
|
|
|
2018-05-30 21:30:45 +00:00
|
|
|
// Get the count of each time unit that Moment handles
|
2018-05-30 08:03:32 +00:00
|
|
|
allUnits
|
|
|
|
.filterBy('inMoment')
|
|
|
|
.mapBy('name')
|
2021-12-28 14:45:20 +00:00
|
|
|
.forEach((unit) => {
|
2018-05-30 08:03:32 +00:00
|
|
|
durationParts[unit] = momentDuration[unit]();
|
|
|
|
});
|
|
|
|
|
2018-05-30 21:30:45 +00:00
|
|
|
// Format each time time bucket as a string
|
|
|
|
// e.g., { years: 5, seconds: 30 } -> [ '5 years', '30s' ]
|
2018-05-30 08:03:32 +00:00
|
|
|
const displayParts = allUnits.reduce((parts, unitType) => {
|
|
|
|
if (durationParts[unitType.name]) {
|
|
|
|
const count = durationParts[unitType.name];
|
2018-09-25 03:01:42 +00:00
|
|
|
parts.push(pluralizeUnits(count, unitType, longForm));
|
2018-05-30 08:03:32 +00:00
|
|
|
}
|
|
|
|
return parts;
|
|
|
|
}, []);
|
|
|
|
|
2018-05-30 21:30:45 +00:00
|
|
|
if (displayParts.length) {
|
|
|
|
return displayParts.join(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the duration is 0, show 0 in terms of `units`
|
2018-09-25 03:01:42 +00:00
|
|
|
return pluralizeUnits(0, allUnits.findBy('suffix', units), longForm);
|
2018-05-30 08:03:32 +00:00
|
|
|
}
|