filesize.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. "use strict";
  2. /**
  3. * filesize
  4. *
  5. * @copyright 2018 Jason Mulligan <jason.mulligan@avoidwork.com>
  6. * @license BSD-3-Clause
  7. * @version 3.6.1
  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. neg = void 0,
  44. num = void 0,
  45. output = void 0,
  46. round = void 0,
  47. unix = void 0,
  48. separator = void 0,
  49. spacer = void 0,
  50. standard = void 0,
  51. symbols = void 0;
  52. if (isNaN(arg)) {
  53. throw new Error("Invalid arguments");
  54. }
  55. bits = descriptor.bits === true;
  56. unix = descriptor.unix === true;
  57. base = descriptor.base || 2;
  58. round = descriptor.round !== void 0 ? descriptor.round : unix ? 1 : 2;
  59. separator = descriptor.separator !== void 0 ? descriptor.separator || "" : "";
  60. spacer = descriptor.spacer !== void 0 ? descriptor.spacer : unix ? "" : " ";
  61. symbols = descriptor.symbols || descriptor.suffixes || {};
  62. standard = base === 2 ? descriptor.standard || "jedec" : "jedec";
  63. output = descriptor.output || "string";
  64. full = descriptor.fullform === true;
  65. fullforms = descriptor.fullforms instanceof Array ? descriptor.fullforms : [];
  66. e = descriptor.exponent !== void 0 ? descriptor.exponent : -1;
  67. num = Number(arg);
  68. neg = num < 0;
  69. ceil = base > 2 ? 1000 : 1024;
  70. // Flipping a negative number to determine the size
  71. if (neg) {
  72. num = -num;
  73. }
  74. // Determining the exponent
  75. if (e === -1 || isNaN(e)) {
  76. e = Math.floor(Math.log(num) / Math.log(ceil));
  77. if (e < 0) {
  78. e = 0;
  79. }
  80. }
  81. // Exceeding supported length, time to reduce & multiply
  82. if (e > 8) {
  83. e = 8;
  84. }
  85. // Zero is now a special case because bytes divide by 1
  86. if (num === 0) {
  87. result[0] = 0;
  88. result[1] = unix ? "" : symbol[standard][bits ? "bits" : "bytes"][e];
  89. } else {
  90. val = num / (base === 2 ? Math.pow(2, e * 10) : Math.pow(1000, e));
  91. if (bits) {
  92. val = val * 8;
  93. if (val >= ceil && e < 8) {
  94. val = val / ceil;
  95. e++;
  96. }
  97. }
  98. result[0] = Number(val.toFixed(e > 0 ? round : 0));
  99. result[1] = base === 10 && e === 1 ? bits ? "kb" : "kB" : symbol[standard][bits ? "bits" : "bytes"][e];
  100. if (unix) {
  101. result[1] = standard === "jedec" ? result[1].charAt(0) : e > 0 ? result[1].replace(/B$/, "") : result[1];
  102. if (b.test(result[1])) {
  103. result[0] = Math.floor(result[0]);
  104. result[1] = "";
  105. }
  106. }
  107. }
  108. // Decorating a 'diff'
  109. if (neg) {
  110. result[0] = -result[0];
  111. }
  112. // Applying custom symbol
  113. result[1] = symbols[result[1]] || result[1];
  114. // Returning Array, Object, or String (default)
  115. if (output === "array") {
  116. return result;
  117. }
  118. if (output === "exponent") {
  119. return e;
  120. }
  121. if (output === "object") {
  122. return { value: result[0], suffix: result[1], symbol: result[1] };
  123. }
  124. if (full) {
  125. result[1] = fullforms[e] ? fullforms[e] : fullform[standard][e] + (bits ? "bit" : "byte") + (result[0] === 1 ? "" : "s");
  126. }
  127. if (separator.length > 0) {
  128. result[0] = result[0].toString().replace(".", separator);
  129. }
  130. return result.join(spacer);
  131. }
  132. // Partial application for functional programming
  133. filesize.partial = function (opt) {
  134. return function (arg) {
  135. return filesize(arg, opt);
  136. };
  137. };
  138. // CommonJS, AMD, script tag
  139. if (typeof exports !== "undefined") {
  140. module.exports = filesize;
  141. } else if (typeof define === "function" && define.amd) {
  142. define(function () {
  143. return filesize;
  144. });
  145. } else {
  146. global.filesize = filesize;
  147. }
  148. })(typeof window !== "undefined" ? window : global);