filesize.es6.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. (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. const b = /^(b|B)$/,
  14. symbol = {
  15. iec: {
  16. bits: ["b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib"],
  17. bytes: ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
  18. },
  19. jedec: {
  20. bits: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"],
  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, descriptor = {}) {
  41. let result = [],
  42. val = 0,
  43. e, base, bits, ceil, full, fullforms, locale, localeOptions, neg, num, output, pad, round, u, unix, separator, spacer, standard, symbols, roundingFunc, precision;
  44. if (isNaN(arg)) {
  45. throw new TypeError("Invalid number");
  46. }
  47. bits = descriptor.bits === true;
  48. unix = descriptor.unix === true;
  49. pad = descriptor.pad === true;
  50. base = descriptor.base || 2;
  51. round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;
  52. locale = descriptor.locale !== void 0 ? descriptor.locale : "";
  53. localeOptions = descriptor.localeOptions || {};
  54. separator = descriptor.separator !== void 0 ? descriptor.separator : "";
  55. spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " ";
  56. symbols = descriptor.symbols || {};
  57. standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
  58. output = descriptor.output || "string";
  59. full = descriptor.fullform === true;
  60. fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
  61. e = descriptor.exponent !== void 0 ? descriptor.exponent : -1;
  62. roundingFunc = roundingFuncs[descriptor.roundingMethod] || Math.round;
  63. num = Number(arg);
  64. neg = num < 0;
  65. ceil = base > 2 ? 1000 : 1024;
  66. precision = isNaN(descriptor.precision) === false ? parseInt(descriptor.precision, 10) : 0;
  67. // Flipping a negative number to determine the size
  68. if (neg) {
  69. num = -num;
  70. }
  71. // Determining the exponent
  72. if (e === -1 || isNaN(e)) {
  73. e = Math.floor(Math.log(num) / Math.log(ceil));
  74. if (e < 0) {
  75. e = 0;
  76. }
  77. }
  78. // Exceeding supported length, time to reduce & multiply
  79. if (e > 8) {
  80. if (precision > 0) {
  81. precision += 8 - e;
  82. }
  83. e = 8;
  84. }
  85. if (output === "exponent") {
  86. return e;
  87. }
  88. // Zero is now a special case because bytes divide by 1
  89. if (num === 0) {
  90. result[0] = 0;
  91. u = 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. const p = Math.pow(10, e > 0 ? round : 0);
  102. result[0] = roundingFunc(val * p) / p;
  103. if (result[0] === ceil && e < 8 && descriptor.exponent === void 0) {
  104. result[0] = 1;
  105. e++;
  106. }
  107. u = result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
  108. if (unix) {
  109. result[1] = standard === "jedec" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, "") : result[1];
  110. if (b.test(result[1])) {
  111. result[0] = Math.floor(result[0]);
  112. result[1] = "";
  113. }
  114. }
  115. }
  116. // Decorating a 'diff'
  117. if (neg) {
  118. result[0] = -result[0];
  119. }
  120. // Setting optional precision
  121. if (precision > 0) {
  122. result[0] = result[0].toPrecision(precision);
  123. }
  124. // Applying custom symbol
  125. result[1] = symbols[result[1]] || result[1];
  126. if (locale === true) {
  127. result[0] = result[0].toLocaleString();
  128. } else if (locale.length > 0) {
  129. result[0] = result[0].toLocaleString(locale, localeOptions);
  130. } else if (separator.length > 0) {
  131. result[0] = result[0].toString().replace(".", separator);
  132. }
  133. if (pad && Number.isInteger(result[0]) === false && round > 0) {
  134. const x = separator || ".",
  135. tmp = result[0].toString().split(x),
  136. s = tmp[1] || "",
  137. l = s.length,
  138. n = round - l;
  139. result[0] = `${tmp[0]}${x}${s.padEnd(l + n, "0")}`;
  140. }
  141. if (full) {
  142. result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
  143. }
  144. // Returning Array, Object, or String (default)
  145. return output === "array" ? result : output === "object" ? {value: result[0], symbol: result[1], exponent: e, unit: u} : result.join(spacer);
  146. }
  147. // Partial application for functional programming
  148. filesize.partial = opt => arg => filesize(arg, opt);
  149. return filesize;
  150. })));