16 lines
No EOL
522 B
JavaScript
16 lines
No EOL
522 B
JavaScript
const formatUnit = (value, unit) => {
|
|
return value > 0 ? `${value} ${unit}${value !== 1 ? 's' : ''}` : '';
|
|
};
|
|
|
|
module.exports = ms => {
|
|
const hours = Math.floor(ms / (1000 * 60 * 60));
|
|
const minutes = Math.floor((ms / (1000 * 60)) % 60);
|
|
const seconds = Math.floor((ms / 1000) % 60);
|
|
|
|
const result = [];
|
|
if (hours) result.push(formatUnit(hours, 'hour'));
|
|
if (minutes) result.push(formatUnit(minutes, 'minute'));
|
|
if (seconds) result.push(formatUnit(seconds, 'second'));
|
|
|
|
return result.join(', ') || '0 seconds';
|
|
}; |