[time_utils] Add compact mode to formatDuration

pull/1899/head
Alessandro Cocco 2022-05-28 21:30:50 +02:00
parent dc0157cf1f
commit 889de43d26
1 changed files with 6 additions and 3 deletions

View File

@ -63,16 +63,19 @@ exports.formatTime = (value) => {
/**
* @param {object|int} value {d, h, m, s} object or milliseconds
* @returns an human-readable duration string like "3d 1h 10m 45s"
* @param {boolean} compact `true` to remove all whitespaces between the values
* @returns an human-readable duration string like "3d 1h 10m 45s" (or "3d1h10m45s" if `compact` is `true`)
*/
exports.formatDuration = (value) => {
exports.formatDuration = (value, compact) => {
compact = compact || false;
var duration = "";
var time = safeTime(typeof value === "object" ? value : exports.decodeTime(value));
if (time.d > 0) duration += time.d + "d ";
if (time.h > 0) duration += time.h + "h ";
if (time.m > 0) duration += time.m + "m ";
if (time.s > 0) duration += time.s + "s"
return duration.trim();
duration = duration.trim()
return compact ? duration.replace(" ", "") : duration;
}
exports.getCurrentTimeMillis = () => {