Skip to content

Commit e51123c

Browse files
committed
time_utils: fix formatDuration(..,true) with >2 fields, and add 'limit' option to limit the length of what is displayed
1 parent f511895 commit e51123c

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

modules/time_utils.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,19 @@ exports.formatTime = (value) => {
5959
/**
6060
* @param {object|int} value {d, h, m, s} object or milliseconds
6161
* @param {boolean} compact `true` to remove all whitespaces between the values
62+
* @param {int} limit the max number of values to display (eg default might be "3d 1h 10m 45s" but 2 => "3d 1h"
6263
* @returns an human-readable duration string like "3d 1h 10m 45s" (or "3d1h10m45s" if `compact` is `true`)
6364
*/
64-
exports.formatDuration = (value, compact) => {
65-
compact = compact || false;
65+
exports.formatDuration = (value, compact, limit) => {
66+
compact = !!compact;
67+
limit = limit||4;
6668
var duration = "";
6769
var time = safeTime(typeof value === "object" ? value : exports.decodeTime(value));
68-
if (time.d > 0) duration += time.d + "d ";
69-
if (time.h > 0) duration += time.h + "h ";
70-
if (time.m > 0) duration += time.m + "m ";
71-
if (time.s > 0) duration += time.s + "s"
72-
duration = duration.trim()
73-
return compact ? duration.replace(" ", "") : duration;
70+
if (time.d > 0 && limit-->0) duration += time.d + "d ";
71+
if (time.h > 0 && limit-->0) duration += time.h + "h ";
72+
if (time.m > 0 && limit-->0) duration += time.m + "m ";
73+
if (time.s > 0 && limit-->0) duration += time.s + "s";
74+
return compact ? duration.replaceAll(" ", "") : duration.trim();
7475
}
7576

7677
exports.getCurrentTimeMillis = () => {

0 commit comments

Comments
 (0)