openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
98 lines (97 loc) • 3.05 kB
JavaScript
import { t as durationUnitMs } from "./duration-units-DX2PgIpo.mjs";
//#region src/infra/format-time/format-duration-internal.ts
function resolveDurationParts(e, t, n = false) {
const r = BigInt(Math.trunc(e / durationUnitMs.day));
const i = [
{
value: n ? r / 365n : 0n,
unit: "year"
},
{
value: n ? r % 365n : r,
unit: "day"
},
{
value: Math.trunc(e / durationUnitMs.hour % 24),
unit: "hour"
},
{
value: Math.trunc(e / durationUnitMs.minute % 60),
unit: "minute"
},
{
value: Math.trunc(e / durationUnitMs.second % 60),
unit: "second"
},
{
value: e < 1e3 ? Math.trunc(e) : 0,
unit: "millisecond"
}
].filter(({ value: e }) => e !== 0 && e !== 0n).slice(0, t);
return i.length ? i : [{
value: 0,
unit: "millisecond"
}];
}
function formatDurationParts(e) {
return e.map(({ value: e, unit: t }) => `${e}${t === "millisecond" ? "ms" : t[0]}`).join(" ");
}
function resolveCompactDurationParts(e, t = false) {
if (e == null || !Number.isFinite(e) || e <= 0) return;
const n = Math.round(e);
return resolveDurationParts(n < 1e3 ? n : Math.round(e / 1e3) * 1e3, 2, t);
}
function resolveSingleUnitDurationParts(e) {
let t = durationUnitMs.millisecond;
for (const n of [
"second",
"minute",
"hour",
"day"
]) {
const r = durationUnitMs[n];
if (Math.round(e / t) * t < r) break;
t = r;
}
return resolveDurationParts(Math.round(e / t) * t, 1);
}
//#endregion
//#region src/infra/format-time/format-duration.ts
function formatDurationSeconds(e, t = {}) {
if (!Number.isFinite(e)) return "unknown";
const n = t.decimals ?? 1;
const r = t.unit ?? "s";
const i = (Math.max(0, e) / 1e3).toFixed(Math.max(0, n)).replace(/\.0+$/, "").replace(/(\.\d*[1-9])0+$/, "$1");
return r === "seconds" ? `${i} seconds` : `${i}s`;
}
/** Precise decimal-seconds output: "500ms" or "1.23s". Input is milliseconds. */
function formatDurationPrecise(e, t = {}) {
if (!Number.isFinite(e)) return "unknown";
const n = Math.max(0, Math.round(e));
if (n < 1e3) return `${n}ms`;
return formatDurationSeconds(e, {
decimals: t.decimals ?? 2,
unit: t.unit ?? "s"
});
}
/**
* Compact compound duration: "500ms", "45s", "2m5s", "1h30m".
* With `spaced`: "45s", "2m 5s", "1h 30m".
* Omits trailing zero components: "1m" not "1m 0s", "2h" not "2h 0m".
* Returns undefined for null/undefined/non-finite/non-positive input.
*/
function formatDurationCompact(e, t) {
const n = resolveCompactDurationParts(e, t?.showYears);
const r = n && formatDurationParts(n);
return t?.spaced ? r : r?.replaceAll(" ", "");
}
/**
* Rounded single-unit duration for display: "500ms", "5s", "3m", "2h", "5d".
* Returns fallback string for null/undefined/non-finite input.
*/
function formatDurationHuman(e, t = "n/a") {
if (e == null || !Number.isFinite(e) || e < 0) return t;
return formatDurationParts(resolveSingleUnitDurationParts(e));
}
//#endregion
export { formatDurationParts as a, formatDurationSeconds as i, formatDurationHuman as n, formatDurationPrecise as r, formatDurationCompact as t };