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.

filesize.es6.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * filesize
  3. *
  4. * @copyright 2020 Jason Mulligan <jason.mulligan@avoidwork.com>
  5. * @license BSD-3-Clause
  6. * @version 6.1.0
  7. */
  8. (function (global) {
  9. const b = /^(b|B)$/,
  10. symbol = {
  11. iec: {
  12. bits: ["b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib"],
  13. bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
  14. },
  15. jedec: {
  16. bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
  17. bytes: ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
  18. }
  19. },
  20. fullform = {
  21. iec: ["", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi"],
  22. jedec: ["", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta"]
  23. };
  24. /**
  25. * filesize
  26. *
  27. * @method filesize
  28. * @param {Mixed} arg String, Int or Float to transform
  29. * @param {Object} descriptor [Optional] Flags
  30. * @return {String} Readable file size String
  31. */
  32. function filesize (arg, descriptor = {}) {
  33. let result = [],
  34. val = 0,
  35. e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, round, unix, separator, spacer, standard, symbols;
  36. if (isNaN(arg)) {
  37. throw new TypeError("Invalid number");
  38. }
  39. bits = descriptor.bits === true;
  40. unix = descriptor.unix === true;
  41. base = descriptor.base || 2;
  42. round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;
  43. locale = descriptor.locale !== void 0 ? descriptor.locale : "";
  44. localeOptions = descriptor.localeOptions || {};
  45. separator = descriptor.separator !== void 0 ? descriptor.separator : "";
  46. spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " ";
  47. symbols = descriptor.symbols || {};
  48. standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
  49. output = descriptor.output || "string";
  50. full = descriptor.fullform === true;
  51. fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
  52. e = descriptor.exponent !== void 0 ? descriptor.exponent : -1;
  53. num = Number(arg);
  54. neg = num < 0;
  55. ceil = base > 2 ? 1000 : 1024;
  56. // Flipping a negative number to determine the size
  57. if (neg) {
  58. num = -num;
  59. }
  60. // Determining the exponent
  61. if (e === -1 || isNaN(e)) {
  62. e = Math.floor(Math.log(num) / Math.log(ceil));
  63. if (e < 0) {
  64. e = 0;
  65. }
  66. }
  67. // Exceeding supported length, time to reduce & multiply
  68. if (e > 8) {
  69. e = 8;
  70. }
  71. if (output === "exponent") {
  72. return e;
  73. }
  74. // Zero is now a special case because bytes divide by 1
  75. if (num === 0) {
  76. result[0] = 0;
  77. result[1] = unix ? "" : symbol[standard][bits ? "bits" : "bytes"][e];
  78. } else {
  79. val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
  80. if (bits) {
  81. val = val * 8;
  82. if (val >= ceil && e < 8) {
  83. val = val / ceil;
  84. e++;
  85. }
  86. }
  87. result[0] = Number(val.toFixed(e > 0 ? round : 0));
  88. if (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {
  89. result[0] = 1;
  90. e++;
  91. }
  92. result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
  93. if (unix) {
  94. result[1] = standard === "jedec" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, "") : result[1];
  95. if (b.test(result[1])) {
  96. result[0] = Math.floor(result[0]);
  97. result[1] = "";
  98. }
  99. }
  100. }
  101. // Decorating a 'diff'
  102. if (neg) {
  103. result[0] = -result[0];
  104. }
  105. // Applying custom symbol
  106. result[1] = symbols[result[1]] || result[1];
  107. if (locale === true) {
  108. result[0] = result[0].toLocaleString();
  109. } else if (locale.length > 0) {
  110. result[0] = result[0].toLocaleString(locale, localeOptions);
  111. } else if (separator.length > 0) {
  112. result[0] = result[0].toString().replace(".", separator);
  113. }
  114. // Returning Array, Object, or String (default)
  115. if (output === "array") {
  116. return result;
  117. }
  118. if (full) {
  119. result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
  120. }
  121. if (output === "object") {
  122. return {value: result[0], symbol: result[1], exponent: e};
  123. }
  124. return result.join(spacer);
  125. }
  126. // Partial application for functional programming
  127. filesize.partial = opt => arg => filesize(arg, opt);
  128. // CommonJS, AMD, script tag
  129. if (typeof exports !== "undefined") {
  130. module.exports = filesize;
  131. } else if (typeof define === "function" && define.amd !== void 0) {
  132. define(() => filesize);
  133. } else {
  134. global.filesize = filesize;
  135. }
  136. }(typeof window !== "undefined" ? window : global));