12 lines
378 B
TypeScript
12 lines
378 B
TypeScript
export const KILOBYTE = 1024
|
|
export const MEGABYTE = 1024 * KILOBYTE
|
|
|
|
/**
|
|
* Formats a byte count for error messages and status lines, e.g. "12.3 MB".
|
|
*/
|
|
export function formatBytes(bytes: number): string {
|
|
if (bytes >= MEGABYTE) return `${(bytes / MEGABYTE).toFixed(1)} MB`
|
|
if (bytes >= KILOBYTE) return `${(bytes / KILOBYTE).toFixed(1)} KB`
|
|
return `${bytes} B`
|
|
}
|