openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
83 lines (82 loc) • 2.17 kB
JavaScript
import { t as expectDefined } from "./expect-DDakjvvQ.mjs";
//#region packages/normalization-core/src/format.ts
const BYTE_SIZE_UNITS = [
"byte",
"kilo",
"mega",
"giga",
"tera"
];
const BYTE_SIZE_STYLES = {
iec: {
base: 1024,
labels: [
"B",
"KiB",
"MiB",
"GiB",
"TiB"
]
},
"legacy-binary": {
base: 1024,
labels: [
"B",
"KB",
"MB",
"GB",
"TB"
]
}
};
/** Buckets an absolute duration while preserving nested display rounding at unit boundaries. */
function bucketRelativeTimeMs(e) {
const t = Math.round(e / 1e3);
if (t < 60) return {
value: t,
unit: "second"
};
const n = Math.round(t / 60);
if (n < 60) return {
value: n,
unit: "minute"
};
const r = Math.round(n / 60);
return r < 48 ? {
value: r,
unit: "hour"
} : {
value: Math.round(r / 24),
unit: "day"
};
}
/** Formats a byte count with caller-explicit scale, labels, precision, and unit cap. */
function formatByteSize(e, t) {
const { base: n, labels: r } = BYTE_SIZE_STYLES[t.style];
const i = BYTE_SIZE_UNITS.indexOf(t.maxUnit);
let a = 0;
let o = e;
while (o >= n && a < i) {
o /= n;
a += 1;
}
const s = expectDefined(BYTE_SIZE_UNITS[a], "byte-size unit");
const c = expectDefined(r[a], "byte-size label");
const l = typeof t.fractionDigits === "function" ? t.fractionDigits(o, s) : t.fractionDigits;
if (l === null) return `${o}${t.separator}${c}`;
if (t.floorUnits?.includes(s)) o = Math.floor(o * 10 ** l) / 10 ** l;
return `${o.toFixed(l)}${t.separator}${c}`;
}
/** Formats token units while callers retain input validation and whole-token rounding. */
function formatCompactTokenCount(e, t = {}) {
if (e < 1e3) return String(Math.round(e));
const trim = (e) => t.trimTrailingZero ? e.replace(/\.0$/, "") : e;
if (t.maxUnit === "billion" && e >= 1e9) return `${trim((e / 1e9).toFixed(1))}B`;
if (e < 1e6) {
const n = (e / 1e3).toFixed(t.thousandsPrecision ?? 1);
if (Number(n) < 1e3) return `${trim(n)}${t.thousandsSuffix ?? "k"}`;
}
return `${trim((e / 1e6).toFixed(1))}${t.millionsSuffix ?? "m"}`;
}
//#endregion
export { formatByteSize as n, formatCompactTokenCount as r, bucketRelativeTimeMs as t };