filesize.js 5.5 KB

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