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.js 5.1KB

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