SizeFormatHelpers.js 436B

12345678910111213141516171819
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Sean Larkin @thelarkinn
  4. */
  5. "use strict";
  6. const SizeFormatHelpers = exports;
  7. SizeFormatHelpers.formatSize = size => {
  8. if(size <= 0) {
  9. return "0 bytes";
  10. }
  11. const abbreviations = ["bytes", "kB", "MB", "GB"];
  12. const index = Math.floor(Math.log(size) / Math.log(1000));
  13. return `${+(size / Math.pow(1000, index)).toPrecision(3)} ${abbreviations[index]}`;
  14. };