| 1 | export function formatBytes(n: number): string { |
| 2 | if (!Number.isFinite(n) || n <= 0) return ""; |
| 3 | const units = ["B", "KiB", "MiB", "GiB"]; |
| 4 | let value = n; |
| 5 | let unit = 0; |
| 6 | while (value >= 1024 && unit < units.length - 1) { |
| 7 | value /= 1024; |
| 8 | unit += 1; |
| 9 | } |
| 10 | return `${value >= 100 ? Math.round(value) : value.toFixed(1)} ${units[unit]}`; |
| 11 | } |
| 12 | |
| 13 | export function parentOf(path: string): string { |
| 14 | const trimmed = path.replace(/\/+$/, ""); |
| 15 | const idx = trimmed.lastIndexOf("/"); |
| 16 | if (idx <= 0) return "/"; |
| 17 | return trimmed.slice(0, idx); |
| 18 | } |
| 19 |