You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SizeFormatHelpers.js 538B

123456789101112131415161718192021222324
  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 (typeof size !== "number" || Number.isNaN(size) === true) {
  9. return "unknown size";
  10. }
  11. if (size <= 0) {
  12. return "0 bytes";
  13. }
  14. const abbreviations = ["bytes", "KiB", "MiB", "GiB"];
  15. const index = Math.floor(Math.log(size) / Math.log(1024));
  16. return `${+(size / Math.pow(1024, index)).toPrecision(3)} ${
  17. abbreviations[index]
  18. }`;
  19. };