mirror of
https://github.com/baserow/baserow.git
synced 2026-12-06 09:18:47 +00:00
19 lines
616 B
JavaScript
19 lines
616 B
JavaScript
/**
|
|
* Originally from
|
|
* https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
|
|
*
|
|
* Converts an integer representing the amount of bytes to a human readable format.
|
|
* Where for example 1024 will end up in 1KB.
|
|
*/
|
|
export function formatBytes(bytes, decimals = 2) {
|
|
if (bytes === 0) return '0 Bytes'
|
|
|
|
const k = 1024
|
|
const dm = decimals < 0 ? 0 : decimals
|
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
|
|
|
return parseFloat((bytes / k ** i).toFixed(dm)) + ' ' + sizes[i]
|
|
}
|