2016-11-29 23:45:03 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Sean Larkin @thelarkinn
|
|
|
|
*/
|
2016-11-29 23:32:10 +08:00
|
|
|
var SizeFormatHelpers = exports;
|
|
|
|
|
|
|
|
SizeFormatHelpers.formatSize = function(size) {
|
2016-12-05 06:47:19 +08:00
|
|
|
if(size <= 0) {
|
2016-11-29 23:45:03 +08:00
|
|
|
return "0 bytes";
|
|
|
|
}
|
2016-11-29 23:32:10 +08:00
|
|
|
|
2016-11-29 23:45:03 +08:00
|
|
|
var abbreviations = ["bytes", "kB", "MB", "GB"];
|
|
|
|
var index = Math.floor(Math.log(size) / Math.log(1000));
|
2016-11-29 23:32:10 +08:00
|
|
|
|
2016-12-05 06:47:19 +08:00
|
|
|
return +(size / Math.pow(1000, index))
|
2016-11-29 23:45:03 +08:00
|
|
|
.toPrecision(3) + " " + abbreviations[index];
|
|
|
|
}
|