Files
bambuddy/frontend/src/utils/file.ts
T
2026-02-18 03:32:24 -10:00

21 lines
604 B
TypeScript

/**
* Formats a byte count into a human-readable string (e.g. `1.5 MB`).
*
* @param bytes - The number of bytes to format.
* @returns A formatted string with the appropriate unit (B, KB, MB, GB, or TB).
*/
export function formatFileSize(bytes: number): string {
if (bytes === 0) return '0 B';
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
const k = 1024;
const i = Math.floor(Math.log(bytes) / Math.log(k));
const size = bytes / Math.pow(k, i);
// No decimals for bytes, 1 decimal for larger units
return i === 0
? `${size} ${units[i]}`
: `${size.toFixed(1)} ${units[i]}`;
}