Files
donutbrowser/src/lib/format-bytes.ts
T
2026-09-09 10:09:14 +04:00

15 lines
546 B
TypeScript

/**
* Bytes as a person reads them.
*
* The unit symbols are the same in every locale we ship, so this stays out of
* the translation files; only the number changes.
*/
export function formatBytes(bytes: number): string {
if (!Number.isFinite(bytes) || bytes < 0) return "0 B";
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
if (bytes < 1024 * 1024 * 1024)
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
}