filesize.es6.js 4.0 KB

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