Nav apraksta

pretty-bytes.js 919B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*!
  2. pretty-bytes
  3. Convert bytes to a human readable string: 1337 → 1.34 kB
  4. https://github.com/sindresorhus/pretty-bytes
  5. by Sindre Sorhus
  6. MIT License
  7. */
  8. (function () {
  9. 'use strict';
  10. // Number.isNaN() polyfill
  11. var isNaN = function (val) {
  12. return val !== val;
  13. };
  14. var prettyBytes = function (num) {
  15. if (typeof num !== 'number' || isNaN(num)) {
  16. throw new TypeError('Input must be a number');
  17. }
  18. var exponent;
  19. var unit;
  20. var neg = num < 0;
  21. if (neg) {
  22. num = -num;
  23. }
  24. if (num === 0) {
  25. return '0 B';
  26. }
  27. exponent = Math.floor(Math.log(num) / Math.log(1000));
  28. num = (num / Math.pow(1000, exponent)).toFixed(2) * 1;
  29. unit = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'][exponent];
  30. return (neg ? '-' : '') + num + ' ' + unit;
  31. };
  32. if (typeof module !== 'undefined' && module.exports) {
  33. module.exports = prettyBytes;
  34. } else {
  35. window.prettyBytes = prettyBytes;
  36. }
  37. })();