const BYTE_UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'] as const export function formatBytes(bytes: number): string { if (!Number.isFinite(bytes) || bytes <= 0) { return '0 B' } let value = bytes let unitIndex = 0 while (value >= 1024 && unitIndex < BYTE_UNITS.length - 1) { value /= 1024 unitIndex += 1 } const precision = value >= 100 || unitIndex === 0 ? 0 : value >= 10 ? 1 : 2 return `${value.toFixed(precision)} ${BYTE_UNITS[unitIndex]}` }