webpack/lib/SizeFormatHelpers.js

26 lines
586 B
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Sean Larkin @thelarkinn
*/
2018-07-30 23:08:51 +08:00
2017-02-21 21:52:40 +08:00
"use strict";
2018-11-09 05:59:19 +08:00
/**
* @param {number} size the size in bytes
* @returns {string} the formatted size
*/
module.exports.formatSize = (size) => {
2018-03-29 21:28:03 +08:00
if (typeof size !== "number" || Number.isNaN(size) === true) {
return "unknown size";
}
2018-02-25 09:00:20 +08:00
if (size <= 0) {
return "0 bytes";
}
const abbreviations = ["bytes", "KiB", "MiB", "GiB"];
const index = Math.floor(Math.log(size) / Math.log(1024));
2024-07-31 11:28:55 +08:00
return `${Number((size / 1024 ** index).toPrecision(3))} ${abbreviations[index]}`;
2017-01-11 17:51:58 +08:00
};