ots/src/helpers.js
2023-10-02 21:52:24 +02:00

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,
}