cobalt/web/src/lib/util.ts

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}`;
}