es-toolkit 0.0.1-dev.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/README.md +31 -0
  3. package/dist/array/chunk.d.ts +24 -0
  4. package/dist/array/chunk.spec.d.ts +1 -0
  5. package/dist/array/difference.d.ts +23 -0
  6. package/dist/array/difference.spec.d.ts +1 -0
  7. package/dist/array/differenceBy.d.ts +26 -0
  8. package/dist/array/differenceBy.spec.d.ts +1 -0
  9. package/dist/array/differenceWith.d.ts +22 -0
  10. package/dist/array/differenceWith.spec.d.ts +1 -0
  11. package/dist/array/drop.d.ts +16 -0
  12. package/dist/array/drop.spec.d.ts +1 -0
  13. package/dist/array/dropRight.d.ts +16 -0
  14. package/dist/array/dropRight.spec.d.ts +1 -0
  15. package/dist/array/dropRightWhile.d.ts +18 -0
  16. package/dist/array/dropRightWhile.spec.d.ts +1 -0
  17. package/dist/array/dropWhile.d.ts +18 -0
  18. package/dist/array/dropWhile.spec.d.ts +1 -0
  19. package/dist/array/groupBy.d.ts +31 -0
  20. package/dist/array/groupBy.spec.d.ts +1 -0
  21. package/dist/array/index.d.ts +30 -0
  22. package/dist/array/index.js +991 -0
  23. package/dist/array/intersection.d.ts +18 -0
  24. package/dist/array/intersection.spec.d.ts +1 -0
  25. package/dist/array/intersectionBy.d.ts +21 -0
  26. package/dist/array/intersectionBy.spec.d.ts +1 -0
  27. package/dist/array/intersectionWith.d.ts +22 -0
  28. package/dist/array/intersectionWith.spec.d.ts +1 -0
  29. package/dist/array/partition.d.ts +22 -0
  30. package/dist/array/partition.spec.d.ts +1 -0
  31. package/dist/array/sample.d.ts +14 -0
  32. package/dist/array/sample.spec.d.ts +1 -0
  33. package/dist/array/shuffle.d.ts +14 -0
  34. package/dist/array/shuffle.spec.d.ts +1 -0
  35. package/dist/array/take.d.ts +23 -0
  36. package/dist/array/take.spec.d.ts +1 -0
  37. package/dist/array/takeRight.d.ts +21 -0
  38. package/dist/array/takeRight.spec.d.ts +1 -0
  39. package/dist/array/takeRightWhile.d.ts +18 -0
  40. package/dist/array/takeRightWhile.spec.d.ts +1 -0
  41. package/dist/array/takeWhile.d.ts +19 -0
  42. package/dist/array/takeWhile.spec.d.ts +1 -0
  43. package/dist/array/union.d.ts +17 -0
  44. package/dist/array/union.spec.d.ts +1 -0
  45. package/dist/array/unionBy.d.ts +21 -0
  46. package/dist/array/unionBy.spec.d.ts +1 -0
  47. package/dist/array/unionWith.d.ts +20 -0
  48. package/dist/array/unionWith.spec.d.ts +1 -0
  49. package/dist/array/uniq.d.ts +15 -0
  50. package/dist/array/uniq.spec.d.ts +1 -0
  51. package/dist/array/uniqBy.d.ts +12 -0
  52. package/dist/array/uniqBy.spec.d.ts +1 -0
  53. package/dist/array/uniqWith.d.ts +12 -0
  54. package/dist/array/uniqWith.spec.d.ts +1 -0
  55. package/dist/array/xor.d.ts +17 -0
  56. package/dist/array/xor.spec.d.ts +1 -0
  57. package/dist/array/xorBy.d.ts +20 -0
  58. package/dist/array/xorBy.spec.d.ts +1 -0
  59. package/dist/array/xorWith.d.ts +19 -0
  60. package/dist/array/xorWith.spec.d.ts +1 -0
  61. package/dist/array/zip.d.ts +25 -0
  62. package/dist/array/zip.spec.d.ts +1 -0
  63. package/dist/array/zipWith.d.ts +32 -0
  64. package/dist/array/zipWith.spec.d.ts +1 -0
  65. package/dist/function/debounce.d.ts +24 -0
  66. package/dist/function/debounce.spec.d.ts +1 -0
  67. package/dist/function/index.d.ts +3 -0
  68. package/dist/function/index.js +112 -0
  69. package/dist/function/once.d.ts +17 -0
  70. package/dist/function/once.spec.d.ts +1 -0
  71. package/dist/function/throttle.d.ts +26 -0
  72. package/dist/function/throttle.spec.d.ts +1 -0
  73. package/dist/index.d.ts +6 -0
  74. package/dist/index.js +1514 -0
  75. package/dist/math/clamp.d.ts +19 -0
  76. package/dist/math/clamp.spec.d.ts +1 -0
  77. package/dist/math/index.d.ts +3 -0
  78. package/dist/math/index.js +148 -0
  79. package/dist/math/round.d.ts +16 -0
  80. package/dist/math/round.spec.d.ts +1 -0
  81. package/dist/math/sum.d.ts +14 -0
  82. package/dist/math/sum.spec.d.ts +1 -0
  83. package/dist/object/index.d.ts +4 -0
  84. package/dist/object/index.js +286 -0
  85. package/dist/object/omit.d.ts +16 -0
  86. package/dist/object/omit.spec.d.ts +1 -0
  87. package/dist/object/omitBy.d.ts +19 -0
  88. package/dist/object/omitBy.spec.d.ts +1 -0
  89. package/dist/object/pick.d.ts +16 -0
  90. package/dist/object/pick.spec.d.ts +1 -0
  91. package/dist/object/pickBy.d.ts +19 -0
  92. package/dist/object/pickBy.spec.d.ts +1 -0
  93. package/dist/predicate/index.d.ts +4 -0
  94. package/dist/predicate/index.js +98 -0
  95. package/dist/predicate/isNil.d.ts +20 -0
  96. package/dist/predicate/isNil.spec.d.ts +1 -0
  97. package/dist/predicate/isNotNil.d.ts +16 -0
  98. package/dist/predicate/isNotNil.spec.d.ts +1 -0
  99. package/dist/predicate/isNull.d.ts +21 -0
  100. package/dist/predicate/isNull.spec.d.ts +1 -0
  101. package/dist/predicate/isUndefined.d.ts +21 -0
  102. package/dist/predicate/isUndefined.spec.d.ts +1 -0
  103. package/dist/promise/delay.d.ts +19 -0
  104. package/dist/promise/delay.spec.d.ts +1 -0
  105. package/dist/promise/index.d.ts +1 -0
  106. package/dist/promise/index.js +29 -0
  107. package/esm/_virtual/_rollupPluginBabelHelpers.mjs +163 -0
  108. package/esm/array/_virtual/_rollupPluginBabelHelpers.mjs +78 -0
  109. package/esm/array/chunk.mjs +38 -0
  110. package/esm/array/difference.mjs +29 -0
  111. package/esm/array/differenceBy.mjs +35 -0
  112. package/esm/array/differenceWith.mjs +30 -0
  113. package/esm/array/drop.mjs +20 -0
  114. package/esm/array/dropRight.mjs +20 -0
  115. package/esm/array/dropRightWhile.mjs +26 -0
  116. package/esm/array/dropWhile.mjs +25 -0
  117. package/esm/array/groupBy.mjs +54 -0
  118. package/esm/array/index.mjs +30 -0
  119. package/esm/array/intersection.mjs +24 -0
  120. package/esm/array/intersectionBy.mjs +30 -0
  121. package/esm/array/intersectionWith.mjs +30 -0
  122. package/esm/array/partition.mjs +46 -0
  123. package/esm/array/sample.mjs +19 -0
  124. package/esm/array/shuffle.mjs +27 -0
  125. package/esm/array/take.mjs +27 -0
  126. package/esm/array/takeRight.mjs +28 -0
  127. package/esm/array/takeRightWhile.mjs +27 -0
  128. package/esm/array/takeWhile.mjs +41 -0
  129. package/esm/array/union.mjs +23 -0
  130. package/esm/array/unionBy.mjs +35 -0
  131. package/esm/array/unionWith.mjs +26 -0
  132. package/esm/array/uniq.mjs +37 -0
  133. package/esm/array/uniqBy.mjs +20 -0
  134. package/esm/array/uniqWith.mjs +39 -0
  135. package/esm/array/xor.mjs +25 -0
  136. package/esm/array/xorBy.mjs +30 -0
  137. package/esm/array/xorWith.mjs +29 -0
  138. package/esm/array/zip.mjs +52 -0
  139. package/esm/array/zipWith.mjs +54 -0
  140. package/esm/function/debounce.mjs +39 -0
  141. package/esm/function/index.mjs +3 -0
  142. package/esm/function/once.mjs +31 -0
  143. package/esm/function/throttle.mjs +38 -0
  144. package/esm/index.mjs +45 -0
  145. package/esm/math/_virtual/_rollupPluginBabelHelpers.mjs +66 -0
  146. package/esm/math/clamp.mjs +26 -0
  147. package/esm/math/index.mjs +3 -0
  148. package/esm/math/round.mjs +22 -0
  149. package/esm/math/sum.mjs +33 -0
  150. package/esm/object/_virtual/_rollupPluginBabelHelpers.mjs +151 -0
  151. package/esm/object/index.mjs +4 -0
  152. package/esm/object/omit.mjs +35 -0
  153. package/esm/object/omitBy.mjs +35 -0
  154. package/esm/object/pick.mjs +35 -0
  155. package/esm/object/pickBy.mjs +35 -0
  156. package/esm/predicate/index.mjs +4 -0
  157. package/esm/predicate/isNil.mjs +24 -0
  158. package/esm/predicate/isNotNil.mjs +20 -0
  159. package/esm/predicate/isNull.mjs +25 -0
  160. package/esm/predicate/isUndefined.mjs +25 -0
  161. package/esm/promise/delay.mjs +25 -0
  162. package/esm/promise/index.mjs +1 -0
  163. package/package.json +108 -0
package/dist/index.js ADDED
@@ -0,0 +1,1514 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /**
6
+ * Splits an array into smaller arrays of a specified length.
7
+ *
8
+ * This function takes an input array and divides it into multiple smaller arrays,
9
+ * each of a specified length. If the input array cannot be evenly divided,
10
+ * the final sub-array will contain the remaining elements.
11
+ *
12
+ * @template T The type of elements in the array.
13
+ * @param {T[]} arr - The array to be chunked into smaller arrays.
14
+ * @param {number} size - The size of each smaller array. Must be a positive integer.
15
+ * @returns {T[][]} A two-dimensional array where each sub-array has a maximum length of `size`.
16
+ * @throws {Error} Throws an error if `size` is not a positive integer.
17
+ *
18
+ * @example
19
+ * // Splits an array of numbers into sub-arrays of length 2
20
+ * chunk([1, 2, 3, 4, 5], 2);
21
+ * // Returns: [[1, 2], [3, 4], [5]]
22
+ *
23
+ * @example
24
+ * // Splits an array of strings into sub-arrays of length 3
25
+ * chunk(['a', 'b', 'c', 'd', 'e', 'f', 'g'], 3);
26
+ * // Returns: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g']]
27
+ */
28
+ function chunk(arr, size) {
29
+ if (!Number.isInteger(size) || size <= 0) {
30
+ throw new Error('Size must be an integer greater than zero.');
31
+ }
32
+ var chunkLength = Math.ceil(arr.length / size);
33
+ var result = Array(chunkLength);
34
+ for (var index = 0; index < chunkLength; index++) {
35
+ var start = index * size;
36
+ var end = start + size;
37
+ result[index] = arr.slice(start, end);
38
+ }
39
+ return result;
40
+ }
41
+
42
+ /**
43
+ * Computes the difference between two arrays.
44
+ *
45
+ * This function takes two arrays and returns a new array containing the elements
46
+ * that are present in the first array but not in the second array. It effectively
47
+ * filters out any elements from the first array that also appear in the second array.
48
+ *
49
+ * @template T
50
+ * @param {T[]} firstArr - The array from which to derive the difference. This is the primary array
51
+ * from which elements will be compared and filtered.
52
+ * @param {T[]} secondArr - The array containing elements to be excluded from the first array.
53
+ * Each element in this array will be checked against the first array, and if a match is found,
54
+ * that element will be excluded from the result.
55
+ * @returns {T[]} A new array containing the elements that are present in the first array but not
56
+ * in the second array.
57
+ *
58
+ * @example
59
+ * * const array1 = [1, 2, 3, 4, 5];
60
+ * const array2 = [2, 4];
61
+ * const result = difference(array1, array2);
62
+ * // result will be [1, 3, 5] since 2 and 4 are in both arrays and are excluded from the result.
63
+ */
64
+ function difference(firstArr, secondArr) {
65
+ return firstArr.filter(function (item) {
66
+ return !secondArr.includes(item);
67
+ });
68
+ }
69
+
70
+ /**
71
+ * Computes the difference between two arrays after mapping their elements through a provided function.
72
+ *
73
+ * This function takes two arrays and a mapper function. It returns a new array containing the elements
74
+ * that are present in the first array but not in the second array, based on the identity calculated
75
+ * by the mapper function.
76
+ *
77
+ * Essentially, it filters out any elements from the first array that, when
78
+ * mapped, match an element in the mapped version of the second array.
79
+ *
80
+ * @template T, U
81
+ * @param {T[]} firstArr - The primary array from which to derive the difference.
82
+ * @param {T[]} secondArr - The array containing elements to be excluded from the first array.
83
+ * @param {(value: T) => U} mapper - The function to map the elements of both arrays. This function
84
+ * is applied to each element in both arrays, and the comparison is made based on the mapped values.
85
+ * @returns {T[]} A new array containing the elements from the first array that do not have a corresponding
86
+ * mapped identity in the second array.
87
+ *
88
+ * @example
89
+ * * const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }];
90
+ * const array2 = [{ id: 2 }, { id: 4 }];
91
+ * const mapper = item => item.id;
92
+ * const result = differenceBy(array1, array2, mapper);
93
+ * // result will be [{ id: 1 }, { id: 3 }] since the elements with id 2 are in both arrays and are excluded from the result.
94
+ */
95
+ function differenceBy(firstArr, secondArr, mapper) {
96
+ var mappedSecondArr = secondArr.map(function (item) {
97
+ return mapper(item);
98
+ });
99
+ return firstArr.filter(function (item) {
100
+ return !mappedSecondArr.includes(mapper(item));
101
+ });
102
+ }
103
+
104
+ /**
105
+ * Computes the difference between two arrays based on a custom equality function.
106
+ *
107
+ * This function takes two arrays and a custom comparison function. It returns a new array containing
108
+ * the elements that are present in the first array but not in the second array. The comparison to determine
109
+ * if elements are equal is made using the provided custom function.
110
+ *
111
+ * @template T
112
+ * @param {T[]} firstArr - The array from which to get the difference.
113
+ * @param {T[]} secondArr - The array containing elements to exclude from the first array.
114
+ * @param {(x: T, y: T) => boolean} areItemsEqual - A function to determine if two items are equal.
115
+ * @returns {T[]} A new array containing the elements from the first array that do not match any elements in the second array
116
+ * according to the custom equality function.
117
+ *
118
+ * @example
119
+ * * const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }];
120
+ * const array2 = [{ id: 2 }, { id: 4 }];
121
+ * const areItemsEqual = (a, b) => a.id === b.id;
122
+ * const result = differenceWith(array1, array2, areItemsEqual);
123
+ * // result will be [{ id: 1 }, { id: 3 }] since the elements with id 2 are considered equal and are excluded from the result.
124
+ */
125
+ function differenceWith(firstArr, secondArr, areItemsEqual) {
126
+ return firstArr.filter(function (firstItem) {
127
+ return secondArr.every(function (secondItem) {
128
+ return !areItemsEqual(firstItem, secondItem);
129
+ });
130
+ });
131
+ }
132
+
133
+ /**
134
+ * Removes a specified number of elements from the beginning of an array and returns the rest.
135
+ *
136
+ * This function takes an array and a number, and returns a new array with the specified number
137
+ * of elements removed from the start.
138
+ *
139
+ * @param {T[]} arr - The array from which to drop elements.
140
+ * @param {number} itemsCount - The number of elements to drop from the beginning of the array.
141
+ * @returns {T[]} A new array with the specified number of elements removed from the start.
142
+ *
143
+ * @example
144
+ * * const array = [1, 2, 3, 4, 5];
145
+ * const result = drop(array, 2);
146
+ * // result will be [3, 4, 5] since the first two elements are dropped.
147
+ */
148
+ function drop(arr, itemsCount) {
149
+ return arr.slice(itemsCount);
150
+ }
151
+
152
+ /**
153
+ * Removes a specified number of elements from the end of an array and returns the rest.
154
+ *
155
+ * This function takes an array and a number, and returns a new array with the specified number
156
+ * of elements removed from the end.
157
+ *
158
+ * @param {T[]} arr - The array from which to drop elements.
159
+ * @param {number} itemsCount - The number of elements to drop from the end of the array.
160
+ * @returns {T[]} A new array with the specified number of elements removed from the end.
161
+ *
162
+ * @example
163
+ * * const array = [1, 2, 3, 4, 5];
164
+ * const result = dropRight(array, 2);
165
+ * // result will be [1, 2, 3] since the last two elements are dropped.
166
+ */
167
+ function dropRight(arr, itemsCount) {
168
+ return arr.slice(0, -itemsCount);
169
+ }
170
+
171
+ /**
172
+ * Removes elements from the beginning of an array until the predicate returns false.
173
+ *
174
+ * This function iterates over an array and drops elements from the start until the provided
175
+ * predicate function returns false. It then returns a new array with the remaining elements.
176
+ *
177
+ * @param {T[]} arr - The array from which to drop elements.
178
+ * @param {(item: T) => boolean} canContinueDropping - A predicate function that determines
179
+ * whether to continue dropping elements. The function is called with each element, and dropping
180
+ * continues as long as it returns true.
181
+ * @returns {T[]} A new array with the elements remaining after the predicate returns false.
182
+ *
183
+ * @example
184
+ * const array = [1, 2, 3, 4, 5];
185
+ * const result = dropWhile(array, x => x < 3);
186
+ * // result will be [3, 4, 5] since elements less than 3 are dropped.
187
+ */
188
+ function dropWhile(arr, canContinueDropping) {
189
+ var dropEndIndex = arr.findIndex(function (item) {
190
+ return !canContinueDropping(item);
191
+ });
192
+ return arr.slice(dropEndIndex);
193
+ }
194
+
195
+ /**
196
+ * Removes elements from the end of an array until the predicate returns false.
197
+ *
198
+ * This function iterates over an array from the end and drops elements until the provided
199
+ * predicate function returns false. It then returns a new array with the remaining elements.
200
+ *
201
+ * @param {T[]} arr - The array from which to drop elements.
202
+ * @param {function(item: T): boolean} canContinueDropping - A predicate function that determines
203
+ * whether to continue dropping elements. The function is called with each element from the end,
204
+ * and dropping continues as long as it returns true.
205
+ * @returns {T[]} A new array with the elements remaining after the predicate returns false.
206
+ *
207
+ * @example
208
+ * const array = [1, 2, 3, 4, 5];
209
+ * const result = dropRightWhile(array, x => x > 3);
210
+ * // result will be [1, 2, 3] since elements greater than 3 are dropped from the end.
211
+ */
212
+ function dropRightWhile(arr, canContinueDropping) {
213
+ var reversed = arr.slice().reverse();
214
+ var dropped = dropWhile(reversed, canContinueDropping);
215
+ return dropped.slice().reverse();
216
+ }
217
+
218
+ function _iterableToArrayLimit(r, l) {
219
+ var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
220
+ if (null != t) {
221
+ var e,
222
+ n,
223
+ i,
224
+ u,
225
+ a = [],
226
+ f = !0,
227
+ o = !1;
228
+ try {
229
+ if (i = (t = t.call(r)).next, 0 === l) {
230
+ if (Object(t) !== t) return;
231
+ f = !1;
232
+ } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
233
+ } catch (r) {
234
+ o = !0, n = r;
235
+ } finally {
236
+ try {
237
+ if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
238
+ } finally {
239
+ if (o) throw n;
240
+ }
241
+ }
242
+ return a;
243
+ }
244
+ }
245
+ function ownKeys(e, r) {
246
+ var t = Object.keys(e);
247
+ if (Object.getOwnPropertySymbols) {
248
+ var o = Object.getOwnPropertySymbols(e);
249
+ r && (o = o.filter(function (r) {
250
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
251
+ })), t.push.apply(t, o);
252
+ }
253
+ return t;
254
+ }
255
+ function _objectSpread2(e) {
256
+ for (var r = 1; r < arguments.length; r++) {
257
+ var t = null != arguments[r] ? arguments[r] : {};
258
+ r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
259
+ _defineProperty(e, r, t[r]);
260
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
261
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
262
+ });
263
+ }
264
+ return e;
265
+ }
266
+ function _toPrimitive(t, r) {
267
+ if ("object" != typeof t || !t) return t;
268
+ var e = t[Symbol.toPrimitive];
269
+ if (void 0 !== e) {
270
+ var i = e.call(t, r || "default");
271
+ if ("object" != typeof i) return i;
272
+ throw new TypeError("@@toPrimitive must return a primitive value.");
273
+ }
274
+ return ("string" === r ? String : Number)(t);
275
+ }
276
+ function _toPropertyKey(t) {
277
+ var i = _toPrimitive(t, "string");
278
+ return "symbol" == typeof i ? i : i + "";
279
+ }
280
+ function _defineProperty(obj, key, value) {
281
+ key = _toPropertyKey(key);
282
+ if (key in obj) {
283
+ Object.defineProperty(obj, key, {
284
+ value: value,
285
+ enumerable: true,
286
+ configurable: true,
287
+ writable: true
288
+ });
289
+ } else {
290
+ obj[key] = value;
291
+ }
292
+ return obj;
293
+ }
294
+ function _slicedToArray(arr, i) {
295
+ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
296
+ }
297
+ function _toConsumableArray(arr) {
298
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
299
+ }
300
+ function _arrayWithoutHoles(arr) {
301
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
302
+ }
303
+ function _arrayWithHoles(arr) {
304
+ if (Array.isArray(arr)) return arr;
305
+ }
306
+ function _iterableToArray(iter) {
307
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
308
+ }
309
+ function _unsupportedIterableToArray(o, minLen) {
310
+ if (!o) return;
311
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
312
+ var n = Object.prototype.toString.call(o).slice(8, -1);
313
+ if (n === "Object" && o.constructor) n = o.constructor.name;
314
+ if (n === "Map" || n === "Set") return Array.from(o);
315
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
316
+ }
317
+ function _arrayLikeToArray(arr, len) {
318
+ if (len == null || len > arr.length) len = arr.length;
319
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
320
+ return arr2;
321
+ }
322
+ function _nonIterableSpread() {
323
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
324
+ }
325
+ function _nonIterableRest() {
326
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
327
+ }
328
+ function _createForOfIteratorHelper(o, allowArrayLike) {
329
+ var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
330
+ if (!it) {
331
+ if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
332
+ if (it) o = it;
333
+ var i = 0;
334
+ var F = function () {};
335
+ return {
336
+ s: F,
337
+ n: function () {
338
+ if (i >= o.length) return {
339
+ done: true
340
+ };
341
+ return {
342
+ done: false,
343
+ value: o[i++]
344
+ };
345
+ },
346
+ e: function (e) {
347
+ throw e;
348
+ },
349
+ f: F
350
+ };
351
+ }
352
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
353
+ }
354
+ var normalCompletion = true,
355
+ didErr = false,
356
+ err;
357
+ return {
358
+ s: function () {
359
+ it = it.call(o);
360
+ },
361
+ n: function () {
362
+ var step = it.next();
363
+ normalCompletion = step.done;
364
+ return step;
365
+ },
366
+ e: function (e) {
367
+ didErr = true;
368
+ err = e;
369
+ },
370
+ f: function () {
371
+ try {
372
+ if (!normalCompletion && it.return != null) it.return();
373
+ } finally {
374
+ if (didErr) throw err;
375
+ }
376
+ }
377
+ };
378
+ }
379
+
380
+ /**
381
+ * Groups the elements of an array based on a provided key-generating function.
382
+ *
383
+ * This function takes an array and a function that generates a key from each element. It returns
384
+ * an object where the keys are the generated keys and the values are arrays of elements that share
385
+ * the same key.
386
+ *
387
+ * @param {T[]} arr - The array to group.
388
+ * @param {(item: T) => K} getKeyFromItem - A function that generates a key from an element.
389
+ * @returns {Record<K, T[]>} An object where each key is associated with an array of elements that
390
+ * share that key.
391
+ *
392
+ * @example
393
+ * const array = [
394
+ * { category: 'fruit', name: 'apple' },
395
+ * { category: 'fruit', name: 'banana' },
396
+ * { category: 'vegetable', name: 'carrot' }
397
+ * ];
398
+ * const result = groupBy(array, item => item.category);
399
+ * // result will be:
400
+ * // {
401
+ * // fruit: [
402
+ * // { category: 'fruit', name: 'apple' },
403
+ * // { category: 'fruit', name: 'banana' }
404
+ * // ],
405
+ * // vegetable: [
406
+ * // { category: 'vegetable', name: 'carrot' }
407
+ * // ]
408
+ * // }
409
+ */
410
+ function groupBy(arr, getKeyFromItem) {
411
+ var result = {};
412
+ var _iterator = _createForOfIteratorHelper(arr),
413
+ _step;
414
+ try {
415
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
416
+ var item = _step.value;
417
+ var key = getKeyFromItem(item);
418
+ if (result[key] == null) {
419
+ result[key] = [];
420
+ }
421
+ result[key].push(item);
422
+ }
423
+ } catch (err) {
424
+ _iterator.e(err);
425
+ } finally {
426
+ _iterator.f();
427
+ }
428
+ return result;
429
+ }
430
+
431
+ /**
432
+ * Returns the intersection of two arrays.
433
+ *
434
+ * This function takes two arrays and returns a new array containing the elements that are
435
+ * present in both arrays. It effectively filters out any elements from the first array that
436
+ * are not found in the second array.
437
+ *
438
+ * @param {T[]} firstArr - The first array to compare.
439
+ * @param {T[]} secondArr - The second array to compare.
440
+ * @returns {T[]} A new array containing the elements that are present in both arrays.
441
+ *
442
+ * @example
443
+ * const array1 = [1, 2, 3, 4, 5];
444
+ * const array2 = [3, 4, 5, 6, 7];
445
+ * const result = intersection(array1, array2);
446
+ * // result will be [3, 4, 5] since these elements are in both arrays.
447
+ */
448
+ function intersection(firstArr, secondArr) {
449
+ return firstArr.filter(function (item) {
450
+ return secondArr.includes(item);
451
+ });
452
+ }
453
+
454
+ /**
455
+ * Returns the intersection of two arrays based on a mapping function.
456
+ *
457
+ * This function takes two arrays and a mapping function. It returns a new array containing
458
+ * the elements from the first array that, when mapped using the provided function, have matching
459
+ * mapped elements in the second array. It effectively filters out any elements from the first array
460
+ * that do not have corresponding mapped values in the second array.
461
+ *
462
+ * @param {T[]} firstArr - The first array to compare.
463
+ * @param {T[]} secondArr - The second array to compare.
464
+ * @param {(item: T) => U} mapper - A function to map the elements of both arrays for comparison.
465
+ * @returns {T[]} A new array containing the elements from the first array that have corresponding mapped values in the second array.
466
+ *
467
+ * @example
468
+ * const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }];
469
+ * const array2 = [{ id: 2 }, { id: 4 }];
470
+ * const mapper = item => item.id;
471
+ * const result = intersectionBy(array1, array2, mapper);
472
+ * // result will be [{ id: 2 }] since only this element has a matching id in both arrays.
473
+ */
474
+ function intersectionBy(firstArr, secondArr, mapper) {
475
+ var mappedSecondArr = secondArr.map(function (x) {
476
+ return mapper(x);
477
+ });
478
+ return firstArr.filter(function (item) {
479
+ return mappedSecondArr.includes(mapper(item));
480
+ });
481
+ }
482
+
483
+ /**
484
+ * Returns the intersection of two arrays based on a custom equality function.
485
+ *
486
+ * This function takes two arrays and a custom equality function. It returns a new array containing
487
+ * the elements from the first array that have matching elements in the second array, as determined
488
+ * by the custom equality function. It effectively filters out any elements from the first array that
489
+ * do not have corresponding matches in the second array according to the equality function.
490
+ *
491
+ * @param {T[]} firstArr - The first array to compare.
492
+ * @param {T[]} secondArr - The second array to compare.
493
+ * @param {(x: T, y: T) => boolean} areItemsEqual - A custom function to determine if two elements are equal.
494
+ * This function takes two arguments, one from each array, and returns `true` if the elements are considered equal, and `false` otherwise.
495
+ * @returns {T[]} A new array containing the elements from the first array that have corresponding matches in the second array according to the custom equality function.
496
+ *
497
+ * @example
498
+ * * const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }];
499
+ * const array2 = [{ id: 2 }, { id: 4 }];
500
+ * const areItemsEqual = (a, b) => a.id === b.id;
501
+ * const result = intersectionWith(array1, array2, areItemsEqual);
502
+ * // result will be [{ id: 2 }] since this element has a matching id in both arrays.
503
+ */
504
+ function intersectionWith(firstArr, secondArr, areItemsEqual) {
505
+ return firstArr.filter(function (firstItem) {
506
+ return secondArr.some(function (secondItem) {
507
+ return areItemsEqual(firstItem, secondItem);
508
+ });
509
+ });
510
+ }
511
+
512
+ /**
513
+ * Splits an array into two groups based on a predicate function.
514
+ *
515
+ * This function takes an array and a predicate function. It returns a tuple of two arrays:
516
+ * the first array contains elements for which the predicate function returns true, and
517
+ * the second array contains elements for which the predicate function returns false.
518
+ *
519
+ * @param {T[]} arr - The array to partition.
520
+ * @param {(value: T) => boolean} isInTruthy - A predicate function that determines
521
+ * whether an element should be placed in the truthy array. The function is called with each
522
+ * element of the array.
523
+ * @returns {[T[], T[]]} A tuple containing two arrays: the first array contains elements for
524
+ * which the predicate returned true, and the second array contains elements for which the
525
+ * predicate returned false.
526
+ *
527
+ * @example
528
+ * const array = [1, 2, 3, 4, 5];
529
+ * const isEven = x => x % 2 === 0;
530
+ * const [even, odd] = partition(array, isEven);
531
+ * // even will be [2, 4], and odd will be [1, 3, 5]
532
+ */
533
+ function partition(arr, isInTruthy) {
534
+ var truthy = [];
535
+ var falsy = [];
536
+ var _iterator = _createForOfIteratorHelper(arr),
537
+ _step;
538
+ try {
539
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
540
+ var item = _step.value;
541
+ if (isInTruthy(item)) {
542
+ truthy.push(item);
543
+ } else {
544
+ falsy.push(item);
545
+ }
546
+ }
547
+ } catch (err) {
548
+ _iterator.e(err);
549
+ } finally {
550
+ _iterator.f();
551
+ }
552
+ return [truthy, falsy];
553
+ }
554
+
555
+ /**
556
+ * Returns a random element from an array.
557
+ *
558
+ * This function takes an array and returns a single element selected randomly from the array.
559
+ *
560
+ * @param {T[]} arr - The array to sample from.
561
+ * @returns {T} A random element from the array.
562
+ *
563
+ * @example
564
+ * const array = [1, 2, 3, 4, 5];
565
+ * const randomElement = sample(array);
566
+ * // randomElement will be one of the elements from the array, selected randomly.
567
+ */
568
+ function sample(arr) {
569
+ var randomIndex = Math.floor(Math.random() * arr.length);
570
+ return arr[randomIndex];
571
+ }
572
+
573
+ /**
574
+ * Randomizes the order of elements in an array using the Fisher-Yates algorithm.
575
+ *
576
+ * This function takes an array and returns a new array with its elements shuffled in a random order.
577
+ *
578
+ * @param {T[]} arr - The array to shuffle.
579
+ * @returns {T[]} A new array with its elements shuffled in random order.
580
+ *
581
+ * @example
582
+ * const array = [1, 2, 3, 4, 5];
583
+ * const shuffledArray = shuffle(array);
584
+ * // shuffledArray will be a new array with elements of array in random order, e.g., [3, 1, 4, 5, 2]
585
+ */
586
+ function shuffle(arr) {
587
+ /**
588
+ * https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
589
+ */
590
+ for (var i = arr.length - 1; i >= 1; i--) {
591
+ var j = Math.floor(Math.random() * (i + 1));
592
+ var _ref = [arr[j], arr[i]];
593
+ arr[i] = _ref[0];
594
+ arr[j] = _ref[1];
595
+ }
596
+ return arr;
597
+ }
598
+
599
+ /**
600
+ * Returns a new array containing the first `count` elements from the input array `arr`.
601
+ * If `count` is greater than the length of `arr`, the entire array is returned.
602
+ *
603
+ * @template T - Type of elements in the input array.
604
+ *
605
+ * @param {T[]} arr - The array to take elements from.
606
+ * @param {number} count - The number of elements to take.
607
+ * @returns {T[]} A new array containing the first `count` elements from `arr`.
608
+ *
609
+ * @example
610
+ * // Returns [1, 2, 3]
611
+ * take([1, 2, 3, 4, 5], 3);
612
+ *
613
+ * @example
614
+ * // Returns ['a', 'b']
615
+ * take(['a', 'b', 'c'], 2);
616
+ *
617
+ * @example
618
+ * // Returns [1, 2, 3]
619
+ * take([1, 2, 3], 5);
620
+ */
621
+ function take(arr, count) {
622
+ return arr.slice(0, count);
623
+ }
624
+
625
+ /**
626
+ * Returns a new array containing the last `count` elements from the input array `arr`.
627
+ * If `count` is greater than the length of `arr`, the entire array is returned.
628
+ *
629
+ * @param {T[]} arr - The array to take elements from.
630
+ * @param {number} count - The number of elements to take.
631
+ * @returns {T[]} A new array containing the last `count` elements from `arr`.
632
+ *
633
+ * @example
634
+ * // Returns [4, 5]
635
+ * takeRight([1, 2, 3, 4, 5], 2);
636
+ *
637
+ * @example
638
+ * // Returns ['b', 'c']
639
+ * takeRight(['a', 'b', 'c'], 2);
640
+ *
641
+ * @example
642
+ * // Returns [1, 2, 3]
643
+ * takeRight([1, 2, 3], 5);
644
+ */
645
+ function takeRight(arr, count) {
646
+ if (count === 0) {
647
+ return [];
648
+ }
649
+ return arr.slice(-count);
650
+ }
651
+
652
+ /**
653
+ * Takes elements from the end of the array while the predicate function returns `true`.
654
+ *
655
+ * @template T - Type of elements in the input array.
656
+ *
657
+ * @param {T[]} arr - The array to take elements from.
658
+ * @param {function(T): boolean} shouldContinueTaking - The function invoked per element.
659
+ * @returns {T[]} A new array containing the elements taken from the end while the predicate returns `true`.
660
+ *
661
+ * @example
662
+ * // Returns [3, 2, 1]
663
+ * takeRightWhile([5, 4, 3, 2, 1], n => n < 4);
664
+ *
665
+ * @example
666
+ * // Returns []
667
+ * takeRightWhile([1, 2, 3], n => n > 3);
668
+ */
669
+ function takeRightWhile(arr, shouldContinueTaking) {
670
+ for (var i = arr.length - 1; i >= 0; i--) {
671
+ if (!shouldContinueTaking(arr[i])) {
672
+ return arr.slice(i + 1);
673
+ }
674
+ }
675
+ return arr.slice();
676
+ }
677
+
678
+ /**
679
+ * Returns a new array containing the leading elements of the provided array
680
+ * that satisfy the provided predicate function. It stops taking elements as soon
681
+ * as an element does not satisfy the predicate.
682
+ *
683
+ * @template T - The type of elements in the array.
684
+ * @param {T[]} arr - The array to process.
685
+ * @param {(element: T) => boolean} shouldContinueTaking - The predicate function that is called with each element. Elements are included in the result as long as this function returns true.
686
+ * @returns {T[]} A new array containing the leading elements that satisfy the predicate.
687
+ *
688
+ * @example
689
+ * // Returns [1, 2]
690
+ * takeWhile([1, 2, 3, 4], x => x < 3);
691
+ *
692
+ * @example
693
+ * // Returns []
694
+ * takeWhile([1, 2, 3, 4], x => x > 3);
695
+ */
696
+ function takeWhile(arr, shouldContinueTaking) {
697
+ var result = [];
698
+ var _iterator = _createForOfIteratorHelper(arr),
699
+ _step;
700
+ try {
701
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
702
+ var item = _step.value;
703
+ if (!shouldContinueTaking(item)) {
704
+ break;
705
+ }
706
+ result.push(item);
707
+ }
708
+ } catch (err) {
709
+ _iterator.e(err);
710
+ } finally {
711
+ _iterator.f();
712
+ }
713
+ return result;
714
+ }
715
+
716
+ /**
717
+ * Creates a duplicate-free version of an array.
718
+ *
719
+ * This function takes an array and returns a new array containing only the unique values
720
+ * from the original array, preserving the order of first occurrence.
721
+ *
722
+ * @param {T[]} arr - The array to process.
723
+ * @returns {T[]} A new array with only unique values from the original array.
724
+ *
725
+ * @example
726
+ * * const array = [1, 2, 2, 3, 4, 4, 5];
727
+ * const result = uniq(array);
728
+ * // result will be [1, 2, 3, 4, 5]
729
+ */
730
+ function uniq(arr) {
731
+ var result = [];
732
+ var _iterator = _createForOfIteratorHelper(arr),
733
+ _step;
734
+ try {
735
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
736
+ var item = _step.value;
737
+ if (result.includes(item)) {
738
+ continue;
739
+ }
740
+ result.push(item);
741
+ }
742
+ } catch (err) {
743
+ _iterator.e(err);
744
+ } finally {
745
+ _iterator.f();
746
+ }
747
+ return result;
748
+ }
749
+
750
+ /**
751
+ * Creates an array of unique values from all given arrays.
752
+ *
753
+ * This function takes two arrays, merges them into a single array, and returns a new array
754
+ * containing only the unique values from the merged array.
755
+ *
756
+ * @param {T[]} arr1 - The first array to merge and filter for unique values.
757
+ * @param {T[]} arr2 - The second array to merge and filter for unique values.
758
+ * @returns {T[]} A new array of unique values.
759
+ *
760
+ * @example
761
+ * const array1 = [1, 2, 3];
762
+ * const array2 = [3, 4, 5];
763
+ * const result = union(array1, array2);
764
+ * // result will be [1, 2, 3, 4, 5]
765
+ */
766
+ function union(arr1, arr2) {
767
+ return uniq(arr1.concat(arr2));
768
+ }
769
+
770
+ /**
771
+ * Creates an array of unique values, in order, from all given arrays using a provided mapping function to determine equality.
772
+ *
773
+ * @param {T[]} arr1 - The first array.
774
+ * @param {T[]} arr2 - The second array.
775
+ * @param {function(item: T): U} mapper - The function to map array elements to comparison values.
776
+ * @returns {T[]} A new array containing the union of unique elements from `arr1` and `arr2`, based on the values returned by the mapping function.
777
+ *
778
+ * @example
779
+ * // Custom mapping function for numbers (modulo comparison)
780
+ * const moduloMapper = (x) => x % 3;
781
+ * unionBy([1, 2, 3], [4, 5, 6], moduloMapper);
782
+ * // Returns [1, 2, 3]
783
+ *
784
+ * @example
785
+ * // Custom mapping function for objects with an 'id' property
786
+ * const idMapper = (obj) => obj.id;
787
+ * unionBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], idMapper);
788
+ * // Returns [{ id: 1 }, { id: 2 }, { id: 3 }]
789
+ */
790
+ function unionBy(arr1, arr2, mapper) {
791
+ var map = new Map();
792
+ for (var _i = 0, _arr = [].concat(_toConsumableArray(arr1), _toConsumableArray(arr2)); _i < _arr.length; _i++) {
793
+ var item = _arr[_i];
794
+ var key = mapper(item);
795
+ if (!map.has(key)) {
796
+ map.set(key, item);
797
+ }
798
+ }
799
+ return _toConsumableArray(map.values());
800
+ }
801
+
802
+ /**
803
+ * The `uniqWith` function takes an array as its first argument and a 'comparator' function as the second.
804
+ *
805
+ * It evaluates the elements of the array using the comparator function, and if true is returned, it considers those elements as duplicates and removes them.
806
+ *
807
+ * @example
808
+ * ```ts
809
+ * uniqWith([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], (a, b) => );
810
+ * // [1, 2, 3, 5, 7]
811
+ * ```
812
+ */
813
+ function uniqWith(arr, areItemsEqual) {
814
+ var result = [];
815
+ var _iterator = _createForOfIteratorHelper(arr),
816
+ _step;
817
+ try {
818
+ var _loop = function _loop() {
819
+ var item = _step.value;
820
+ var isUniq = result.every(function (v) {
821
+ return !areItemsEqual(v, item);
822
+ });
823
+ if (isUniq) {
824
+ result.push(item);
825
+ }
826
+ };
827
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
828
+ _loop();
829
+ }
830
+ } catch (err) {
831
+ _iterator.e(err);
832
+ } finally {
833
+ _iterator.f();
834
+ }
835
+ return result;
836
+ }
837
+
838
+ /**
839
+ * Creates an array of unique values from two given arrays based on a custom equality function.
840
+ *
841
+ * This function takes two arrays and a custom equality function, merges the arrays, and returns
842
+ * a new array containing only the unique values as determined by the custom equality function.
843
+ *
844
+ * @param {T[]} arr1 - The first array to merge and filter for unique values.
845
+ * @param {T[]} arr2 - The second array to merge and filter for unique values.
846
+ * @param {(item1: T, item2: T) => boolean} areItemsEqual - A custom function to determine if two elements are equal.
847
+ * It takes two arguments and returns `true` if the elements are considered equal, and `false` otherwise.
848
+ * @returns {T[]} A new array of unique values based on the custom equality function.
849
+ *
850
+ * @example
851
+ * const array1 = [{ id: 1 }, { id: 2 }];
852
+ * const array2 = [{ id: 2 }, { id: 3 }];
853
+ * const areItemsEqual = (a, b) => a.id === b.id;
854
+ * const result = unionWith(array1, array2, areItemsEqual);
855
+ * // result will be [{ id: 1 }, { id: 2 }, { id: 3 }] since { id: 2 } is considered equal in both arrays
856
+ */
857
+ function unionWith(arr1, arr2, areItemsEqual) {
858
+ return uniqWith(arr1.concat(arr2), areItemsEqual);
859
+ }
860
+
861
+ /**
862
+ * The `uniqBy` function takes an array as its first argument and a 'converter' function as the second. It maps the array elements using the converter function, then removes any duplicates.
863
+ *
864
+ * It filters out elements with the same value, meaning it does not check for duplicates in data types like Objects.
865
+ *
866
+ * @example
867
+ * ```ts
868
+ * uniqBy([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], Math.floor);
869
+ * // [1.2, 2.1, 3.3, 5.7, 7.19]
870
+ * ```
871
+ */
872
+ function uniqBy(arr, converter) {
873
+ return uniqWith(arr, function (item1, item2) {
874
+ return converter(item1) === converter(item2);
875
+ });
876
+ }
877
+
878
+ /**
879
+ * Computes the symmetric difference between two arrays. The symmetric difference is the set of elements
880
+ * which are in either of the arrays, but not in their intersection.
881
+ *
882
+ * @param {T[]} arr1 - The first array.
883
+ * @param {T[]} arr2 - The second array.
884
+ * @returns {T[]} An array containing the elements that are present in either `arr1` or `arr2` but not in both.
885
+ *
886
+ * @example
887
+ * // Returns [1, 2, 5, 6]
888
+ * xor([1, 2, 3, 4], [3, 4, 5, 6]);
889
+ *
890
+ * @example
891
+ * // Returns ['a', 'c']
892
+ * xor(['a', 'b'], ['b', 'c']);
893
+ */
894
+ function xor(arr1, arr2) {
895
+ return difference(union(arr1, arr2), intersection(arr1, arr2));
896
+ }
897
+
898
+ /**
899
+ * Computes the symmetric difference between two arrays using a custom mapping function.
900
+ * The symmetric difference is the set of elements which are in either of the arrays,
901
+ * but not in their intersection, determined by the result of the mapping function.
902
+ *
903
+ * @template T - Type of elements in the input arrays.
904
+ * @template U - Type of the values returned by the mapping function.
905
+ *
906
+ * @param {T[]} arr1 - The first array.
907
+ * @param {T[]} arr2 - The second array.
908
+ * @param {function(item: T): U} mapper - The function to map array elements to comparison values.
909
+ * @returns {T[]} An array containing the elements that are present in either `arr1` or `arr2` but not in both, based on the values returned by the mapping function.
910
+ *
911
+ * @example
912
+ * // Custom mapping function for objects with an 'id' property
913
+ * const idMapper = obj => obj.id;
914
+ * xorBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], idMapper);
915
+ * // Returns [{ id: 1 }, { id: 3 }]
916
+ */
917
+ function xorBy(arr1, arr2, mapper) {
918
+ var union = unionBy(arr1, arr2, mapper);
919
+ var intersection = intersectionBy(arr1, arr2, mapper);
920
+ return differenceBy(union, intersection, mapper);
921
+ }
922
+
923
+ /**
924
+ * Computes the symmetric difference between two arrays using a custom equality function.
925
+ * The symmetric difference is the set of elements which are in either of the arrays,
926
+ * but not in their intersection.
927
+ *
928
+ * @template T - Type of elements in the input arrays.
929
+ *
930
+ * @param {T[]} arr1 - The first array.
931
+ * @param {T[]} arr2 - The second array.
932
+ * @param {(item1: T, item2: T) => boolean} areElementsEqual - The custom equality function to compare elements.
933
+ * @returns {T[]} An array containing the elements that are present in either `arr1` or `arr2` but not in both, based on the custom equality function.
934
+ *
935
+ * @example
936
+ * // Custom equality function for objects with an 'id' property
937
+ * const areObjectsEqual = (a, b) => a.id === b.id;
938
+ * xorWith([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], areObjectsEqual);
939
+ * // Returns [{ id: 1 }, { id: 3 }]
940
+ */
941
+ function xorWith(arr1, arr2, areElementsEqual) {
942
+ var union = unionWith(arr1, arr2, areElementsEqual);
943
+ var intersection = intersectionWith(arr1, arr2, areElementsEqual);
944
+ return differenceWith(union, intersection, areElementsEqual);
945
+ }
946
+
947
+ /**
948
+ * Combines multiple arrays into a single array of tuples.
949
+ *
950
+ * This function takes multiple arrays and returns a new array where each element is a tuple
951
+ * containing the corresponding elements from the input arrays. If the input arrays are of
952
+ * different lengths, the resulting array will have the length of the longest input array,
953
+ * with undefined values for missing elements.
954
+ *
955
+ * @param {...T[][]} arrs - The arrays to zip together.
956
+ * @returns {T[][]} A new array of tuples containing the corresponding elements from the input arrays.
957
+ *
958
+ * @example
959
+ * const arr1 = [1, 2, 3];
960
+ * const arr2 = ['a', 'b', 'c'];
961
+ * const result = zip(arr1, arr2);
962
+ * // result will be [[1, 'a'], [2, 'b'], [3, 'c']]
963
+ *
964
+ * const arr3 = [true, false];
965
+ * const result2 = zip(arr1, arr2, arr3);
966
+ * // result2 will be [[1, 'a', true], [2, 'b', false], [3, 'c', undefined]]
967
+ */
968
+
969
+ function zip() {
970
+ var result = [];
971
+ for (var _len = arguments.length, arrs = new Array(_len), _key = 0; _key < _len; _key++) {
972
+ arrs[_key] = arguments[_key];
973
+ }
974
+ var maxIndex = Math.max.apply(Math, _toConsumableArray(arrs.map(function (x) {
975
+ return x.length;
976
+ })));
977
+ for (var i = 0; i < maxIndex; i++) {
978
+ var element = [];
979
+ var _iterator = _createForOfIteratorHelper(arrs),
980
+ _step;
981
+ try {
982
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
983
+ var arr = _step.value;
984
+ element.push(arr[i]);
985
+ }
986
+ } catch (err) {
987
+ _iterator.e(err);
988
+ } finally {
989
+ _iterator.f();
990
+ }
991
+ result.push(element);
992
+ }
993
+ return result;
994
+ }
995
+
996
+ /**
997
+ * Combines multiple arrays into a single array using a custom combiner function.
998
+ *
999
+ * This function takes multiple arrays and a combiner function, and returns a new array where each element
1000
+ * is the result of applying the combiner function to the corresponding elements of the input arrays.
1001
+ *
1002
+ * @param {T[]} arr1 - The first array to zip.
1003
+ * @param {U[]} [arr2] - The second array to zip (optional).
1004
+ * @param {V[]} [arr3] - The third array to zip (optional).
1005
+ * @param {W[]} [arr4] - The fourth array to zip (optional).
1006
+ * @param {function} combine - The combiner function that takes corresponding elements from each array and returns a single value.
1007
+ * @returns {R[]} A new array where each element is the result of applying the combiner function to the corresponding elements of the input arrays.
1008
+ *
1009
+ * @example
1010
+ * // Example usage with two arrays:
1011
+ * const arr1 = [1, 2, 3];
1012
+ * const arr2 = [4, 5, 6];
1013
+ * const result = zipWith(arr1, arr2, (a, b) => a + b);
1014
+ * // result will be [5, 7, 9]
1015
+ *
1016
+ * @example
1017
+ * // Example usage with three arrays:
1018
+ * const arr1 = [1, 2];
1019
+ * const arr2 = [3, 4];
1020
+ * const arr3 = [5, 6];
1021
+ * const result = zipWith(arr1, arr2, arr3, (a, b, c) => `${a}${b}${c}`);
1022
+ * // result will be [`135`, `246`]
1023
+ */
1024
+
1025
+ function zipWith(arr1) {
1026
+ for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
1027
+ rest[_key - 1] = arguments[_key];
1028
+ }
1029
+ var arrs = [arr1].concat(_toConsumableArray(rest.slice(0, -1)));
1030
+ var combine = rest[rest.length - 1];
1031
+ var result = [];
1032
+ var maxIndex = Math.max.apply(Math, _toConsumableArray(arrs.map(function (arr) {
1033
+ return arr.length;
1034
+ })));
1035
+ var _loop = function _loop(i) {
1036
+ var elements = arrs.map(function (arr) {
1037
+ return arr[i];
1038
+ });
1039
+ result.push(combine.apply(void 0, _toConsumableArray(elements)));
1040
+ };
1041
+ for (var i = 0; i < maxIndex; i++) {
1042
+ _loop(i);
1043
+ }
1044
+ return result;
1045
+ }
1046
+
1047
+ /**
1048
+ * Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
1049
+ * have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
1050
+ * method to cancel any pending execution.
1051
+ *
1052
+ * @param {() => void} func - The function to debounce.
1053
+ * @param {number} debounceMs - The number of milliseconds to delay.
1054
+ * @returns {{ (): void; cancel: () => void }} A new debounced function with a `cancel` method.
1055
+ *
1056
+ * @example
1057
+ * const debouncedFunction = debounce(() => {
1058
+ * console.log('Function executed');
1059
+ * }, 1000);
1060
+ *
1061
+ * // Will log 'Function executed' after 1 second if not called again in that time
1062
+ * debouncedFunction();
1063
+ *
1064
+ * // Will not log anything as the previous call is canceled
1065
+ * debouncedFunction.cancel();
1066
+ */
1067
+ function debounce(func, debounceMs) {
1068
+ var timeoutId = null;
1069
+ var debounced = function debounced() {
1070
+ if (timeoutId != null) {
1071
+ clearTimeout(timeoutId);
1072
+ }
1073
+ timeoutId = setTimeout(function () {
1074
+ func();
1075
+ }, debounceMs);
1076
+ };
1077
+ debounced.cancel = function () {
1078
+ if (timeoutId != null) {
1079
+ clearTimeout(timeoutId);
1080
+ }
1081
+ };
1082
+ return debounced;
1083
+ }
1084
+
1085
+ /**
1086
+ * Creates a function that is restricted to invoking the provided function `func` once.
1087
+ * Repeated calls to the function will return the value from the first invocation.
1088
+ *
1089
+ * @param {F} func - The function to restrict.
1090
+ * @returns {F} A new function that invokes `func` once and caches the result.
1091
+ *
1092
+ * @example
1093
+ * const initialize = once(() => {
1094
+ * console.log('Initialized!');
1095
+ * return true;
1096
+ * });
1097
+ *
1098
+ * initialize(); // Logs: 'Initialized!' and returns true
1099
+ * initialize(); // Returns true without logging
1100
+ */
1101
+ function once(func) {
1102
+ var called = false;
1103
+ var cache;
1104
+ return function () {
1105
+ if (called) {
1106
+ return cache;
1107
+ }
1108
+ var result = func();
1109
+ called = true;
1110
+ cache = result;
1111
+ return result;
1112
+ };
1113
+ }
1114
+
1115
+ /**
1116
+ * Creates a throttled function that only invokes the provided function at most once
1117
+ * per every `throttleMs` milliseconds. Subsequent calls to the throttled function
1118
+ * within the wait time will not trigger the execution of the original function.
1119
+ *
1120
+ * @param {() => void} func - The function to throttle.
1121
+ * @param {number} throttleMs - The number of milliseconds to throttle executions to.
1122
+ * @returns {() => void} A new throttled function.
1123
+ *
1124
+ * @example
1125
+ * const throttledFunction = throttle(() => {
1126
+ * console.log('Function executed');
1127
+ * }, 1000);
1128
+ *
1129
+ * // Will log 'Function executed' immediately
1130
+ * throttledFunction();
1131
+ *
1132
+ * // Will not log anything as it is within the throttle time
1133
+ * throttledFunction();
1134
+ *
1135
+ * // After 1 second
1136
+ * setTimeout(() => {
1137
+ * throttledFunction(); // Will log 'Function executed'
1138
+ * }, 1000);
1139
+ */
1140
+ function throttle(func, throttleMs) {
1141
+ var lastCallTime;
1142
+ var throttledFunction = function throttledFunction() {
1143
+ var now = Date.now();
1144
+ if (lastCallTime == null || now - lastCallTime >= throttleMs) {
1145
+ lastCallTime = now;
1146
+ func();
1147
+ }
1148
+ };
1149
+ return throttledFunction;
1150
+ }
1151
+
1152
+ /**
1153
+ * Clamps a number within the inclusive lower and upper bounds.
1154
+ *
1155
+ * This function takes a number and two bounds, and returns the number clamped within the specified bounds.
1156
+ * If only one bound is provided, it returns the minimum of the value and the bound.
1157
+ *
1158
+ * @param {number} value - The number to clamp.
1159
+ * @param {number} minimum - The minimum bound to clamp the number.
1160
+ * @param {number} maximum - The maximum bound to clamp the number.
1161
+ * @returns {number} The clamped number within the specified bounds.
1162
+ *
1163
+ * @example
1164
+ * * const result1 = clamp(10, 5); // result1 will be 5, as 10 is clamped to the bound 5
1165
+ * const result2 = clamp(10, 5, 15); // result2 will be 10, as it is within the bounds 5 and 15
1166
+ * const result3 = clamp(2, 5, 15); // result3 will be 5, as 2 is clamped to the lower bound 5
1167
+ * const result4 = clamp(20, 5, 15); // result4 will be 15, as 20 is clamped to the upper bound 15
1168
+ */
1169
+
1170
+ function clamp(value, bound1, bound2) {
1171
+ if (bound2 == null) {
1172
+ return Math.min(value, bound1);
1173
+ }
1174
+ return Math.min(Math.max(value, bound1), bound2);
1175
+ }
1176
+
1177
+ /**
1178
+ * Rounds a number to a specified precision.
1179
+ *
1180
+ * This function takes a number and an optional precision value, and returns the number rounded
1181
+ * to the specified number of decimal places.
1182
+ *
1183
+ * @param {number} value - The number to round.
1184
+ * @param {number} [precision=0] - The number of decimal places to round to. Defaults to 0.
1185
+ * @returns {number} The rounded number.
1186
+ *
1187
+ * @example
1188
+ * * const result1 = round(1.2345); // result1 will be 1
1189
+ * const result2 = round(1.2345, 2); // result2 will be 1.23
1190
+ * const result3 = round(1.2345, 3); // result3 will be 1.235
1191
+ */
1192
+ function round(value) {
1193
+ var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
1194
+ var multiplier = Math.pow(10, precision);
1195
+ return Math.round(value * multiplier) / multiplier;
1196
+ }
1197
+
1198
+ /**
1199
+ * Calculates the sum of an array of numbers.
1200
+ *
1201
+ * This function takes an array of numbers and returns the sum of all the elements in the array.
1202
+ *
1203
+ * @param {number[]} nums - An array of numbers to be summed.
1204
+ * @returns {number} The sum of all the numbers in the array.
1205
+ *
1206
+ * @example
1207
+ * * const numbers = [1, 2, 3, 4, 5];
1208
+ * const result = sum(numbers);
1209
+ * // result will be 15
1210
+ */
1211
+ function sum(nums) {
1212
+ var result = 0;
1213
+ var _iterator = _createForOfIteratorHelper(nums),
1214
+ _step;
1215
+ try {
1216
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
1217
+ var num = _step.value;
1218
+ result += num;
1219
+ }
1220
+ } catch (err) {
1221
+ _iterator.e(err);
1222
+ } finally {
1223
+ _iterator.f();
1224
+ }
1225
+ return result;
1226
+ }
1227
+
1228
+ /**
1229
+ * Creates a new object with specified keys omitted.
1230
+ *
1231
+ * This function takes an object and an array of keys, and returns a new object that
1232
+ * excludes the properties corresponding to the specified keys.
1233
+ *
1234
+ * @param {T} obj - The object to omit keys from.
1235
+ * @param {K[]} keys - An array of keys to be omitted from the object.
1236
+ * @returns {Omit<T, K>} A new object with the specified keys omitted.
1237
+ *
1238
+ * @example
1239
+ * * const obj = { a: 1, b: 2, c: 3 };
1240
+ * const result = omit(obj, ['b', 'c']);
1241
+ * // result will be { a: 1 }
1242
+ */
1243
+ function omit(obj, keys) {
1244
+ var result = _objectSpread2({}, obj);
1245
+ var _iterator = _createForOfIteratorHelper(keys),
1246
+ _step;
1247
+ try {
1248
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
1249
+ var key = _step.value;
1250
+ delete result[key];
1251
+ }
1252
+ } catch (err) {
1253
+ _iterator.e(err);
1254
+ } finally {
1255
+ _iterator.f();
1256
+ }
1257
+ return result;
1258
+ }
1259
+
1260
+ /**
1261
+ * Creates a new object composed of the properties that do not satisfy the predicate function.
1262
+ *
1263
+ * This function takes an object and a predicate function, and returns a new object that
1264
+ * includes only the properties for which the predicate function returns false.
1265
+ *
1266
+ * @param {T} obj - The object to omit properties from.
1267
+ * @param {(value: T[string], key: string) => boolean} shouldOmit - A predicate function that determines
1268
+ * whether a property should be omitted. It takes the property's key and value as arguments and returns `true`
1269
+ * if the property should be omitted, and `false` otherwise.
1270
+ * @returns {Partial<T>} A new object with the properties that do not satisfy the predicate function.
1271
+ *
1272
+ * @example
1273
+ * const obj = { a: 1, b: 'omit', c: 3 };
1274
+ * const shouldOmit = (key, value) => typeof value === 'string';
1275
+ * const result = omitBy(obj, shouldOmit);
1276
+ * // result will be { a: 1, c: 3 }
1277
+ */
1278
+ function omitBy(obj, shouldOmit) {
1279
+ var result = {};
1280
+ for (var _i = 0, _Object$entries = Object.entries(obj); _i < _Object$entries.length; _i++) {
1281
+ var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
1282
+ key = _Object$entries$_i[0],
1283
+ value = _Object$entries$_i[1];
1284
+ if (shouldOmit(value, key)) {
1285
+ continue;
1286
+ }
1287
+ result[key] = value;
1288
+ }
1289
+ return result;
1290
+ }
1291
+
1292
+ /**
1293
+ * Creates a new object composed of the picked object properties.
1294
+ *
1295
+ * This function takes an object and an array of keys, and returns a new object that
1296
+ * includes only the properties corresponding to the specified keys.
1297
+ *
1298
+ * @param {T} obj - The object to pick keys from.
1299
+ * @param {K[]} keys - An array of keys to be picked from the object.
1300
+ * @returns {Pick<T, K>} A new object with the specified keys picked.
1301
+ *
1302
+ * @example
1303
+ * * const obj = { a: 1, b: 2, c: 3 };
1304
+ * const result = pick(obj, ['a', 'c']);
1305
+ * // result will be { a: 1, c: 3 }
1306
+ */
1307
+ function pick(obj, keys) {
1308
+ var result = {};
1309
+ var _iterator = _createForOfIteratorHelper(keys),
1310
+ _step;
1311
+ try {
1312
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
1313
+ var key = _step.value;
1314
+ result[key] = obj[key];
1315
+ }
1316
+ } catch (err) {
1317
+ _iterator.e(err);
1318
+ } finally {
1319
+ _iterator.f();
1320
+ }
1321
+ return result;
1322
+ }
1323
+
1324
+ /**
1325
+ * Creates a new object composed of the properties that satisfy the predicate function.
1326
+ *
1327
+ * This function takes an object and a predicate function, and returns a new object that
1328
+ * includes only the properties for which the predicate function returns true.
1329
+ *
1330
+ * @param {T} obj - The object to pick properties from.
1331
+ * @param {(value: T[keyof T], key: string) => boolean} shouldPick - A predicate function that determines
1332
+ * whether a property should be picked. It takes the property's key and value as arguments and returns `true`
1333
+ * if the property should be picked, and `false` otherwise.
1334
+ * @returns {Partial<T>} A new object with the properties that satisfy the predicate function.
1335
+ *
1336
+ * @example
1337
+ * const obj = { a: 1, b: 'pick', c: 3 };
1338
+ * const shouldPick = (value) => typeof value === 'string';
1339
+ * const result = pickBy(obj, shouldPick);
1340
+ * // result will be { b: 'pick' }
1341
+ */
1342
+ function pickBy(obj, shouldPick) {
1343
+ var result = {};
1344
+ for (var _i = 0, _Object$entries = Object.entries(obj); _i < _Object$entries.length; _i++) {
1345
+ var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
1346
+ key = _Object$entries$_i[0],
1347
+ value = _Object$entries$_i[1];
1348
+ if (!shouldPick(value, key)) {
1349
+ continue;
1350
+ }
1351
+ result[key] = value;
1352
+ }
1353
+ return result;
1354
+ }
1355
+
1356
+ /**
1357
+ * Checks if a given value is null or undefined.
1358
+ *
1359
+ * This function tests whether the provided value is either `null` or `undefined`.
1360
+ * It returns `true` if the value is `null` or `undefined`, and `false` otherwise.
1361
+ *
1362
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `null` or `undefined`.
1363
+ *
1364
+ * @param {unknown} x - The value to test for null or undefined.
1365
+ * @returns {boolean} `true` if the value is null or undefined, `false` otherwise.
1366
+ *
1367
+ * @example
1368
+ * * const value1 = null;
1369
+ * const value2 = undefined;
1370
+ * const value3 = 42;
1371
+ * const result1 = isNil(value1); // true
1372
+ * const result2 = isNil(value2); // true
1373
+ * const result3 = isNil(value3); // false
1374
+ */
1375
+ function isNil(x) {
1376
+ return x == null || x == undefined;
1377
+ }
1378
+
1379
+ /**
1380
+ * Checks if the given value is not null nor undefined.
1381
+ *
1382
+ * The main use of this function is to be used with TypeScript as a type predicate.
1383
+ *
1384
+ * @param {T | null | undefined} x - The value to test if it is not null nor undefined.
1385
+ * @returns {x is T} True if the value is not null nor undefined, false otherwise.
1386
+ *
1387
+ * @example
1388
+ * // Here the type of `arr` is (number | undefined)[]
1389
+ * const arr = [1, undefined, 3];
1390
+ * // Here the type of `result` is number[]
1391
+ * const result = arr.filter(isNotNil);
1392
+ * // result will be [1, 3]
1393
+ */
1394
+ function isNotNil(x) {
1395
+ return x != null && x != undefined;
1396
+ }
1397
+
1398
+ /**
1399
+ * Checks if the given value is null.
1400
+ *
1401
+ * This function tests whether the provided value is strictly equal to `null`.
1402
+ * It returns `true` if the value is `null`, and `false` otherwise.
1403
+ *
1404
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `null`.
1405
+ *
1406
+ * @param {unknown} x - The value to test if it is null.
1407
+ * @returns {x is null} True if the value is null, false otherwise.
1408
+ *
1409
+ * @example
1410
+ * * const value1 = null;
1411
+ * const value2 = undefined;
1412
+ * const value3 = 42;
1413
+ *
1414
+ * console.log(isNull(value1)); // true
1415
+ * console.log(isNull(value2)); // false
1416
+ * console.log(isNull(value3)); // false
1417
+ */
1418
+ function isNull(x) {
1419
+ return x === null;
1420
+ }
1421
+
1422
+ /**
1423
+ * Checks if the given value is undefined.
1424
+ *
1425
+ * This function tests whether the provided value is strictly equal to `undefined`.
1426
+ * It returns `true` if the value is `undefined`, and `false` otherwise.
1427
+ *
1428
+ * This function can also serve as a type predicate in TypeScript, narrowing the type of the argument to `undefined`.
1429
+ *
1430
+ * @param {unknown} x - The value to test if it is undefined.
1431
+ * @returns {x is undefined} true if the value is undefined, false otherwise.
1432
+ *
1433
+ * @example
1434
+ * * const value1 = undefined;
1435
+ * const value2 = null;
1436
+ * const value3 = 42;
1437
+ *
1438
+ * console.log(isUndefined(value1)); // true
1439
+ * console.log(isUndefined(value2)); // false
1440
+ * console.log(isUndefined(value3)); // false
1441
+ */
1442
+ function isUndefined(x) {
1443
+ return x === undefined;
1444
+ }
1445
+
1446
+ /**
1447
+ * Delays the execution of code for a specified number of milliseconds.
1448
+ *
1449
+ * This function returns a Promise that resolves after the specified delay, allowing you to use it
1450
+ * with async/await to pause execution.
1451
+ *
1452
+ * @param {number} ms - The number of milliseconds to delay.
1453
+ * @returns {Promise<void>} A Promise that resolves after the specified delay.
1454
+ *
1455
+ * @example
1456
+ * async function foo() {
1457
+ * console.log('Start');
1458
+ * await delay(1000); // Delays execution for 1 second
1459
+ * console.log('End');
1460
+ * }
1461
+ *
1462
+ * foo();
1463
+ */
1464
+ function delay(ms) {
1465
+ return new Promise(function (resolve) {
1466
+ setTimeout(resolve, ms);
1467
+ });
1468
+ }
1469
+
1470
+ exports.chunk = chunk;
1471
+ exports.clamp = clamp;
1472
+ exports.debounce = debounce;
1473
+ exports.delay = delay;
1474
+ exports.difference = difference;
1475
+ exports.differenceBy = differenceBy;
1476
+ exports.differenceWith = differenceWith;
1477
+ exports.drop = drop;
1478
+ exports.dropRight = dropRight;
1479
+ exports.dropRightWhile = dropRightWhile;
1480
+ exports.dropWhile = dropWhile;
1481
+ exports.groupBy = groupBy;
1482
+ exports.intersection = intersection;
1483
+ exports.intersectionBy = intersectionBy;
1484
+ exports.intersectionWith = intersectionWith;
1485
+ exports.isNil = isNil;
1486
+ exports.isNotNil = isNotNil;
1487
+ exports.isNull = isNull;
1488
+ exports.isUndefined = isUndefined;
1489
+ exports.omit = omit;
1490
+ exports.omitBy = omitBy;
1491
+ exports.once = once;
1492
+ exports.partition = partition;
1493
+ exports.pick = pick;
1494
+ exports.pickBy = pickBy;
1495
+ exports.round = round;
1496
+ exports.sample = sample;
1497
+ exports.shuffle = shuffle;
1498
+ exports.sum = sum;
1499
+ exports.take = take;
1500
+ exports.takeRight = takeRight;
1501
+ exports.takeRightWhile = takeRightWhile;
1502
+ exports.takeWhile = takeWhile;
1503
+ exports.throttle = throttle;
1504
+ exports.union = union;
1505
+ exports.unionBy = unionBy;
1506
+ exports.unionWith = unionWith;
1507
+ exports.uniq = uniq;
1508
+ exports.uniqBy = uniqBy;
1509
+ exports.uniqWith = uniqWith;
1510
+ exports.xor = xor;
1511
+ exports.xorBy = xorBy;
1512
+ exports.xorWith = xorWith;
1513
+ exports.zip = zip;
1514
+ exports.zipWith = zipWith;