webpack/lib/SizeFormatHelpers.js

19 lines
436 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 => {
2016-12-05 06:47:19 +08:00
if(size <= 0) {
return "0 bytes";
}
2017-02-21 21:52:40 +08:00
const abbreviations = ["bytes", "kB", "MB", "GB"];
const index = Math.floor(Math.log(size) / Math.log(1000));
2017-02-21 21:52:40 +08:00
return `${+(size / Math.pow(1000, index)).toPrecision(3)} ${abbreviations[index]}`;
2017-01-11 17:51:58 +08:00
};