2016-11-29 23:45:03 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Sean Larkin @thelarkinn
|
|
|
|
*/
|
2017-02-21 21:52:40 +08:00
|
|
|
"use strict";
|
2016-11-29 23:32:10 +08:00
|
|
|
|
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) {
|
2016-11-29 23:45:03 +08:00
|
|
|
return "0 bytes";
|
|
|
|
}
|
2016-11-29 23:32:10 +08:00
|
|
|
|
2017-02-21 21:52:40 +08:00
|
|
|
const abbreviations = ["bytes", "kB", "MB", "GB"];
|
|
|
|
const index = Math.floor(Math.log(size) / Math.log(1000));
|
2016-11-29 23:32:10 +08:00
|
|
|
|
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
|
|
|
};
|