mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
18 lines
476 B
TypeScript
18 lines
476 B
TypeScript
const BYTE_UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'] as const
|
|
|
|
export function formatBytes(bytes: number): string {
|
|
if (!Number.isFinite(bytes) || bytes <= 0) {
|
|
return '0 B'
|
|
}
|
|
|
|
let value = bytes
|
|
let unitIndex = 0
|
|
while (value >= 1024 && unitIndex < BYTE_UNITS.length - 1) {
|
|
value /= 1024
|
|
unitIndex += 1
|
|
}
|
|
|
|
const precision = value >= 100 || unitIndex === 0 ? 0 : value >= 10 ? 1 : 2
|
|
return `${value.toFixed(precision)} ${BYTE_UNITS[unitIndex]}`
|
|
}
|