mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
fix format duration rounding days error (#582)
* fix format duration rounding days error * added tests Signed-off-by: Sebastian Malton <smalton@mirantis.com> Co-authored-by: Sebastian Malton <smalton@mirantis.com>
This commit is contained in:
1 parent
55687b7d35
commit
4bd29a6d88
6 files changed
+91
-22
No files matched your search
@@ -1,24 +1,35 @@
|
||||
// Formatting date duration in shorten format, e.g. "2d", or "25m"
|
||||
|
||||
import moment from "moment";
|
||||
|
||||
const suffixes = ["w", "d", "h", "m", "s"];
|
||||
|
||||
/**
|
||||
* This function formats durations in a more human readable form.
|
||||
* @param timeValue the duration in milliseconds to format
|
||||
* @param compact when true, only the largest non-zero time frame will be returned
|
||||
*/
|
||||
export function formatDuration(timeValue: number, compact: boolean) {
|
||||
let result = "";
|
||||
const duration = moment.duration(timeValue);
|
||||
const suffixes = ["d", "h", "m"];
|
||||
const duration = moment.duration(timeValue, "milliseconds");
|
||||
const durationValues = [
|
||||
Math.round(duration.asDays()),
|
||||
Math.floor(duration.asWeeks()),
|
||||
Math.floor(duration.asDays()) % 7,
|
||||
duration.hours(),
|
||||
duration.minutes(),
|
||||
duration.seconds(),
|
||||
];
|
||||
durationValues.forEach((value, index) => {
|
||||
if (value) result += value + suffixes[index] + " ";
|
||||
});
|
||||
|
||||
const meaningfulValues = durationValues
|
||||
.map((a, i): [number, string] => [a, suffixes[i]])
|
||||
.filter(([dur, _suf]) => dur > 0)
|
||||
.filter(([_dur, suf], i) => i === 0 || suf !== "s") // remove seconds, unless it is the only one
|
||||
.map(([dur, suf]) => dur + suf);
|
||||
|
||||
if (meaningfulValues.length === 0) {
|
||||
return "0s";
|
||||
}
|
||||
|
||||
if (compact) {
|
||||
result = result.split(" ")[0];
|
||||
return meaningfulValues[0];
|
||||
}
|
||||
if (!result) {
|
||||
return "<1m";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return meaningfulValues.join(" ");
|
||||
}
|
||||
Reference in new issue
Block a user