filesize.esm.js 4.6 KB

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