es-toolkit 0.0.1 → 1.0.0-dev.24
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.
- package/CHANGELOG.md +5 -0
- package/README.md +31 -0
- package/dist/array/chunk.d.ts +21 -5
- package/dist/array/difference.d.ts +20 -4
- package/dist/array/differenceBy.d.ts +23 -5
- package/dist/array/differenceWith.d.ts +19 -5
- package/dist/array/drop.d.ts +13 -4
- package/dist/array/dropRight.d.ts +13 -4
- package/dist/array/dropRightWhile.d.ts +15 -4
- package/dist/array/dropWhile.d.ts +15 -4
- package/dist/array/groupBy.d.ts +31 -1
- package/dist/array/groupBy.spec.d.ts +1 -0
- package/dist/array/index.js +437 -224
- package/dist/array/intersection.d.ts +13 -6
- package/dist/array/intersectionBy.d.ts +17 -8
- package/dist/array/intersectionWith.d.ts +18 -8
- package/dist/array/partition.d.ts +19 -5
- package/dist/array/sample.d.ts +10 -3
- package/dist/array/shuffle.d.ts +11 -4
- package/dist/array/takeRight.d.ts +0 -2
- package/dist/array/union.d.ts +13 -6
- package/dist/array/unionBy.d.ts +0 -3
- package/dist/array/unionWith.d.ts +15 -9
- package/dist/array/uniq.d.ts +9 -8
- package/dist/array/xor.d.ts +0 -2
- package/dist/array/zip.d.ts +18 -6
- package/dist/array/zipWith.d.ts +26 -3
- package/dist/function/debounce.d.ts +4 -4
- package/dist/function/index.js +4 -5
- package/dist/function/once.d.ts +0 -1
- package/dist/index.js +651 -248
- package/dist/math/clamp.d.ts +16 -8
- package/dist/math/index.js +114 -29
- package/dist/math/round.d.ts +11 -8
- package/dist/math/sum.d.ts +10 -9
- package/dist/object/index.js +122 -12
- package/dist/object/omit.d.ts +13 -4
- package/dist/object/omitBy.d.ts +19 -1
- package/dist/object/omitBy.spec.d.ts +1 -0
- package/dist/object/pick.d.ts +13 -4
- package/dist/object/pickBy.d.ts +19 -1
- package/dist/object/pickBy.spec.d.ts +1 -0
- package/dist/predicate/index.js +58 -10
- package/dist/predicate/isNil.d.ts +17 -3
- package/dist/predicate/isNotNil.d.ts +7 -3
- package/dist/predicate/isNull.d.ts +17 -2
- package/dist/predicate/isUndefined.d.ts +17 -2
- package/dist/promise/delay.d.ts +17 -4
- package/dist/promise/index.js +16 -3
- package/esm/array/_virtual/_rollupPluginBabelHelpers.mjs +1 -37
- package/esm/array/chunk.mjs +28 -25
- package/esm/array/difference.mjs +20 -4
- package/esm/array/differenceBy.mjs +23 -5
- package/esm/array/differenceWith.mjs +19 -5
- package/esm/array/drop.mjs +13 -4
- package/esm/array/dropRight.mjs +13 -4
- package/esm/array/dropRightWhile.mjs +15 -4
- package/esm/array/dropWhile.mjs +15 -4
- package/esm/array/groupBy.mjs +51 -2
- package/esm/array/intersection.mjs +13 -6
- package/esm/array/intersectionBy.mjs +17 -8
- package/esm/array/intersectionWith.mjs +18 -8
- package/esm/array/partition.mjs +19 -5
- package/esm/array/sample.mjs +10 -3
- package/esm/array/shuffle.mjs +12 -5
- package/esm/array/takeRight.mjs +0 -2
- package/esm/array/union.mjs +14 -10
- package/esm/array/unionBy.mjs +0 -3
- package/esm/array/unionWith.mjs +16 -11
- package/esm/array/uniq.mjs +28 -9
- package/esm/array/xor.mjs +0 -2
- package/esm/array/zip.mjs +18 -6
- package/esm/array/zipWith.mjs +26 -3
- package/esm/function/debounce.mjs +4 -4
- package/esm/function/once.mjs +0 -1
- package/esm/math/_virtual/_rollupPluginBabelHelpers.mjs +66 -0
- package/esm/math/clamp.mjs +15 -7
- package/esm/math/round.mjs +11 -8
- package/esm/math/sum.mjs +25 -14
- package/esm/object/_virtual/_rollupPluginBabelHelpers.mjs +37 -1
- package/esm/object/omit.mjs +13 -4
- package/esm/object/omitBy.mjs +32 -2
- package/esm/object/pick.mjs +13 -4
- package/esm/object/pickBy.mjs +32 -2
- package/esm/predicate/isNil.mjs +17 -3
- package/esm/predicate/isNotNil.mjs +7 -3
- package/esm/predicate/isNull.mjs +17 -2
- package/esm/predicate/isUndefined.mjs +17 -2
- package/esm/promise/delay.mjs +16 -3
- package/package.json +17 -3
- package/.vscode/extensions.json +0 -7
- package/.vscode/settings.json +0 -10
- package/.yarn/sdks/eslint/bin/eslint.js +0 -20
- package/.yarn/sdks/eslint/lib/api.js +0 -20
- package/.yarn/sdks/eslint/lib/unsupported-api.js +0 -20
- package/.yarn/sdks/eslint/package.json +0 -14
- package/.yarn/sdks/integrations.yml +0 -5
- package/.yarn/sdks/prettier/bin/prettier.cjs +0 -20
- package/.yarn/sdks/prettier/index.cjs +0 -20
- package/.yarn/sdks/prettier/package.json +0 -7
- package/.yarn/sdks/typescript/bin/tsc +0 -20
- package/.yarn/sdks/typescript/bin/tsserver +0 -20
- package/.yarn/sdks/typescript/lib/tsc.js +0 -20
- package/.yarn/sdks/typescript/lib/tsserver.js +0 -225
- package/.yarn/sdks/typescript/lib/tsserverlibrary.js +0 -225
- package/.yarn/sdks/typescript/lib/typescript.js +0 -20
- package/.yarn/sdks/typescript/package.json +0 -10
- package/babel.config.js +0 -6
- package/coverage/.tmp/coverage-0.json +0 -1
- package/coverage/.tmp/coverage-1.json +0 -1
- package/coverage/.tmp/coverage-10.json +0 -1
- package/coverage/.tmp/coverage-11.json +0 -1
- package/coverage/.tmp/coverage-12.json +0 -1
- package/coverage/.tmp/coverage-13.json +0 -1
- package/coverage/.tmp/coverage-14.json +0 -1
- package/coverage/.tmp/coverage-15.json +0 -1
- package/coverage/.tmp/coverage-16.json +0 -1
- package/coverage/.tmp/coverage-17.json +0 -1
- package/coverage/.tmp/coverage-18.json +0 -1
- package/coverage/.tmp/coverage-19.json +0 -1
- package/coverage/.tmp/coverage-2.json +0 -1
- package/coverage/.tmp/coverage-20.json +0 -1
- package/coverage/.tmp/coverage-21.json +0 -1
- package/coverage/.tmp/coverage-22.json +0 -1
- package/coverage/.tmp/coverage-23.json +0 -1
- package/coverage/.tmp/coverage-24.json +0 -1
- package/coverage/.tmp/coverage-25.json +0 -1
- package/coverage/.tmp/coverage-26.json +0 -1
- package/coverage/.tmp/coverage-27.json +0 -1
- package/coverage/.tmp/coverage-28.json +0 -1
- package/coverage/.tmp/coverage-29.json +0 -1
- package/coverage/.tmp/coverage-3.json +0 -1
- package/coverage/.tmp/coverage-30.json +0 -1
- package/coverage/.tmp/coverage-4.json +0 -1
- package/coverage/.tmp/coverage-5.json +0 -1
- package/coverage/.tmp/coverage-6.json +0 -1
- package/coverage/.tmp/coverage-7.json +0 -1
- package/coverage/.tmp/coverage-8.json +0 -1
- package/coverage/.tmp/coverage-9.json +0 -1
- package/rollup.config.js +0 -5
- package/src/array/chunk.spec.ts +0 -27
- package/src/array/chunk.ts +0 -24
- package/src/array/difference.spec.ts +0 -10
- package/src/array/difference.ts +0 -11
- package/src/array/differenceBy.spec.ts +0 -9
- package/src/array/differenceBy.ts +0 -14
- package/src/array/differenceWith.spec.ts +0 -8
- package/src/array/differenceWith.ts +0 -14
- package/src/array/drop.spec.ts +0 -9
- package/src/array/drop.ts +0 -9
- package/src/array/dropRight.spec.ts +0 -9
- package/src/array/dropRight.ts +0 -9
- package/src/array/dropRightWhile.spec.ts +0 -16
- package/src/array/dropRightWhile.ts +0 -13
- package/src/array/dropWhile.spec.ts +0 -14
- package/src/array/dropWhile.ts +0 -10
- package/src/array/groupBy.ts +0 -3
- package/src/array/index.ts +0 -30
- package/src/array/intersection.spec.ts +0 -11
- package/src/array/intersection.ts +0 -15
- package/src/array/intersectionBy.spec.ts +0 -9
- package/src/array/intersectionBy.ts +0 -18
- package/src/array/intersectionWith.spec.ts +0 -9
- package/src/array/intersectionWith.ts +0 -18
- package/src/array/partition.spec.ts +0 -23
- package/src/array/partition.ts +0 -21
- package/src/array/sample.spec.ts +0 -11
- package/src/array/sample.ts +0 -10
- package/src/array/shuffle.spec.ts +0 -11
- package/src/array/shuffle.ts +0 -18
- package/src/array/take.spec.ts +0 -25
- package/src/array/take.ts +0 -25
- package/src/array/takeRight.spec.ts +0 -25
- package/src/array/takeRight.ts +0 -29
- package/src/array/takeRightWhile.spec.ts +0 -16
- package/src/array/takeRightWhile.ts +0 -26
- package/src/array/takeWhile.spec.ts +0 -34
- package/src/array/takeWhile.ts +0 -31
- package/src/array/union.spec.ts +0 -10
- package/src/array/union.ts +0 -14
- package/src/array/unionBy.spec.ts +0 -12
- package/src/array/unionBy.ts +0 -35
- package/src/array/unionWith.spec.ts +0 -23
- package/src/array/unionWith.ts +0 -18
- package/src/array/uniq.spec.ts +0 -8
- package/src/array/uniq.ts +0 -16
- package/src/array/uniqBy.spec.ts +0 -8
- package/src/array/uniqBy.ts +0 -17
- package/src/array/uniqWith.spec.ts +0 -16
- package/src/array/uniqWith.ts +0 -29
- package/src/array/xor.spec.ts +0 -13
- package/src/array/xor.ts +0 -25
- package/src/array/xorBy.spec.ts +0 -16
- package/src/array/xorBy.ts +0 -29
- package/src/array/xorWith.spec.ts +0 -16
- package/src/array/xorWith.ts +0 -28
- package/src/array/zip.spec.ts +0 -14
- package/src/array/zip.ts +0 -34
- package/src/array/zipWith.spec.ts +0 -13
- package/src/array/zipWith.ts +0 -23
- package/src/function/debounce.spec.ts +0 -76
- package/src/function/debounce.ts +0 -42
- package/src/function/index.ts +0 -3
- package/src/function/once.spec.ts +0 -31
- package/src/function/once.ts +0 -34
- package/src/function/throttle.spec.ts +0 -32
- package/src/function/throttle.ts +0 -39
- package/src/index.ts +0 -6
- package/src/math/clamp.spec.ts +0 -18
- package/src/math/clamp.ts +0 -17
- package/src/math/index.ts +0 -3
- package/src/math/round.spec.ts +0 -53
- package/src/math/round.ts +0 -16
- package/src/math/sum.spec.ts +0 -29
- package/src/math/sum.ts +0 -17
- package/src/object/index.ts +0 -4
- package/src/object/omit.spec.ts +0 -10
- package/src/object/omit.ts +0 -15
- package/src/object/omitBy.ts +0 -3
- package/src/object/pick.spec.ts +0 -10
- package/src/object/pick.ts +0 -15
- package/src/object/pickBy.ts +0 -3
- package/src/predicate/index.ts +0 -4
- package/src/predicate/isNil.spec.ts +0 -23
- package/src/predicate/isNil.ts +0 -8
- package/src/predicate/isNotNil.spec.ts +0 -29
- package/src/predicate/isNotNil.ts +0 -14
- package/src/predicate/isNull.spec.ts +0 -23
- package/src/predicate/isNull.ts +0 -8
- package/src/predicate/isUndefined.spec.ts +0 -23
- package/src/predicate/isUndefined.ts +0 -8
- package/src/promise/delay.spec.ts +0 -13
- package/src/promise/delay.ts +0 -10
- package/src/promise/index.ts +0 -1
- package/tsconfig.json +0 -12
- package/vitest.config.mts +0 -13
package/dist/array/index.js
CHANGED
|
@@ -2,45 +2,225 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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.');
|
|
30
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
|
+
});
|
|
31
102
|
}
|
|
32
|
-
|
|
33
|
-
|
|
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
|
+
});
|
|
34
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
|
+
|
|
35
218
|
function _toConsumableArray(arr) {
|
|
36
219
|
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
|
|
37
220
|
}
|
|
38
221
|
function _arrayWithoutHoles(arr) {
|
|
39
222
|
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
|
|
40
223
|
}
|
|
41
|
-
function _arrayWithHoles(arr) {
|
|
42
|
-
if (Array.isArray(arr)) return arr;
|
|
43
|
-
}
|
|
44
224
|
function _iterableToArray(iter) {
|
|
45
225
|
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
46
226
|
}
|
|
@@ -60,9 +240,6 @@ function _arrayLikeToArray(arr, len) {
|
|
|
60
240
|
function _nonIterableSpread() {
|
|
61
241
|
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
62
242
|
}
|
|
63
|
-
function _nonIterableRest() {
|
|
64
|
-
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
65
|
-
}
|
|
66
243
|
function _createForOfIteratorHelper(o, allowArrayLike) {
|
|
67
244
|
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
|
|
68
245
|
if (!it) {
|
|
@@ -116,28 +293,47 @@ function _createForOfIteratorHelper(o, allowArrayLike) {
|
|
|
116
293
|
}
|
|
117
294
|
|
|
118
295
|
/**
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
296
|
+
* Groups the elements of an array based on a provided key-generating function.
|
|
297
|
+
*
|
|
298
|
+
* This function takes an array and a function that generates a key from each element. It returns
|
|
299
|
+
* an object where the keys are the generated keys and the values are arrays of elements that share
|
|
300
|
+
* the same key.
|
|
301
|
+
*
|
|
302
|
+
* @param {T[]} arr - The array to group.
|
|
303
|
+
* @param {(item: T) => K} getKeyFromItem - A function that generates a key from an element.
|
|
304
|
+
* @returns {Record<K, T[]>} An object where each key is associated with an array of elements that
|
|
305
|
+
* share that key.
|
|
306
|
+
*
|
|
307
|
+
* @example
|
|
308
|
+
* const array = [
|
|
309
|
+
* { category: 'fruit', name: 'apple' },
|
|
310
|
+
* { category: 'fruit', name: 'banana' },
|
|
311
|
+
* { category: 'vegetable', name: 'carrot' }
|
|
312
|
+
* ];
|
|
313
|
+
* const result = groupBy(array, item => item.category);
|
|
314
|
+
* // result will be:
|
|
315
|
+
* // {
|
|
316
|
+
* // fruit: [
|
|
317
|
+
* // { category: 'fruit', name: 'apple' },
|
|
318
|
+
* // { category: 'fruit', name: 'banana' }
|
|
319
|
+
* // ],
|
|
320
|
+
* // vegetable: [
|
|
321
|
+
* // { category: 'vegetable', name: 'carrot' }
|
|
322
|
+
* // ]
|
|
323
|
+
* // }
|
|
124
324
|
*/
|
|
125
|
-
function
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
var result = [];
|
|
130
|
-
var _iterator = _createForOfIteratorHelper(arr.entries()),
|
|
325
|
+
function groupBy(arr, getKeyFromItem) {
|
|
326
|
+
var result = {};
|
|
327
|
+
var _iterator = _createForOfIteratorHelper(arr),
|
|
131
328
|
_step;
|
|
132
329
|
try {
|
|
133
330
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
134
|
-
var
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
result.push([]);
|
|
331
|
+
var item = _step.value;
|
|
332
|
+
var key = getKeyFromItem(item);
|
|
333
|
+
if (result[key] == null) {
|
|
334
|
+
result[key] = [];
|
|
139
335
|
}
|
|
140
|
-
result[
|
|
336
|
+
result[key].push(item);
|
|
141
337
|
}
|
|
142
338
|
} catch (err) {
|
|
143
339
|
_iterator.e(err);
|
|
@@ -148,106 +344,21 @@ function chunk(arr, size) {
|
|
|
148
344
|
}
|
|
149
345
|
|
|
150
346
|
/**
|
|
151
|
-
* @name difference
|
|
152
|
-
* Returns the difference of two arrays.
|
|
153
|
-
* @param firstArr The array to get the difference from
|
|
154
|
-
* @param secondArr The elements to exclude from `firstArr`
|
|
155
|
-
*/
|
|
156
|
-
function difference(firstArr, secondArr) {
|
|
157
|
-
return firstArr.filter(function (item) {
|
|
158
|
-
return !secondArr.includes(item);
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* @name differenceBy
|
|
164
|
-
* Returns the difference of two arrays. The identity of two elements is calculated after the elements are mapped with `mapper`.
|
|
165
|
-
* @param firstArr The array to get the difference from
|
|
166
|
-
* @param secondArr The elements to exclude from `firstArr`
|
|
167
|
-
* @param mapper The function to map the elements when calculating the difference
|
|
168
|
-
*/
|
|
169
|
-
function differenceBy(firstArr, secondArr, mapper) {
|
|
170
|
-
var mappedSecondArr = secondArr.map(function (item) {
|
|
171
|
-
return mapper(item);
|
|
172
|
-
});
|
|
173
|
-
return firstArr.filter(function (item) {
|
|
174
|
-
return !mappedSecondArr.includes(mapper(item));
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* @name differenceWith
|
|
180
|
-
* Returns the difference of two arrays. The identity of two elements is calculated with the function `areItemsEqual`.
|
|
181
|
-
* @param firstArr The array to get the difference from
|
|
182
|
-
* @param secondArr The elements to exclude from `firstArr`
|
|
183
|
-
* @param areItemsEqual The function to determine whether two items are equal
|
|
184
|
-
*/
|
|
185
|
-
function differenceWith(firstArr, secondArr, areItemsEqual) {
|
|
186
|
-
return firstArr.filter(function (firstItem) {
|
|
187
|
-
return secondArr.every(function (secondItem) {
|
|
188
|
-
return !areItemsEqual(firstItem, secondItem);
|
|
189
|
-
});
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* @name drop
|
|
195
|
-
* Drop some elements from an array from the beginning. This function returns a new array.
|
|
196
|
-
* @param arr The element to drop items from the beginning
|
|
197
|
-
* @param itemsCount The amount of elements to drop
|
|
198
|
-
*/
|
|
199
|
-
function drop(arr, itemsCount) {
|
|
200
|
-
return arr.slice(itemsCount);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* @name dropRight
|
|
205
|
-
* Drop some elements from an array from the end. This function returns a new array.
|
|
206
|
-
* @param arr The element to drop items from the end
|
|
207
|
-
* @param itemsCount The amount of elements to drop
|
|
208
|
-
*/
|
|
209
|
-
function dropRight(arr, itemsCount) {
|
|
210
|
-
return arr.slice(0, -itemsCount);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* @name dropWhile
|
|
215
|
-
* Drop some elements from an array from the beginning, until `predicate` returns false.
|
|
216
|
-
* @param arr The element to drop items from the beginning
|
|
217
|
-
* @param canContinueDropping The predicate to check if the function can continue dropping elements.
|
|
218
|
-
*/
|
|
219
|
-
function dropWhile(arr, canContinueDropping) {
|
|
220
|
-
var dropEndIndex = arr.findIndex(function (item) {
|
|
221
|
-
return !canContinueDropping(item);
|
|
222
|
-
});
|
|
223
|
-
return arr.slice(dropEndIndex);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* @name dropRightWhile
|
|
228
|
-
* Drop some elements from an array from the end, until `predicate` returns false.
|
|
229
|
-
* @param arr The element to drop items from the end
|
|
230
|
-
* @param canContinueDropping The predicate to check if the function can continue dropping elements.
|
|
231
|
-
*/
|
|
232
|
-
function dropRightWhile(arr, canContinueDropping) {
|
|
233
|
-
var reversed = arr.slice().reverse();
|
|
234
|
-
var dropped = dropWhile(reversed, canContinueDropping);
|
|
235
|
-
return dropped.slice().reverse();
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function groupBy() {
|
|
239
|
-
throw new Error('Not implemented');
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* @name intersection
|
|
244
|
-
* @description
|
|
245
347
|
* Returns the intersection of two arrays.
|
|
246
|
-
*
|
|
247
|
-
*
|
|
348
|
+
*
|
|
349
|
+
* This function takes two arrays and returns a new array containing the elements that are
|
|
350
|
+
* present in both arrays. It effectively filters out any elements from the first array that
|
|
351
|
+
* are not found in the second array.
|
|
352
|
+
*
|
|
353
|
+
* @param {T[]} firstArr - The first array to compare.
|
|
354
|
+
* @param {T[]} secondArr - The second array to compare.
|
|
355
|
+
* @returns {T[]} A new array containing the elements that are present in both arrays.
|
|
356
|
+
*
|
|
248
357
|
* @example
|
|
249
|
-
*
|
|
250
|
-
*
|
|
358
|
+
* const array1 = [1, 2, 3, 4, 5];
|
|
359
|
+
* const array2 = [3, 4, 5, 6, 7];
|
|
360
|
+
* const result = intersection(array1, array2);
|
|
361
|
+
* // result will be [3, 4, 5] since these elements are in both arrays.
|
|
251
362
|
*/
|
|
252
363
|
function intersection(firstArr, secondArr) {
|
|
253
364
|
return firstArr.filter(function (item) {
|
|
@@ -256,15 +367,24 @@ function intersection(firstArr, secondArr) {
|
|
|
256
367
|
}
|
|
257
368
|
|
|
258
369
|
/**
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
370
|
+
* Returns the intersection of two arrays based on a mapping function.
|
|
371
|
+
*
|
|
372
|
+
* This function takes two arrays and a mapping function. It returns a new array containing
|
|
373
|
+
* the elements from the first array that, when mapped using the provided function, have matching
|
|
374
|
+
* mapped elements in the second array. It effectively filters out any elements from the first array
|
|
375
|
+
* that do not have corresponding mapped values in the second array.
|
|
376
|
+
*
|
|
377
|
+
* @param {T[]} firstArr - The first array to compare.
|
|
378
|
+
* @param {T[]} secondArr - The second array to compare.
|
|
379
|
+
* @param {(item: T) => U} mapper - A function to map the elements of both arrays for comparison.
|
|
380
|
+
* @returns {T[]} A new array containing the elements from the first array that have corresponding mapped values in the second array.
|
|
381
|
+
*
|
|
382
|
+
* @example
|
|
383
|
+
* const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }];
|
|
384
|
+
* const array2 = [{ id: 2 }, { id: 4 }];
|
|
385
|
+
* const mapper = item => item.id;
|
|
386
|
+
* const result = intersectionBy(array1, array2, mapper);
|
|
387
|
+
* // result will be [{ id: 2 }] since only this element has a matching id in both arrays.
|
|
268
388
|
*/
|
|
269
389
|
function intersectionBy(firstArr, secondArr, mapper) {
|
|
270
390
|
var mappedSecondArr = secondArr.map(function (x) {
|
|
@@ -276,15 +396,25 @@ function intersectionBy(firstArr, secondArr, mapper) {
|
|
|
276
396
|
}
|
|
277
397
|
|
|
278
398
|
/**
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
*
|
|
287
|
-
*
|
|
399
|
+
* Returns the intersection of two arrays based on a custom equality function.
|
|
400
|
+
*
|
|
401
|
+
* This function takes two arrays and a custom equality function. It returns a new array containing
|
|
402
|
+
* the elements from the first array that have matching elements in the second array, as determined
|
|
403
|
+
* by the custom equality function. It effectively filters out any elements from the first array that
|
|
404
|
+
* do not have corresponding matches in the second array according to the equality function.
|
|
405
|
+
*
|
|
406
|
+
* @param {T[]} firstArr - The first array to compare.
|
|
407
|
+
* @param {T[]} secondArr - The second array to compare.
|
|
408
|
+
* @param {(x: T, y: T) => boolean} areItemsEqual - A custom function to determine if two elements are equal.
|
|
409
|
+
* This function takes two arguments, one from each array, and returns `true` if the elements are considered equal, and `false` otherwise.
|
|
410
|
+
* @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.
|
|
411
|
+
*
|
|
412
|
+
* @example
|
|
413
|
+
* * const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }];
|
|
414
|
+
* const array2 = [{ id: 2 }, { id: 4 }];
|
|
415
|
+
* const areItemsEqual = (a, b) => a.id === b.id;
|
|
416
|
+
* const result = intersectionWith(array1, array2, areItemsEqual);
|
|
417
|
+
* // result will be [{ id: 2 }] since this element has a matching id in both arrays.
|
|
288
418
|
*/
|
|
289
419
|
function intersectionWith(firstArr, secondArr, areItemsEqual) {
|
|
290
420
|
return firstArr.filter(function (firstItem) {
|
|
@@ -295,11 +425,25 @@ function intersectionWith(firstArr, secondArr, areItemsEqual) {
|
|
|
295
425
|
}
|
|
296
426
|
|
|
297
427
|
/**
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
428
|
+
* Splits an array into two groups based on a predicate function.
|
|
429
|
+
*
|
|
430
|
+
* This function takes an array and a predicate function. It returns a tuple of two arrays:
|
|
431
|
+
* the first array contains elements for which the predicate function returns true, and
|
|
432
|
+
* the second array contains elements for which the predicate function returns false.
|
|
433
|
+
*
|
|
434
|
+
* @param {T[]} arr - The array to partition.
|
|
435
|
+
* @param {(value: T) => boolean} isInTruthy - A predicate function that determines
|
|
436
|
+
* whether an element should be placed in the truthy array. The function is called with each
|
|
437
|
+
* element of the array.
|
|
438
|
+
* @returns {[T[], T[]]} A tuple containing two arrays: the first array contains elements for
|
|
439
|
+
* which the predicate returned true, and the second array contains elements for which the
|
|
440
|
+
* predicate returned false.
|
|
441
|
+
*
|
|
442
|
+
* @example
|
|
443
|
+
* const array = [1, 2, 3, 4, 5];
|
|
444
|
+
* const isEven = x => x % 2 === 0;
|
|
445
|
+
* const [even, odd] = partition(array, isEven);
|
|
446
|
+
* // even will be [2, 4], and odd will be [1, 3, 5]
|
|
303
447
|
*/
|
|
304
448
|
function partition(arr, isInTruthy) {
|
|
305
449
|
var truthy = [];
|
|
@@ -324,10 +468,17 @@ function partition(arr, isInTruthy) {
|
|
|
324
468
|
}
|
|
325
469
|
|
|
326
470
|
/**
|
|
327
|
-
* @name sample
|
|
328
|
-
* @description
|
|
329
471
|
* Returns a random element from an array.
|
|
330
|
-
*
|
|
472
|
+
*
|
|
473
|
+
* This function takes an array and returns a single element selected randomly from the array.
|
|
474
|
+
*
|
|
475
|
+
* @param {T[]} arr - The array to sample from.
|
|
476
|
+
* @returns {T} A random element from the array.
|
|
477
|
+
*
|
|
478
|
+
* @example
|
|
479
|
+
* const array = [1, 2, 3, 4, 5];
|
|
480
|
+
* const randomElement = sample(array);
|
|
481
|
+
* // randomElement will be one of the elements from the array, selected randomly.
|
|
331
482
|
*/
|
|
332
483
|
function sample(arr) {
|
|
333
484
|
var randomIndex = Math.floor(Math.random() * arr.length);
|
|
@@ -335,17 +486,24 @@ function sample(arr) {
|
|
|
335
486
|
}
|
|
336
487
|
|
|
337
488
|
/**
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
*
|
|
341
|
-
*
|
|
489
|
+
* Randomizes the order of elements in an array using the Fisher-Yates algorithm.
|
|
490
|
+
*
|
|
491
|
+
* This function takes an array and returns a new array with its elements shuffled in a random order.
|
|
492
|
+
*
|
|
493
|
+
* @param {T[]} arr - The array to shuffle.
|
|
494
|
+
* @returns {T[]} A new array with its elements shuffled in random order.
|
|
495
|
+
*
|
|
496
|
+
* @example
|
|
497
|
+
* const array = [1, 2, 3, 4, 5];
|
|
498
|
+
* const shuffledArray = shuffle(array);
|
|
499
|
+
* // shuffledArray will be a new array with elements of array in random order, e.g., [3, 1, 4, 5, 2]
|
|
342
500
|
*/
|
|
343
501
|
function shuffle(arr) {
|
|
344
502
|
/**
|
|
345
503
|
* https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
|
|
346
504
|
*/
|
|
347
505
|
for (var i = arr.length - 1; i >= 1; i--) {
|
|
348
|
-
var j = Math.floor(Math.random() * i);
|
|
506
|
+
var j = Math.floor(Math.random() * (i + 1));
|
|
349
507
|
var _ref = [arr[j], arr[i]];
|
|
350
508
|
arr[i] = _ref[0];
|
|
351
509
|
arr[j] = _ref[1];
|
|
@@ -382,8 +540,6 @@ function take(arr, count) {
|
|
|
382
540
|
/**
|
|
383
541
|
* Returns a new array containing the last `count` elements from the input array `arr`.
|
|
384
542
|
* If `count` is greater than the length of `arr`, the entire array is returned.
|
|
385
|
-
*
|
|
386
|
-
* @template T - Type of elements in the input array.
|
|
387
543
|
*
|
|
388
544
|
* @param {T[]} arr - The array to take elements from.
|
|
389
545
|
* @param {number} count - The number of elements to take.
|
|
@@ -473,43 +629,61 @@ function takeWhile(arr, shouldContinueTaking) {
|
|
|
473
629
|
}
|
|
474
630
|
|
|
475
631
|
/**
|
|
476
|
-
*
|
|
477
|
-
*
|
|
478
|
-
*
|
|
632
|
+
* Creates a duplicate-free version of an array.
|
|
633
|
+
*
|
|
634
|
+
* This function takes an array and returns a new array containing only the unique values
|
|
635
|
+
* from the original array, preserving the order of first occurrence.
|
|
479
636
|
*
|
|
480
|
-
*
|
|
637
|
+
* @param {T[]} arr - The array to process.
|
|
638
|
+
* @returns {T[]} A new array with only unique values from the original array.
|
|
481
639
|
*
|
|
482
640
|
* @example
|
|
483
|
-
*
|
|
484
|
-
*
|
|
485
|
-
* // [1, 2, 3,
|
|
486
|
-
* ```
|
|
641
|
+
* * const array = [1, 2, 2, 3, 4, 4, 5];
|
|
642
|
+
* const result = uniq(array);
|
|
643
|
+
* // result will be [1, 2, 3, 4, 5]
|
|
487
644
|
*/
|
|
488
645
|
function uniq(arr) {
|
|
489
|
-
|
|
646
|
+
var result = [];
|
|
647
|
+
var _iterator = _createForOfIteratorHelper(arr),
|
|
648
|
+
_step;
|
|
649
|
+
try {
|
|
650
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
651
|
+
var item = _step.value;
|
|
652
|
+
if (result.includes(item)) {
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
result.push(item);
|
|
656
|
+
}
|
|
657
|
+
} catch (err) {
|
|
658
|
+
_iterator.e(err);
|
|
659
|
+
} finally {
|
|
660
|
+
_iterator.f();
|
|
661
|
+
}
|
|
662
|
+
return result;
|
|
490
663
|
}
|
|
491
664
|
|
|
492
665
|
/**
|
|
493
|
-
*
|
|
666
|
+
* Creates an array of unique values from all given arrays.
|
|
667
|
+
*
|
|
668
|
+
* This function takes two arrays, merges them into a single array, and returns a new array
|
|
669
|
+
* containing only the unique values from the merged array.
|
|
670
|
+
*
|
|
671
|
+
* @param {T[]} arr1 - The first array to merge and filter for unique values.
|
|
672
|
+
* @param {T[]} arr2 - The second array to merge and filter for unique values.
|
|
673
|
+
* @returns {T[]} A new array of unique values.
|
|
494
674
|
*
|
|
495
675
|
* @example
|
|
496
|
-
*
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
*
|
|
676
|
+
* const array1 = [1, 2, 3];
|
|
677
|
+
* const array2 = [3, 4, 5];
|
|
678
|
+
* const result = union(array1, array2);
|
|
679
|
+
* // result will be [1, 2, 3, 4, 5]
|
|
500
680
|
*/
|
|
501
|
-
function union() {
|
|
502
|
-
|
|
503
|
-
args[_key] = arguments[_key];
|
|
504
|
-
}
|
|
505
|
-
return uniq(args.flat());
|
|
681
|
+
function union(arr1, arr2) {
|
|
682
|
+
return uniq(arr1.concat(arr2));
|
|
506
683
|
}
|
|
507
684
|
|
|
508
685
|
/**
|
|
509
686
|
* Creates an array of unique values, in order, from all given arrays using a provided mapping function to determine equality.
|
|
510
|
-
*
|
|
511
|
-
* @template T - Type of elements in the input arrays.
|
|
512
|
-
* @template U - Type of the values returned by the mapping function.
|
|
513
687
|
*
|
|
514
688
|
* @param {T[]} arr1 - The first array.
|
|
515
689
|
* @param {T[]} arr2 - The second array.
|
|
@@ -577,20 +751,26 @@ function uniqWith(arr, areItemsEqual) {
|
|
|
577
751
|
}
|
|
578
752
|
|
|
579
753
|
/**
|
|
580
|
-
*
|
|
754
|
+
* Creates an array of unique values from two given arrays based on a custom equality function.
|
|
755
|
+
*
|
|
756
|
+
* This function takes two arrays and a custom equality function, merges the arrays, and returns
|
|
757
|
+
* a new array containing only the unique values as determined by the custom equality function.
|
|
758
|
+
*
|
|
759
|
+
* @param {T[]} arr1 - The first array to merge and filter for unique values.
|
|
760
|
+
* @param {T[]} arr2 - The second array to merge and filter for unique values.
|
|
761
|
+
* @param {(item1: T, item2: T) => boolean} areItemsEqual - A custom function to determine if two elements are equal.
|
|
762
|
+
* It takes two arguments and returns `true` if the elements are considered equal, and `false` otherwise.
|
|
763
|
+
* @returns {T[]} A new array of unique values based on the custom equality function.
|
|
581
764
|
*
|
|
582
765
|
* @example
|
|
583
|
-
*
|
|
584
|
-
*
|
|
585
|
-
*
|
|
586
|
-
*
|
|
587
|
-
*
|
|
588
|
-
* );
|
|
589
|
-
* // [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
|
|
590
|
-
* ```
|
|
766
|
+
* const array1 = [{ id: 1 }, { id: 2 }];
|
|
767
|
+
* const array2 = [{ id: 2 }, { id: 3 }];
|
|
768
|
+
* const areItemsEqual = (a, b) => a.id === b.id;
|
|
769
|
+
* const result = unionWith(array1, array2, areItemsEqual);
|
|
770
|
+
* // result will be [{ id: 1 }, { id: 2 }, { id: 3 }] since { id: 2 } is considered equal in both arrays
|
|
591
771
|
*/
|
|
592
772
|
function unionWith(arr1, arr2, areItemsEqual) {
|
|
593
|
-
return uniqWith(
|
|
773
|
+
return uniqWith(arr1.concat(arr2), areItemsEqual);
|
|
594
774
|
}
|
|
595
775
|
|
|
596
776
|
/**
|
|
@@ -613,8 +793,6 @@ function uniqBy(arr, converter) {
|
|
|
613
793
|
/**
|
|
614
794
|
* Computes the symmetric difference between two arrays. The symmetric difference is the set of elements
|
|
615
795
|
* which are in either of the arrays, but not in their intersection.
|
|
616
|
-
*
|
|
617
|
-
* @template T - Type of elements in the input arrays.
|
|
618
796
|
*
|
|
619
797
|
* @param {T[]} arr1 - The first array.
|
|
620
798
|
* @param {T[]} arr2 - The second array.
|
|
@@ -682,13 +860,25 @@ function xorWith(arr1, arr2, areElementsEqual) {
|
|
|
682
860
|
}
|
|
683
861
|
|
|
684
862
|
/**
|
|
685
|
-
*
|
|
686
|
-
* elements from the input arrays at the corresponding index.
|
|
863
|
+
* Combines multiple arrays into a single array of tuples.
|
|
687
864
|
*
|
|
688
|
-
*
|
|
689
|
-
*
|
|
690
|
-
*
|
|
691
|
-
*
|
|
865
|
+
* This function takes multiple arrays and returns a new array where each element is a tuple
|
|
866
|
+
* containing the corresponding elements from the input arrays. If the input arrays are of
|
|
867
|
+
* different lengths, the resulting array will have the length of the longest input array,
|
|
868
|
+
* with undefined values for missing elements.
|
|
869
|
+
*
|
|
870
|
+
* @param {...T[][]} arrs - The arrays to zip together.
|
|
871
|
+
* @returns {T[][]} A new array of tuples containing the corresponding elements from the input arrays.
|
|
872
|
+
*
|
|
873
|
+
* @example
|
|
874
|
+
* const arr1 = [1, 2, 3];
|
|
875
|
+
* const arr2 = ['a', 'b', 'c'];
|
|
876
|
+
* const result = zip(arr1, arr2);
|
|
877
|
+
* // result will be [[1, 'a'], [2, 'b'], [3, 'c']]
|
|
878
|
+
*
|
|
879
|
+
* const arr3 = [true, false];
|
|
880
|
+
* const result2 = zip(arr1, arr2, arr3);
|
|
881
|
+
* // result2 will be [[1, 'a', true], [2, 'b', false], [3, 'c', undefined]]
|
|
692
882
|
*/
|
|
693
883
|
|
|
694
884
|
function zip() {
|
|
@@ -719,9 +909,32 @@ function zip() {
|
|
|
719
909
|
}
|
|
720
910
|
|
|
721
911
|
/**
|
|
722
|
-
* Combines multiple arrays
|
|
723
|
-
*
|
|
724
|
-
*
|
|
912
|
+
* Combines multiple arrays into a single array using a custom combiner function.
|
|
913
|
+
*
|
|
914
|
+
* This function takes multiple arrays and a combiner function, and returns a new array where each element
|
|
915
|
+
* is the result of applying the combiner function to the corresponding elements of the input arrays.
|
|
916
|
+
*
|
|
917
|
+
* @param {T[]} arr1 - The first array to zip.
|
|
918
|
+
* @param {U[]} [arr2] - The second array to zip (optional).
|
|
919
|
+
* @param {V[]} [arr3] - The third array to zip (optional).
|
|
920
|
+
* @param {W[]} [arr4] - The fourth array to zip (optional).
|
|
921
|
+
* @param {function} combine - The combiner function that takes corresponding elements from each array and returns a single value.
|
|
922
|
+
* @returns {R[]} A new array where each element is the result of applying the combiner function to the corresponding elements of the input arrays.
|
|
923
|
+
*
|
|
924
|
+
* @example
|
|
925
|
+
* // Example usage with two arrays:
|
|
926
|
+
* const arr1 = [1, 2, 3];
|
|
927
|
+
* const arr2 = [4, 5, 6];
|
|
928
|
+
* const result = zipWith(arr1, arr2, (a, b) => a + b);
|
|
929
|
+
* // result will be [5, 7, 9]
|
|
930
|
+
*
|
|
931
|
+
* @example
|
|
932
|
+
* // Example usage with three arrays:
|
|
933
|
+
* const arr1 = [1, 2];
|
|
934
|
+
* const arr2 = [3, 4];
|
|
935
|
+
* const arr3 = [5, 6];
|
|
936
|
+
* const result = zipWith(arr1, arr2, arr3, (a, b, c) => `${a}${b}${c}`);
|
|
937
|
+
* // result will be [`135`, `246`]
|
|
725
938
|
*/
|
|
726
939
|
|
|
727
940
|
function zipWith(arr1) {
|