utils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AggregateError = exports.CustomAggregateError = exports.AbortError = exports.zip = exports.wait = exports.parseContentRange = exports.toArrayRecursively = exports.toArray = exports.times = exports.range = exports.invert = exports.forEach = exports.endsWith = exports.chunk = exports.assign = void 0;
  4. function assign(target, source) {
  5. for (const key in source) {
  6. if (source.hasOwnProperty(key)) {
  7. target[key] = source[key];
  8. }
  9. }
  10. }
  11. exports.assign = assign;
  12. function chunk(iterable, length) {
  13. const results = [];
  14. const lengthOfIterable = iterable.length;
  15. for (let i = 0; i < lengthOfIterable; i += length) {
  16. const chunked = [];
  17. for (let ci = i; ci < i + length; ci++) {
  18. chunked.push(iterable[ci]);
  19. }
  20. results.push(chunked);
  21. }
  22. return results;
  23. }
  24. exports.chunk = chunk;
  25. function endsWith(string, expectedEnding) {
  26. if (string.length < expectedEnding.length) {
  27. return false;
  28. }
  29. const actualEnding = string.substr(string.length - expectedEnding.length);
  30. return actualEnding === expectedEnding;
  31. }
  32. exports.endsWith = endsWith;
  33. function forEach(iterable, func) {
  34. const { length } = iterable;
  35. for (let i = 0; i < length; i++) {
  36. func(iterable[i], i);
  37. }
  38. }
  39. exports.forEach = forEach;
  40. function invert(oldObj) {
  41. const newObj = {};
  42. for (const key in oldObj) {
  43. if (oldObj.hasOwnProperty(key)) {
  44. const value = oldObj[key];
  45. newObj[value] = key;
  46. }
  47. }
  48. return newObj;
  49. }
  50. exports.invert = invert;
  51. function range(n) {
  52. const results = [];
  53. for (let i = 0; i < n; i++) {
  54. results.push(i);
  55. }
  56. return results;
  57. }
  58. exports.range = range;
  59. function times(numTimes, func) {
  60. const results = [];
  61. for (let i = 0; i < numTimes; i++) {
  62. results.push(func(i));
  63. }
  64. return results;
  65. }
  66. exports.times = times;
  67. function toArray(iterable) {
  68. const results = [];
  69. const { length } = iterable;
  70. for (let i = 0; i < length; i++) {
  71. results.push(iterable[i]);
  72. }
  73. return results;
  74. }
  75. exports.toArray = toArray;
  76. function toArrayRecursively(input) {
  77. if (input.length) {
  78. return toArray(input).map(toArrayRecursively);
  79. }
  80. return input;
  81. }
  82. exports.toArrayRecursively = toArrayRecursively;
  83. // copied from https://github.com/academia-de-codigo/parse-content-range-header/blob/master/index.js
  84. function parseContentRange(headerValue) {
  85. if (!headerValue) {
  86. return null;
  87. }
  88. if (typeof headerValue !== 'string') {
  89. throw new Error('invalid argument');
  90. }
  91. const parseInt = (number) => Number.parseInt(number, 10);
  92. // Check for presence of unit
  93. let matches = headerValue.match(/^(\w*) /);
  94. const unit = matches && matches[1];
  95. // check for start-end/size header format
  96. matches = headerValue.match(/(\d+)-(\d+)\/(\d+|\*)/);
  97. if (matches) {
  98. return {
  99. unit,
  100. first: parseInt(matches[1]),
  101. last: parseInt(matches[2]),
  102. length: matches[3] === '*' ? null : parseInt(matches[3]),
  103. };
  104. }
  105. // check for size header format
  106. matches = headerValue.match(/(\d+|\*)/);
  107. if (matches) {
  108. return {
  109. unit,
  110. first: null,
  111. last: null,
  112. length: matches[1] === '*' ? null : parseInt(matches[1]),
  113. };
  114. }
  115. return null;
  116. }
  117. exports.parseContentRange = parseContentRange;
  118. /*
  119. * Promisified wrapper around 'setTimeout' to allow 'await'
  120. */
  121. async function wait(milliseconds) {
  122. return new Promise((resolve) => setTimeout(resolve, milliseconds));
  123. }
  124. exports.wait = wait;
  125. function zip(a, b) {
  126. const A = Array.isArray(a) ? a : Array.from(a);
  127. const B = Array.isArray(b) ? b : Array.from(b);
  128. return A.map((k, i) => [k, B[i]]);
  129. }
  130. exports.zip = zip;
  131. // Based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
  132. class AbortError extends Error {
  133. constructor(params) {
  134. // Pass remaining arguments (including vendor specific ones) to parent constructor
  135. super(params);
  136. // Maintains proper stack trace for where our error was thrown (only available on V8)
  137. if (Error.captureStackTrace) {
  138. Error.captureStackTrace(this, AbortError);
  139. }
  140. this.name = 'AbortError';
  141. }
  142. }
  143. exports.AbortError = AbortError;
  144. class CustomAggregateError extends Error {
  145. constructor(errors, message) {
  146. super(message);
  147. this.errors = errors;
  148. this.message = message;
  149. this.name = 'AggregateError';
  150. }
  151. }
  152. exports.CustomAggregateError = CustomAggregateError;
  153. exports.AggregateError = CustomAggregateError;
  154. //# sourceMappingURL=utils.js.map