/
githubmirror
/
webpack
Обзор
Документация
Войти
/
githubmirror
/
webpack
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
lib/util/formatSize.js
32 строки
766 B
Alexander Akait
fix: several small correctness fixes (formatSize, data URIs, CSS unescape, RegExp flags) (#21366)
08 июл 2026, 23:21
Не верифицирован
08 июл 2026, 23:21
2d60c44
Код
Авторство
О чём код?
/* MIT License http://www.opensource.org/licenses/mit-license.php Author Sean Larkin @thelarkinn */ "use strict"; /** * Returns the formatted size. * @param {number=} size the size in bytes * @returns {string} the formatted size */ const formatSize = (size) => { if (typeof size !== "number" || Number.isNaN(size) === true) { return "unknown size"; } if (size <= 0) { return "0 bytes"; } const abbreviations = ["bytes", "KiB", "MiB", "GiB", "TiB", "PiB"]; // clamp so sizes beyond the largest unit don't index past the table const index = Math.min( Math.floor(Math.log(size) / Math.log(1024)), abbreviations.length - 1 ); return `${Number((size / 1024 ** index).toPrecision(3))} ${abbreviations[index]}`; }; module.exports = formatSize;