19 lines
391 B
JavaScript
19 lines
391 B
JavaScript
export const formatFileSize = (value) => {
|
|
const bytes = Number(value);
|
|
if (!Number.isFinite(bytes) || bytes <= 0) {
|
|
return '0 B';
|
|
}
|
|
|
|
const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
|
|
let index = 0;
|
|
let amount = bytes;
|
|
|
|
while (amount >= 1024 && index < units.length - 1) {
|
|
amount /= 1024;
|
|
index += 1;
|
|
}
|
|
|
|
return `${amount.toFixed(2)} ${units[index]}`;
|
|
};
|
|
|