Several improvements

This commit is contained in:
2026-08-03 16:04:17 -05:00
parent f94b08a4e6
commit a713af2fee
7 changed files with 102 additions and 31 deletions
+24
View File
@@ -0,0 +1,24 @@
(function exposeDurationFormatter(root, factory) {
const formatter = factory();
if (typeof module === "object" && module.exports) {
module.exports = formatter;
} else {
root.NCCFormatDuration = formatter;
}
}(globalThis, () => function formatDuration(value) {
if (typeof value !== "string") return value;
const match = value.match(/^P(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/);
if (!match || !match.slice(1).some((part) => part !== undefined)) return value;
const units = [
[match[1], "day"],
[match[2], "hr"],
[match[3], "min"],
[match[4], "sec"]
].filter(([amount]) => Number(amount) > 0);
if (!units.length) return "0 min";
return units.map(([amount, unit]) => (
`${Number(amount)} ${unit}${unit === "day" && Number(amount) !== 1 ? "s" : ""}`
)).join(" ");
}));