2020-11-04 12:30:01 +01:00
|
|
|
// Found on Stackoverflow, perfect code :D https://stackoverflow.com/a/14919494/8654475
|
2020-11-09 12:42:26 +01:00
|
|
|
function humanFileSize(bytes, si = false, dp = 1) {
|
2020-11-04 12:30:01 +01:00
|
|
|
const thresh = si ? 1000 : 1024;
|
2020-11-09 12:42:26 +01:00
|
|
|
|
2020-11-04 12:30:01 +01:00
|
|
|
if (Math.abs(bytes) < thresh) {
|
2020-11-09 12:42:26 +01:00
|
|
|
return bytes + " B";
|
2020-11-04 12:30:01 +01:00
|
|
|
}
|
2020-11-09 12:42:26 +01:00
|
|
|
|
|
|
|
const units = si ? ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] : ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
|
2020-11-04 12:30:01 +01:00
|
|
|
let u = -1;
|
2020-11-09 12:42:26 +01:00
|
|
|
const r = 10 ** dp;
|
|
|
|
|
2020-11-04 12:30:01 +01:00
|
|
|
do {
|
|
|
|
bytes /= thresh;
|
|
|
|
++u;
|
|
|
|
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
|
2020-11-09 12:42:26 +01:00
|
|
|
|
|
|
|
return bytes.toFixed(dp) + " " + units[u];
|
2020-11-04 12:30:01 +01:00
|
|
|
}
|
|
|
|
|
2022-04-30 16:19:31 +02:00
|
|
|
export { humanFileSize } ;
|