Download src/utils/format.ts from algorembrant/computer-auditor: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/algorembrant/computer-auditor/resolve/main/src/utils/format.ts
- Command line
-
hf download hf://algorembrant/computer-auditor/src/utils/format.ts
-
curl -L -o format.ts https://huggingface.co/algorembrant/computer-auditor/resolve/main/src/utils/format.ts
1.06 kB
| export function formatBytes(n: number): string { | |
| if (!Number.isFinite(n) || n < 0) return '0 B' | |
| const u = ['B', 'KB', 'MB', 'GB', 'TB'] | |
| let i = 0 | |
| let v = n | |
| while (v >= 1024 && i < u.length - 1) { | |
| v /= 1024 | |
| i++ | |
| } | |
| return `${v < 10 && i > 0 ? v.toFixed(1) : Math.round(v)} ${u[i]}` | |
| } | |
| /** Exact size as decimal kilobytes (1 KB = 1024 bytes). Use + suffix when walk was capped. */ | |
| export function formatSizeKb(bytes: number, opts?: { truncated?: boolean }): string { | |
| if (!Number.isFinite(bytes) || bytes < 0) bytes = 0 | |
| const kb = bytes / 1024 | |
| const s = kb.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 }) | |
| return opts?.truncated ? `${s}+ KB` : `${s} KB` | |
| } | |
| export function formatUptime(sec: number): string { | |
| const d = Math.floor(sec / 86400) | |
| const h = Math.floor((sec % 86400) / 3600) | |
| const m = Math.floor((sec % 3600) / 60) | |
| const parts: string[] = [] | |
| if (d) parts.push(`${d}d`) | |
| if (h || d) parts.push(`${h}h`) | |
| parts.push(`${m}m`) | |
| return parts.join(' ') | |
| } | |