mirror of
https://github.com/Luzifer/ots.git
synced 2024-12-18 12:04:35 -05:00
22 lines
456 B
JavaScript
22 lines
456 B
JavaScript
/**
|
|
* Converts number of bytes into human format (524288 -> "512.0 KiB")
|
|
* @param {Number} bytes Byte amount to convert into human readable format
|
|
* @returns String
|
|
*/
|
|
function bytesToHuman(bytes) {
|
|
for (const t of [
|
|
{ thresh: 1024 * 1024, unit: 'MiB' },
|
|
{ thresh: 1024, unit: 'KiB' },
|
|
]) {
|
|
if (bytes > t.thresh) {
|
|
return `${(bytes / t.thresh).toFixed(1)} ${t.unit}`
|
|
}
|
|
}
|
|
|
|
return `${bytes} B`
|
|
}
|
|
|
|
export {
|
|
bytesToHuman,
|
|
}
|