AI-native design editor. Open-source Figma alternative.
/** Human-readable byte size (e.g. 1.2 MB). */
export function formatStorageBytes(bytes: number): string {
if (!Number.isFinite(bytes) || bytes < 0) return '0 B'
if (bytes < 1024) return `${Math.round(bytes)} B`
const units = ['KB', 'MB', 'GB', 'TB'] as const
let value = bytes / 1024
let unitIndex = 0
while (value >= 1024 && unitIndex < units.length - 1) {
value /= 1024
unitIndex += 1
}
const digits = value >= 10 || unitIndex === 0 ? 0 : 1
return `${value.toFixed(digits)} ${units[unitIndex]}`
}