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/index.js
CHANGED
|
@@ -2,6 +2,219 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
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
|
+
|
|
5
218
|
function _iterableToArrayLimit(r, l) {
|
|
6
219
|
var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
|
|
7
220
|
if (null != t) {
|
|
@@ -165,28 +378,47 @@ function _createForOfIteratorHelper(o, allowArrayLike) {
|
|
|
165
378
|
}
|
|
166
379
|
|
|
167
380
|
/**
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
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
|
+
* // }
|
|
173
409
|
*/
|
|
174
|
-
function
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
178
|
-
var result = [];
|
|
179
|
-
var _iterator = _createForOfIteratorHelper(arr.entries()),
|
|
410
|
+
function groupBy(arr, getKeyFromItem) {
|
|
411
|
+
var result = {};
|
|
412
|
+
var _iterator = _createForOfIteratorHelper(arr),
|
|
180
413
|
_step;
|
|
181
414
|
try {
|
|
182
415
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
183
|
-
var
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
result.push([]);
|
|
416
|
+
var item = _step.value;
|
|
417
|
+
var key = getKeyFromItem(item);
|
|
418
|
+
if (result[key] == null) {
|
|
419
|
+
result[key] = [];
|
|
188
420
|
}
|
|
189
|
-
result[
|
|
421
|
+
result[key].push(item);
|
|
190
422
|
}
|
|
191
423
|
} catch (err) {
|
|
192
424
|
_iterator.e(err);
|
|
@@ -197,106 +429,21 @@ function chunk(arr, size) {
|
|
|
197
429
|
}
|
|
198
430
|
|
|
199
431
|
/**
|
|
200
|
-
* @name difference
|
|
201
|
-
* Returns the difference of two arrays.
|
|
202
|
-
* @param firstArr The array to get the difference from
|
|
203
|
-
* @param secondArr The elements to exclude from `firstArr`
|
|
204
|
-
*/
|
|
205
|
-
function difference(firstArr, secondArr) {
|
|
206
|
-
return firstArr.filter(function (item) {
|
|
207
|
-
return !secondArr.includes(item);
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* @name differenceBy
|
|
213
|
-
* Returns the difference of two arrays. The identity of two elements is calculated after the elements are mapped with `mapper`.
|
|
214
|
-
* @param firstArr The array to get the difference from
|
|
215
|
-
* @param secondArr The elements to exclude from `firstArr`
|
|
216
|
-
* @param mapper The function to map the elements when calculating the difference
|
|
217
|
-
*/
|
|
218
|
-
function differenceBy(firstArr, secondArr, mapper) {
|
|
219
|
-
var mappedSecondArr = secondArr.map(function (item) {
|
|
220
|
-
return mapper(item);
|
|
221
|
-
});
|
|
222
|
-
return firstArr.filter(function (item) {
|
|
223
|
-
return !mappedSecondArr.includes(mapper(item));
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* @name differenceWith
|
|
229
|
-
* Returns the difference of two arrays. The identity of two elements is calculated with the function `areItemsEqual`.
|
|
230
|
-
* @param firstArr The array to get the difference from
|
|
231
|
-
* @param secondArr The elements to exclude from `firstArr`
|
|
232
|
-
* @param areItemsEqual The function to determine whether two items are equal
|
|
233
|
-
*/
|
|
234
|
-
function differenceWith(firstArr, secondArr, areItemsEqual) {
|
|
235
|
-
return firstArr.filter(function (firstItem) {
|
|
236
|
-
return secondArr.every(function (secondItem) {
|
|
237
|
-
return !areItemsEqual(firstItem, secondItem);
|
|
238
|
-
});
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* @name drop
|
|
244
|
-
* Drop some elements from an array from the beginning. This function returns a new array.
|
|
245
|
-
* @param arr The element to drop items from the beginning
|
|
246
|
-
* @param itemsCount The amount of elements to drop
|
|
247
|
-
*/
|
|
248
|
-
function drop(arr, itemsCount) {
|
|
249
|
-
return arr.slice(itemsCount);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* @name dropRight
|
|
254
|
-
* Drop some elements from an array from the end. This function returns a new array.
|
|
255
|
-
* @param arr The element to drop items from the end
|
|
256
|
-
* @param itemsCount The amount of elements to drop
|
|
257
|
-
*/
|
|
258
|
-
function dropRight(arr, itemsCount) {
|
|
259
|
-
return arr.slice(0, -itemsCount);
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* @name dropWhile
|
|
264
|
-
* Drop some elements from an array from the beginning, until `predicate` returns false.
|
|
265
|
-
* @param arr The element to drop items from the beginning
|
|
266
|
-
* @param canContinueDropping The predicate to check if the function can continue dropping elements.
|
|
267
|
-
*/
|
|
268
|
-
function dropWhile(arr, canContinueDropping) {
|
|
269
|
-
var dropEndIndex = arr.findIndex(function (item) {
|
|
270
|
-
return !canContinueDropping(item);
|
|
271
|
-
});
|
|
272
|
-
return arr.slice(dropEndIndex);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* @name dropRightWhile
|
|
277
|
-
* Drop some elements from an array from the end, until `predicate` returns false.
|
|
278
|
-
* @param arr The element to drop items from the end
|
|
279
|
-
* @param canContinueDropping The predicate to check if the function can continue dropping elements.
|
|
280
|
-
*/
|
|
281
|
-
function dropRightWhile(arr, canContinueDropping) {
|
|
282
|
-
var reversed = arr.slice().reverse();
|
|
283
|
-
var dropped = dropWhile(reversed, canContinueDropping);
|
|
284
|
-
return dropped.slice().reverse();
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
function groupBy() {
|
|
288
|
-
throw new Error('Not implemented');
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* @name intersection
|
|
293
|
-
* @description
|
|
294
432
|
* Returns the intersection of two arrays.
|
|
295
|
-
*
|
|
296
|
-
*
|
|
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
|
+
*
|
|
297
442
|
* @example
|
|
298
|
-
*
|
|
299
|
-
*
|
|
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.
|
|
300
447
|
*/
|
|
301
448
|
function intersection(firstArr, secondArr) {
|
|
302
449
|
return firstArr.filter(function (item) {
|
|
@@ -305,15 +452,24 @@ function intersection(firstArr, secondArr) {
|
|
|
305
452
|
}
|
|
306
453
|
|
|
307
454
|
/**
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
*
|
|
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
|
+
*
|
|
314
467
|
* @example
|
|
315
|
-
*
|
|
316
|
-
*
|
|
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.
|
|
317
473
|
*/
|
|
318
474
|
function intersectionBy(firstArr, secondArr, mapper) {
|
|
319
475
|
var mappedSecondArr = secondArr.map(function (x) {
|
|
@@ -325,15 +481,25 @@ function intersectionBy(firstArr, secondArr, mapper) {
|
|
|
325
481
|
}
|
|
326
482
|
|
|
327
483
|
/**
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
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
|
+
*
|
|
334
497
|
* @example
|
|
335
|
-
*
|
|
336
|
-
*
|
|
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.
|
|
337
503
|
*/
|
|
338
504
|
function intersectionWith(firstArr, secondArr, areItemsEqual) {
|
|
339
505
|
return firstArr.filter(function (firstItem) {
|
|
@@ -344,11 +510,25 @@ function intersectionWith(firstArr, secondArr, areItemsEqual) {
|
|
|
344
510
|
}
|
|
345
511
|
|
|
346
512
|
/**
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
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]
|
|
352
532
|
*/
|
|
353
533
|
function partition(arr, isInTruthy) {
|
|
354
534
|
var truthy = [];
|
|
@@ -373,10 +553,17 @@ function partition(arr, isInTruthy) {
|
|
|
373
553
|
}
|
|
374
554
|
|
|
375
555
|
/**
|
|
376
|
-
* @name sample
|
|
377
|
-
* @description
|
|
378
556
|
* Returns a random element from an array.
|
|
379
|
-
*
|
|
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.
|
|
380
567
|
*/
|
|
381
568
|
function sample(arr) {
|
|
382
569
|
var randomIndex = Math.floor(Math.random() * arr.length);
|
|
@@ -384,17 +571,24 @@ function sample(arr) {
|
|
|
384
571
|
}
|
|
385
572
|
|
|
386
573
|
/**
|
|
387
|
-
*
|
|
388
|
-
*
|
|
389
|
-
*
|
|
390
|
-
*
|
|
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]
|
|
391
585
|
*/
|
|
392
586
|
function shuffle(arr) {
|
|
393
587
|
/**
|
|
394
588
|
* https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
|
|
395
589
|
*/
|
|
396
590
|
for (var i = arr.length - 1; i >= 1; i--) {
|
|
397
|
-
var j = Math.floor(Math.random() * i);
|
|
591
|
+
var j = Math.floor(Math.random() * (i + 1));
|
|
398
592
|
var _ref = [arr[j], arr[i]];
|
|
399
593
|
arr[i] = _ref[0];
|
|
400
594
|
arr[j] = _ref[1];
|
|
@@ -431,8 +625,6 @@ function take(arr, count) {
|
|
|
431
625
|
/**
|
|
432
626
|
* Returns a new array containing the last `count` elements from the input array `arr`.
|
|
433
627
|
* If `count` is greater than the length of `arr`, the entire array is returned.
|
|
434
|
-
*
|
|
435
|
-
* @template T - Type of elements in the input array.
|
|
436
628
|
*
|
|
437
629
|
* @param {T[]} arr - The array to take elements from.
|
|
438
630
|
* @param {number} count - The number of elements to take.
|
|
@@ -522,43 +714,61 @@ function takeWhile(arr, shouldContinueTaking) {
|
|
|
522
714
|
}
|
|
523
715
|
|
|
524
716
|
/**
|
|
525
|
-
*
|
|
526
|
-
*
|
|
527
|
-
*
|
|
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.
|
|
528
721
|
*
|
|
529
|
-
*
|
|
722
|
+
* @param {T[]} arr - The array to process.
|
|
723
|
+
* @returns {T[]} A new array with only unique values from the original array.
|
|
530
724
|
*
|
|
531
725
|
* @example
|
|
532
|
-
*
|
|
533
|
-
*
|
|
534
|
-
* // [1, 2, 3,
|
|
535
|
-
* ```
|
|
726
|
+
* * const array = [1, 2, 2, 3, 4, 4, 5];
|
|
727
|
+
* const result = uniq(array);
|
|
728
|
+
* // result will be [1, 2, 3, 4, 5]
|
|
536
729
|
*/
|
|
537
730
|
function uniq(arr) {
|
|
538
|
-
|
|
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;
|
|
539
748
|
}
|
|
540
749
|
|
|
541
750
|
/**
|
|
542
|
-
*
|
|
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.
|
|
543
759
|
*
|
|
544
760
|
* @example
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
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]
|
|
549
765
|
*/
|
|
550
|
-
function union() {
|
|
551
|
-
|
|
552
|
-
args[_key] = arguments[_key];
|
|
553
|
-
}
|
|
554
|
-
return uniq(args.flat());
|
|
766
|
+
function union(arr1, arr2) {
|
|
767
|
+
return uniq(arr1.concat(arr2));
|
|
555
768
|
}
|
|
556
769
|
|
|
557
770
|
/**
|
|
558
771
|
* Creates an array of unique values, in order, from all given arrays using a provided mapping function to determine equality.
|
|
559
|
-
*
|
|
560
|
-
* @template T - Type of elements in the input arrays.
|
|
561
|
-
* @template U - Type of the values returned by the mapping function.
|
|
562
772
|
*
|
|
563
773
|
* @param {T[]} arr1 - The first array.
|
|
564
774
|
* @param {T[]} arr2 - The second array.
|
|
@@ -626,20 +836,26 @@ function uniqWith(arr, areItemsEqual) {
|
|
|
626
836
|
}
|
|
627
837
|
|
|
628
838
|
/**
|
|
629
|
-
*
|
|
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.
|
|
630
849
|
*
|
|
631
850
|
* @example
|
|
632
|
-
*
|
|
633
|
-
*
|
|
634
|
-
*
|
|
635
|
-
*
|
|
636
|
-
*
|
|
637
|
-
* );
|
|
638
|
-
* // [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
|
|
639
|
-
* ```
|
|
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
|
|
640
856
|
*/
|
|
641
857
|
function unionWith(arr1, arr2, areItemsEqual) {
|
|
642
|
-
return uniqWith(
|
|
858
|
+
return uniqWith(arr1.concat(arr2), areItemsEqual);
|
|
643
859
|
}
|
|
644
860
|
|
|
645
861
|
/**
|
|
@@ -662,8 +878,6 @@ function uniqBy(arr, converter) {
|
|
|
662
878
|
/**
|
|
663
879
|
* Computes the symmetric difference between two arrays. The symmetric difference is the set of elements
|
|
664
880
|
* which are in either of the arrays, but not in their intersection.
|
|
665
|
-
*
|
|
666
|
-
* @template T - Type of elements in the input arrays.
|
|
667
881
|
*
|
|
668
882
|
* @param {T[]} arr1 - The first array.
|
|
669
883
|
* @param {T[]} arr2 - The second array.
|
|
@@ -731,13 +945,25 @@ function xorWith(arr1, arr2, areElementsEqual) {
|
|
|
731
945
|
}
|
|
732
946
|
|
|
733
947
|
/**
|
|
734
|
-
*
|
|
735
|
-
* elements from the input arrays at the corresponding index.
|
|
948
|
+
* Combines multiple arrays into a single array of tuples.
|
|
736
949
|
*
|
|
737
|
-
*
|
|
738
|
-
*
|
|
739
|
-
*
|
|
740
|
-
*
|
|
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]]
|
|
741
967
|
*/
|
|
742
968
|
|
|
743
969
|
function zip() {
|
|
@@ -768,9 +994,32 @@ function zip() {
|
|
|
768
994
|
}
|
|
769
995
|
|
|
770
996
|
/**
|
|
771
|
-
* Combines multiple arrays
|
|
772
|
-
*
|
|
773
|
-
*
|
|
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`]
|
|
774
1023
|
*/
|
|
775
1024
|
|
|
776
1025
|
function zipWith(arr1) {
|
|
@@ -796,12 +1045,12 @@ function zipWith(arr1) {
|
|
|
796
1045
|
}
|
|
797
1046
|
|
|
798
1047
|
/**
|
|
799
|
-
* Creates a debounced function that delays invoking the provided function until after `
|
|
1048
|
+
* Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
|
|
800
1049
|
* have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
|
|
801
1050
|
* method to cancel any pending execution.
|
|
802
1051
|
*
|
|
803
1052
|
* @param {() => void} func - The function to debounce.
|
|
804
|
-
* @param {number}
|
|
1053
|
+
* @param {number} debounceMs - The number of milliseconds to delay.
|
|
805
1054
|
* @returns {{ (): void; cancel: () => void }} A new debounced function with a `cancel` method.
|
|
806
1055
|
*
|
|
807
1056
|
* @example
|
|
@@ -815,7 +1064,7 @@ function zipWith(arr1) {
|
|
|
815
1064
|
* // Will not log anything as the previous call is canceled
|
|
816
1065
|
* debouncedFunction.cancel();
|
|
817
1066
|
*/
|
|
818
|
-
function debounce(func,
|
|
1067
|
+
function debounce(func, debounceMs) {
|
|
819
1068
|
var timeoutId = null;
|
|
820
1069
|
var debounced = function debounced() {
|
|
821
1070
|
if (timeoutId != null) {
|
|
@@ -823,7 +1072,7 @@ function debounce(func, waitMs) {
|
|
|
823
1072
|
}
|
|
824
1073
|
timeoutId = setTimeout(function () {
|
|
825
1074
|
func();
|
|
826
|
-
},
|
|
1075
|
+
}, debounceMs);
|
|
827
1076
|
};
|
|
828
1077
|
debounced.cancel = function () {
|
|
829
1078
|
if (timeoutId != null) {
|
|
@@ -837,7 +1086,6 @@ function debounce(func, waitMs) {
|
|
|
837
1086
|
* Creates a function that is restricted to invoking the provided function `func` once.
|
|
838
1087
|
* Repeated calls to the function will return the value from the first invocation.
|
|
839
1088
|
*
|
|
840
|
-
* @template F - The type of the function to be invoked once.
|
|
841
1089
|
* @param {F} func - The function to restrict.
|
|
842
1090
|
* @returns {F} A new function that invokes `func` once and caches the result.
|
|
843
1091
|
*
|
|
@@ -902,15 +1150,23 @@ function throttle(func, throttleMs) {
|
|
|
902
1150
|
}
|
|
903
1151
|
|
|
904
1152
|
/**
|
|
905
|
-
*
|
|
906
|
-
*
|
|
907
|
-
* function
|
|
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
|
+
*
|
|
908
1163
|
* @example
|
|
909
|
-
* clamp(
|
|
910
|
-
* clamp(
|
|
911
|
-
* clamp(
|
|
912
|
-
* clamp(
|
|
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
|
|
913
1168
|
*/
|
|
1169
|
+
|
|
914
1170
|
function clamp(value, bound1, bound2) {
|
|
915
1171
|
if (bound2 == null) {
|
|
916
1172
|
return Math.min(value, bound1);
|
|
@@ -919,16 +1175,19 @@ function clamp(value, bound1, bound2) {
|
|
|
919
1175
|
}
|
|
920
1176
|
|
|
921
1177
|
/**
|
|
922
|
-
*
|
|
923
|
-
* @description Rounds given number to given precision
|
|
924
|
-
* ```typescript
|
|
925
|
-
* function round(value: number, precision: number = 0): number
|
|
926
|
-
* ```
|
|
1178
|
+
* Rounds a number to a specified precision.
|
|
927
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
|
+
*
|
|
928
1187
|
* @example
|
|
929
|
-
* round(1.
|
|
930
|
-
* round(
|
|
931
|
-
* round(
|
|
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
|
|
932
1191
|
*/
|
|
933
1192
|
function round(value) {
|
|
934
1193
|
var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
@@ -937,31 +1196,49 @@ function round(value) {
|
|
|
937
1196
|
}
|
|
938
1197
|
|
|
939
1198
|
/**
|
|
940
|
-
*
|
|
941
|
-
*
|
|
942
|
-
*
|
|
943
|
-
*
|
|
944
|
-
*
|
|
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.
|
|
945
1205
|
*
|
|
946
1206
|
* @example
|
|
947
|
-
*
|
|
948
|
-
* sum(
|
|
949
|
-
*
|
|
1207
|
+
* * const numbers = [1, 2, 3, 4, 5];
|
|
1208
|
+
* const result = sum(numbers);
|
|
1209
|
+
* // result will be 15
|
|
950
1210
|
*/
|
|
951
|
-
function sum() {
|
|
952
|
-
|
|
953
|
-
|
|
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();
|
|
954
1224
|
}
|
|
955
|
-
return
|
|
956
|
-
return acc + curr;
|
|
957
|
-
}, 0);
|
|
1225
|
+
return result;
|
|
958
1226
|
}
|
|
959
1227
|
|
|
960
1228
|
/**
|
|
961
|
-
*
|
|
962
|
-
*
|
|
963
|
-
*
|
|
964
|
-
*
|
|
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 }
|
|
965
1242
|
*/
|
|
966
1243
|
function omit(obj, keys) {
|
|
967
1244
|
var result = _objectSpread2({}, obj);
|
|
@@ -980,15 +1257,52 @@ function omit(obj, keys) {
|
|
|
980
1257
|
return result;
|
|
981
1258
|
}
|
|
982
1259
|
|
|
983
|
-
|
|
984
|
-
|
|
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;
|
|
985
1290
|
}
|
|
986
1291
|
|
|
987
1292
|
/**
|
|
988
|
-
*
|
|
989
|
-
*
|
|
990
|
-
*
|
|
991
|
-
*
|
|
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 }
|
|
992
1306
|
*/
|
|
993
1307
|
function pick(obj, keys) {
|
|
994
1308
|
var result = {};
|
|
@@ -1007,56 +1321,145 @@ function pick(obj, keys) {
|
|
|
1007
1321
|
return result;
|
|
1008
1322
|
}
|
|
1009
1323
|
|
|
1010
|
-
|
|
1011
|
-
|
|
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;
|
|
1012
1354
|
}
|
|
1013
1355
|
|
|
1014
1356
|
/**
|
|
1015
|
-
*
|
|
1016
|
-
*
|
|
1017
|
-
*
|
|
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
|
|
1018
1374
|
*/
|
|
1019
1375
|
function isNil(x) {
|
|
1020
1376
|
return x == null || x == undefined;
|
|
1021
1377
|
}
|
|
1022
1378
|
|
|
1023
1379
|
/**
|
|
1024
|
-
* @name isNotNil
|
|
1025
1380
|
* Checks if the given value is not null nor undefined.
|
|
1026
|
-
*
|
|
1027
|
-
*
|
|
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
|
+
*
|
|
1028
1387
|
* @example
|
|
1029
1388
|
* // Here the type of `arr` is (number | undefined)[]
|
|
1030
1389
|
* const arr = [1, undefined, 3];
|
|
1031
1390
|
* // Here the type of `result` is number[]
|
|
1032
1391
|
* const result = arr.filter(isNotNil);
|
|
1392
|
+
* // result will be [1, 3]
|
|
1033
1393
|
*/
|
|
1034
1394
|
function isNotNil(x) {
|
|
1035
1395
|
return x != null && x != undefined;
|
|
1036
1396
|
}
|
|
1037
1397
|
|
|
1038
1398
|
/**
|
|
1039
|
-
* @name isNull
|
|
1040
1399
|
* Checks if the given value is null.
|
|
1041
|
-
*
|
|
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
|
|
1042
1417
|
*/
|
|
1043
1418
|
function isNull(x) {
|
|
1044
1419
|
return x === null;
|
|
1045
1420
|
}
|
|
1046
1421
|
|
|
1047
1422
|
/**
|
|
1048
|
-
* @name isUndefined
|
|
1049
1423
|
* Checks if the given value is undefined.
|
|
1050
|
-
*
|
|
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
|
|
1051
1441
|
*/
|
|
1052
1442
|
function isUndefined(x) {
|
|
1053
1443
|
return x === undefined;
|
|
1054
1444
|
}
|
|
1055
1445
|
|
|
1056
1446
|
/**
|
|
1057
|
-
*
|
|
1058
|
-
*
|
|
1059
|
-
*
|
|
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();
|
|
1060
1463
|
*/
|
|
1061
1464
|
function delay(ms) {
|
|
1062
1465
|
return new Promise(function (resolve) {
|