mirror of
https://github.com/wukko/cobalt.git
synced 2025-05-02 07:04:27 +02:00
15 lines
397 B
TypeScript
15 lines
397 B
TypeScript
export const formatFileSize = (size: number | undefined) => {
|
|
size ||= 0;
|
|
|
|
// gigabyte, megabyte, kilobyte, byte
|
|
const units = ['G', 'M', 'K', ''];
|
|
while (size >= 1024 && units.length > 1) {
|
|
size /= 1024;
|
|
units.pop();
|
|
}
|
|
|
|
const roundedSize = parseFloat(size.toFixed(2));
|
|
const unit = units[units.length - 1] + "B";
|
|
return `${roundedSize} ${unit}`;
|
|
}
|