| 1 | export function formatBytes(value: number): string { |
| 2 | if (value < 1_024) return `${Math.round(value)} B`; |
| 3 | const units = ["KB", "MB", "GB", "TB"] as const; |
| 4 | let next = value; |
| 5 | let unitIndex = -1; |
| 6 | do { |
| 7 | next /= 1_024; |
| 8 | unitIndex += 1; |
| 9 | } while (next >= 1_024 && unitIndex < units.length - 1); |
| 10 | return `${next.toFixed(next >= 100 ? 0 : next >= 10 ? 1 : 2)} ${units[unitIndex]}`; |
| 11 | } |