14 lines
332 B
JavaScript
14 lines
332 B
JavaScript
const sizes = ['B', 'KB', 'MB', 'GB']
|
|
|
|
export const formatFileSize = (bytes) => {
|
|
if (!bytes) {
|
|
return `0 ${sizes[0]}`
|
|
}
|
|
|
|
const k = 1024
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
|
const formattedSize = parseFloat((bytes / Math.pow(k, i)).toFixed(1))
|
|
|
|
return `${formattedSize} ${sizes[i]}`
|
|
}
|