ChristopherJKoen's picture
Initial React Transcription
1643ce8
raw
history blame contribute delete
368 Bytes
export function formatBytes(bytes: number): string {
if (!Number.isFinite(bytes)) return "0 B";
const units = ["B", "KB", "MB", "GB"];
let value = bytes;
let idx = 0;
while (value >= 1024 && idx < units.length - 1) {
value /= 1024;
idx += 1;
}
const digits = value >= 10 || idx === 0 ? 0 : 1;
return `${value.toFixed(digits)} ${units[idx]}`;
}