f1cff5441a
Signed-off-by: Sebastian <krupinski01@gmail.com>
21 lines
528 B
TypeScript
21 lines
528 B
TypeScript
export function formatFileSize(bytes: number | null | undefined): string {
|
|
if (bytes == null || !Number.isFinite(bytes)) {
|
|
return ''
|
|
}
|
|
|
|
if (bytes < 1024) {
|
|
return `${Math.max(0, Math.round(bytes))} B`
|
|
}
|
|
|
|
const units = ['KB', 'MB', 'GB', 'TB']
|
|
let value = bytes / 1024
|
|
let unitIndex = 0
|
|
|
|
while (value >= 1024 && unitIndex < units.length - 1) {
|
|
value /= 1024
|
|
unitIndex += 1
|
|
}
|
|
|
|
const precision = value >= 10 || unitIndex === 0 ? 1 : 2
|
|
return `${value.toFixed(precision)} ${units[unitIndex]}`
|
|
} |