webpack/lib/SizeFormatHelpers.js

21 lines
445 B
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Sean Larkin @thelarkinn
*/
2017-02-21 21:52:40 +08:00
"use strict";
2017-02-21 21:52:40 +08:00
const SizeFormatHelpers = exports;
SizeFormatHelpers.formatSize = 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));
2018-02-25 09:00:20 +08:00
return `${+(size / Math.pow(1024, index)).toPrecision(3)} ${
abbreviations[index]
}`;
2017-01-11 17:51:58 +08:00
};