40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
const UNITS = ["o", "Ko", "Mo", "Go", "To", "Po"];
|
|
|
|
export function fmtSize(n: number | null | undefined): string {
|
|
if (n == null || !isFinite(n)) return "-";
|
|
let i = 0;
|
|
let v = n;
|
|
while (v >= 1024 && i < UNITS.length - 1) {
|
|
v /= 1024;
|
|
i++;
|
|
}
|
|
return `${v >= 100 || i === 0 ? Math.round(v) : v.toFixed(1).replace(".", ",")} ${UNITS[i]}`;
|
|
}
|
|
|
|
export const fmtNum = (n: number | null | undefined) => (n == null ? "-" : n.toLocaleString("fr-FR"));
|
|
|
|
export function fmtDate(s?: string | null): string {
|
|
if (!s) return "-";
|
|
const d = new Date(s);
|
|
return isNaN(d.getTime()) ? s : d.toLocaleString("fr-FR", { dateStyle: "medium", timeStyle: "short" });
|
|
}
|
|
|
|
export function fmtAgo(secs?: number | null): string {
|
|
if (secs == null) return "-";
|
|
if (secs < 60) return `il y a ${secs} s`;
|
|
if (secs < 3600) return `il y a ${Math.round(secs / 60)} min`;
|
|
if (secs < 86400) return `il y a ${Math.round(secs / 3600)} h`;
|
|
return `il y a ${Math.round(secs / 86400)} j`;
|
|
}
|
|
|
|
export const GIB = 1024 ** 3;
|
|
|
|
/** Découpe une ligne de commande en arguments (guillemets simples et doubles pris en charge). */
|
|
export function splitArgs(line: string): string[] {
|
|
const out: string[] = [];
|
|
const re = /"([^"]*)"|'([^']*)'|(\S+)/g;
|
|
let m: RegExpExecArray | null;
|
|
while ((m = re.exec(line))) out.push(m[1] ?? m[2] ?? m[3]);
|
|
return out;
|
|
}
|