2024-01-13 22:41:40 +00:00
|
|
|
function formatBytes(bytes, decimals = 2) {
|
2024-04-15 11:07:40 +00:00
|
|
|
if (!+bytes) return "0 B";
|
2024-01-13 22:41:40 +00:00
|
|
|
|
2024-04-15 11:07:40 +00:00
|
|
|
const k = 1024;
|
|
|
|
const dm = decimals < 0 ? 0 : decimals;
|
|
|
|
const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
2024-01-13 22:41:40 +00:00
|
|
|
|
2024-04-15 11:07:40 +00:00
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
2024-01-13 22:41:40 +00:00
|
|
|
|
2024-04-15 11:07:40 +00:00
|
|
|
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))}${sizes[i]}`;
|
2024-01-13 22:41:40 +00:00
|
|
|
}
|
|
|
|
|
2024-04-15 11:07:40 +00:00
|
|
|
module.exports = { formatBytes };
|