mirror of
https://github.com/imputnet/cobalt.git
synced 2025-06-29 09:58:27 +00: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}`;
|
|
}
|