@jk-core/components
Version:
components for jk
17 lines (14 loc) • 381 B
text/typescript
export const formatFileSize = (bytes: number, unit: 'KB' | 'MB' | 'GB' | 'TB' = 'MB') => {
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
let unitIndex = 0;
let size = bytes;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex += 1;
if (units[unitIndex] === unit) break;
}
return ({
size,
unit: units[unitIndex],
});
};