es-toolkit 0.0.1-dev.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/README.md +31 -0
- package/dist/array/chunk.d.ts +24 -0
- package/dist/array/chunk.spec.d.ts +1 -0
- package/dist/array/difference.d.ts +23 -0
- package/dist/array/difference.spec.d.ts +1 -0
- package/dist/array/differenceBy.d.ts +26 -0
- package/dist/array/differenceBy.spec.d.ts +1 -0
- package/dist/array/differenceWith.d.ts +22 -0
- package/dist/array/differenceWith.spec.d.ts +1 -0
- package/dist/array/drop.d.ts +16 -0
- package/dist/array/drop.spec.d.ts +1 -0
- package/dist/array/dropRight.d.ts +16 -0
- package/dist/array/dropRight.spec.d.ts +1 -0
- package/dist/array/dropRightWhile.d.ts +18 -0
- package/dist/array/dropRightWhile.spec.d.ts +1 -0
- package/dist/array/dropWhile.d.ts +18 -0
- package/dist/array/dropWhile.spec.d.ts +1 -0
- package/dist/array/groupBy.d.ts +31 -0
- package/dist/array/groupBy.spec.d.ts +1 -0
- package/dist/array/index.d.ts +30 -0
- package/dist/array/index.js +991 -0
- package/dist/array/intersection.d.ts +18 -0
- package/dist/array/intersection.spec.d.ts +1 -0
- package/dist/array/intersectionBy.d.ts +21 -0
- package/dist/array/intersectionBy.spec.d.ts +1 -0
- package/dist/array/intersectionWith.d.ts +22 -0
- package/dist/array/intersectionWith.spec.d.ts +1 -0
- package/dist/array/partition.d.ts +22 -0
- package/dist/array/partition.spec.d.ts +1 -0
- package/dist/array/sample.d.ts +14 -0
- package/dist/array/sample.spec.d.ts +1 -0
- package/dist/array/shuffle.d.ts +14 -0
- package/dist/array/shuffle.spec.d.ts +1 -0
- package/dist/array/take.d.ts +23 -0
- package/dist/array/take.spec.d.ts +1 -0
- package/dist/array/takeRight.d.ts +21 -0
- package/dist/array/takeRight.spec.d.ts +1 -0
- package/dist/array/takeRightWhile.d.ts +18 -0
- package/dist/array/takeRightWhile.spec.d.ts +1 -0
- package/dist/array/takeWhile.d.ts +19 -0
- package/dist/array/takeWhile.spec.d.ts +1 -0
- package/dist/array/union.d.ts +17 -0
- package/dist/array/union.spec.d.ts +1 -0
- package/dist/array/unionBy.d.ts +21 -0
- package/dist/array/unionBy.spec.d.ts +1 -0
- package/dist/array/unionWith.d.ts +20 -0
- package/dist/array/unionWith.spec.d.ts +1 -0
- package/dist/array/uniq.d.ts +15 -0
- package/dist/array/uniq.spec.d.ts +1 -0
- package/dist/array/uniqBy.d.ts +12 -0
- package/dist/array/uniqBy.spec.d.ts +1 -0
- package/dist/array/uniqWith.d.ts +12 -0
- package/dist/array/uniqWith.spec.d.ts +1 -0
- package/dist/array/xor.d.ts +17 -0
- package/dist/array/xor.spec.d.ts +1 -0
- package/dist/array/xorBy.d.ts +20 -0
- package/dist/array/xorBy.spec.d.ts +1 -0
- package/dist/array/xorWith.d.ts +19 -0
- package/dist/array/xorWith.spec.d.ts +1 -0
- package/dist/array/zip.d.ts +25 -0
- package/dist/array/zip.spec.d.ts +1 -0
- package/dist/array/zipWith.d.ts +32 -0
- package/dist/array/zipWith.spec.d.ts +1 -0
- package/dist/function/debounce.d.ts +24 -0
- package/dist/function/debounce.spec.d.ts +1 -0
- package/dist/function/index.d.ts +3 -0
- package/dist/function/index.js +112 -0
- package/dist/function/once.d.ts +17 -0
- package/dist/function/once.spec.d.ts +1 -0
- package/dist/function/throttle.d.ts +26 -0
- package/dist/function/throttle.spec.d.ts +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +1514 -0
- package/dist/math/clamp.d.ts +19 -0
- package/dist/math/clamp.spec.d.ts +1 -0
- package/dist/math/index.d.ts +3 -0
- package/dist/math/index.js +148 -0
- package/dist/math/round.d.ts +16 -0
- package/dist/math/round.spec.d.ts +1 -0
- package/dist/math/sum.d.ts +14 -0
- package/dist/math/sum.spec.d.ts +1 -0
- package/dist/object/index.d.ts +4 -0
- package/dist/object/index.js +286 -0
- package/dist/object/omit.d.ts +16 -0
- package/dist/object/omit.spec.d.ts +1 -0
- package/dist/object/omitBy.d.ts +19 -0
- package/dist/object/omitBy.spec.d.ts +1 -0
- package/dist/object/pick.d.ts +16 -0
- package/dist/object/pick.spec.d.ts +1 -0
- package/dist/object/pickBy.d.ts +19 -0
- package/dist/object/pickBy.spec.d.ts +1 -0
- package/dist/predicate/index.d.ts +4 -0
- package/dist/predicate/index.js +98 -0
- package/dist/predicate/isNil.d.ts +20 -0
- package/dist/predicate/isNil.spec.d.ts +1 -0
- package/dist/predicate/isNotNil.d.ts +16 -0
- package/dist/predicate/isNotNil.spec.d.ts +1 -0
- package/dist/predicate/isNull.d.ts +21 -0
- package/dist/predicate/isNull.spec.d.ts +1 -0
- package/dist/predicate/isUndefined.d.ts +21 -0
- package/dist/predicate/isUndefined.spec.d.ts +1 -0
- package/dist/promise/delay.d.ts +19 -0
- package/dist/promise/delay.spec.d.ts +1 -0
- package/dist/promise/index.d.ts +1 -0
- package/dist/promise/index.js +29 -0
- package/esm/_virtual/_rollupPluginBabelHelpers.mjs +163 -0
- package/esm/array/_virtual/_rollupPluginBabelHelpers.mjs +78 -0
- package/esm/array/chunk.mjs +38 -0
- package/esm/array/difference.mjs +29 -0
- package/esm/array/differenceBy.mjs +35 -0
- package/esm/array/differenceWith.mjs +30 -0
- package/esm/array/drop.mjs +20 -0
- package/esm/array/dropRight.mjs +20 -0
- package/esm/array/dropRightWhile.mjs +26 -0
- package/esm/array/dropWhile.mjs +25 -0
- package/esm/array/groupBy.mjs +54 -0
- package/esm/array/index.mjs +30 -0
- package/esm/array/intersection.mjs +24 -0
- package/esm/array/intersectionBy.mjs +30 -0
- package/esm/array/intersectionWith.mjs +30 -0
- package/esm/array/partition.mjs +46 -0
- package/esm/array/sample.mjs +19 -0
- package/esm/array/shuffle.mjs +27 -0
- package/esm/array/take.mjs +27 -0
- package/esm/array/takeRight.mjs +28 -0
- package/esm/array/takeRightWhile.mjs +27 -0
- package/esm/array/takeWhile.mjs +41 -0
- package/esm/array/union.mjs +23 -0
- package/esm/array/unionBy.mjs +35 -0
- package/esm/array/unionWith.mjs +26 -0
- package/esm/array/uniq.mjs +37 -0
- package/esm/array/uniqBy.mjs +20 -0
- package/esm/array/uniqWith.mjs +39 -0
- package/esm/array/xor.mjs +25 -0
- package/esm/array/xorBy.mjs +30 -0
- package/esm/array/xorWith.mjs +29 -0
- package/esm/array/zip.mjs +52 -0
- package/esm/array/zipWith.mjs +54 -0
- package/esm/function/debounce.mjs +39 -0
- package/esm/function/index.mjs +3 -0
- package/esm/function/once.mjs +31 -0
- package/esm/function/throttle.mjs +38 -0
- package/esm/index.mjs +45 -0
- package/esm/math/_virtual/_rollupPluginBabelHelpers.mjs +66 -0
- package/esm/math/clamp.mjs +26 -0
- package/esm/math/index.mjs +3 -0
- package/esm/math/round.mjs +22 -0
- package/esm/math/sum.mjs +33 -0
- package/esm/object/_virtual/_rollupPluginBabelHelpers.mjs +151 -0
- package/esm/object/index.mjs +4 -0
- package/esm/object/omit.mjs +35 -0
- package/esm/object/omitBy.mjs +35 -0
- package/esm/object/pick.mjs +35 -0
- package/esm/object/pickBy.mjs +35 -0
- package/esm/predicate/index.mjs +4 -0
- package/esm/predicate/isNil.mjs +24 -0
- package/esm/predicate/isNotNil.mjs +20 -0
- package/esm/predicate/isNull.mjs +25 -0
- package/esm/predicate/isUndefined.mjs +25 -0
- package/esm/promise/delay.mjs +25 -0
- package/esm/promise/index.mjs +1 -0
- package/package.json +108 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the intersection of two arrays.
|
|
3
|
+
*
|
|
4
|
+
* This function takes two arrays and returns a new array containing the elements that are
|
|
5
|
+
* present in both arrays. It effectively filters out any elements from the first array that
|
|
6
|
+
* are not found in the second array.
|
|
7
|
+
*
|
|
8
|
+
* @param {T[]} firstArr - The first array to compare.
|
|
9
|
+
* @param {T[]} secondArr - The second array to compare.
|
|
10
|
+
* @returns {T[]} A new array containing the elements that are present in both arrays.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const array1 = [1, 2, 3, 4, 5];
|
|
14
|
+
* const array2 = [3, 4, 5, 6, 7];
|
|
15
|
+
* const result = intersection(array1, array2);
|
|
16
|
+
* // result will be [3, 4, 5] since these elements are in both arrays.
|
|
17
|
+
*/
|
|
18
|
+
export declare function intersection<T>(firstArr: T[], secondArr: T[]): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the intersection of two arrays based on a mapping function.
|
|
3
|
+
*
|
|
4
|
+
* This function takes two arrays and a mapping function. It returns a new array containing
|
|
5
|
+
* the elements from the first array that, when mapped using the provided function, have matching
|
|
6
|
+
* mapped elements in the second array. It effectively filters out any elements from the first array
|
|
7
|
+
* that do not have corresponding mapped values in the second array.
|
|
8
|
+
*
|
|
9
|
+
* @param {T[]} firstArr - The first array to compare.
|
|
10
|
+
* @param {T[]} secondArr - The second array to compare.
|
|
11
|
+
* @param {(item: T) => U} mapper - A function to map the elements of both arrays for comparison.
|
|
12
|
+
* @returns {T[]} A new array containing the elements from the first array that have corresponding mapped values in the second array.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }];
|
|
16
|
+
* const array2 = [{ id: 2 }, { id: 4 }];
|
|
17
|
+
* const mapper = item => item.id;
|
|
18
|
+
* const result = intersectionBy(array1, array2, mapper);
|
|
19
|
+
* // result will be [{ id: 2 }] since only this element has a matching id in both arrays.
|
|
20
|
+
*/
|
|
21
|
+
export declare function intersectionBy<T, U>(firstArr: T[], secondArr: T[], mapper: (item: T) => U): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the intersection of two arrays based on a custom equality function.
|
|
3
|
+
*
|
|
4
|
+
* This function takes two arrays and a custom equality function. It returns a new array containing
|
|
5
|
+
* the elements from the first array that have matching elements in the second array, as determined
|
|
6
|
+
* by the custom equality function. It effectively filters out any elements from the first array that
|
|
7
|
+
* do not have corresponding matches in the second array according to the equality function.
|
|
8
|
+
*
|
|
9
|
+
* @param {T[]} firstArr - The first array to compare.
|
|
10
|
+
* @param {T[]} secondArr - The second array to compare.
|
|
11
|
+
* @param {(x: T, y: T) => boolean} areItemsEqual - A custom function to determine if two elements are equal.
|
|
12
|
+
* This function takes two arguments, one from each array, and returns `true` if the elements are considered equal, and `false` otherwise.
|
|
13
|
+
* @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.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* * const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }];
|
|
17
|
+
* const array2 = [{ id: 2 }, { id: 4 }];
|
|
18
|
+
* const areItemsEqual = (a, b) => a.id === b.id;
|
|
19
|
+
* const result = intersectionWith(array1, array2, areItemsEqual);
|
|
20
|
+
* // result will be [{ id: 2 }] since this element has a matching id in both arrays.
|
|
21
|
+
*/
|
|
22
|
+
export declare function intersectionWith<T>(firstArr: T[], secondArr: T[], areItemsEqual: (x: T, y: T) => boolean): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits an array into two groups based on a predicate function.
|
|
3
|
+
*
|
|
4
|
+
* This function takes an array and a predicate function. It returns a tuple of two arrays:
|
|
5
|
+
* the first array contains elements for which the predicate function returns true, and
|
|
6
|
+
* the second array contains elements for which the predicate function returns false.
|
|
7
|
+
*
|
|
8
|
+
* @param {T[]} arr - The array to partition.
|
|
9
|
+
* @param {(value: T) => boolean} isInTruthy - A predicate function that determines
|
|
10
|
+
* whether an element should be placed in the truthy array. The function is called with each
|
|
11
|
+
* element of the array.
|
|
12
|
+
* @returns {[T[], T[]]} A tuple containing two arrays: the first array contains elements for
|
|
13
|
+
* which the predicate returned true, and the second array contains elements for which the
|
|
14
|
+
* predicate returned false.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* const array = [1, 2, 3, 4, 5];
|
|
18
|
+
* const isEven = x => x % 2 === 0;
|
|
19
|
+
* const [even, odd] = partition(array, isEven);
|
|
20
|
+
* // even will be [2, 4], and odd will be [1, 3, 5]
|
|
21
|
+
*/
|
|
22
|
+
export declare function partition<T>(arr: T[], isInTruthy: (value: T) => boolean): [truthy: T[], falsy: T[]];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a random element from an array.
|
|
3
|
+
*
|
|
4
|
+
* This function takes an array and returns a single element selected randomly from the array.
|
|
5
|
+
*
|
|
6
|
+
* @param {T[]} arr - The array to sample from.
|
|
7
|
+
* @returns {T} A random element from the array.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const array = [1, 2, 3, 4, 5];
|
|
11
|
+
* const randomElement = sample(array);
|
|
12
|
+
* // randomElement will be one of the elements from the array, selected randomly.
|
|
13
|
+
*/
|
|
14
|
+
export declare function sample<T>(arr: T[]): T;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Randomizes the order of elements in an array using the Fisher-Yates algorithm.
|
|
3
|
+
*
|
|
4
|
+
* This function takes an array and returns a new array with its elements shuffled in a random order.
|
|
5
|
+
*
|
|
6
|
+
* @param {T[]} arr - The array to shuffle.
|
|
7
|
+
* @returns {T[]} A new array with its elements shuffled in random order.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const array = [1, 2, 3, 4, 5];
|
|
11
|
+
* const shuffledArray = shuffle(array);
|
|
12
|
+
* // shuffledArray will be a new array with elements of array in random order, e.g., [3, 1, 4, 5, 2]
|
|
13
|
+
*/
|
|
14
|
+
export declare function shuffle<T>(arr: T[]): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a new array containing the first `count` elements from the input array `arr`.
|
|
3
|
+
* If `count` is greater than the length of `arr`, the entire array is returned.
|
|
4
|
+
*
|
|
5
|
+
* @template T - Type of elements in the input array.
|
|
6
|
+
*
|
|
7
|
+
* @param {T[]} arr - The array to take elements from.
|
|
8
|
+
* @param {number} count - The number of elements to take.
|
|
9
|
+
* @returns {T[]} A new array containing the first `count` elements from `arr`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // Returns [1, 2, 3]
|
|
13
|
+
* take([1, 2, 3, 4, 5], 3);
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Returns ['a', 'b']
|
|
17
|
+
* take(['a', 'b', 'c'], 2);
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // Returns [1, 2, 3]
|
|
21
|
+
* take([1, 2, 3], 5);
|
|
22
|
+
*/
|
|
23
|
+
export declare function take<T>(arr: T[], count: number): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a new array containing the last `count` elements from the input array `arr`.
|
|
3
|
+
* If `count` is greater than the length of `arr`, the entire array is returned.
|
|
4
|
+
*
|
|
5
|
+
* @param {T[]} arr - The array to take elements from.
|
|
6
|
+
* @param {number} count - The number of elements to take.
|
|
7
|
+
* @returns {T[]} A new array containing the last `count` elements from `arr`.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // Returns [4, 5]
|
|
11
|
+
* takeRight([1, 2, 3, 4, 5], 2);
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // Returns ['b', 'c']
|
|
15
|
+
* takeRight(['a', 'b', 'c'], 2);
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // Returns [1, 2, 3]
|
|
19
|
+
* takeRight([1, 2, 3], 5);
|
|
20
|
+
*/
|
|
21
|
+
export declare function takeRight<T>(arr: T[], count: number): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Takes elements from the end of the array while the predicate function returns `true`.
|
|
3
|
+
*
|
|
4
|
+
* @template T - Type of elements in the input array.
|
|
5
|
+
*
|
|
6
|
+
* @param {T[]} arr - The array to take elements from.
|
|
7
|
+
* @param {function(T): boolean} shouldContinueTaking - The function invoked per element.
|
|
8
|
+
* @returns {T[]} A new array containing the elements taken from the end while the predicate returns `true`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* // Returns [3, 2, 1]
|
|
12
|
+
* takeRightWhile([5, 4, 3, 2, 1], n => n < 4);
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // Returns []
|
|
16
|
+
* takeRightWhile([1, 2, 3], n => n > 3);
|
|
17
|
+
*/
|
|
18
|
+
export declare function takeRightWhile<T>(arr: T[], shouldContinueTaking: (item: T) => boolean): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a new array containing the leading elements of the provided array
|
|
3
|
+
* that satisfy the provided predicate function. It stops taking elements as soon
|
|
4
|
+
* as an element does not satisfy the predicate.
|
|
5
|
+
*
|
|
6
|
+
* @template T - The type of elements in the array.
|
|
7
|
+
* @param {T[]} arr - The array to process.
|
|
8
|
+
* @param {(element: T) => boolean} shouldContinueTaking - The predicate function that is called with each element. Elements are included in the result as long as this function returns true.
|
|
9
|
+
* @returns {T[]} A new array containing the leading elements that satisfy the predicate.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // Returns [1, 2]
|
|
13
|
+
* takeWhile([1, 2, 3, 4], x => x < 3);
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Returns []
|
|
17
|
+
* takeWhile([1, 2, 3, 4], x => x > 3);
|
|
18
|
+
*/
|
|
19
|
+
export declare function takeWhile<T>(arr: T[], shouldContinueTaking: (element: T) => boolean): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an array of unique values from all given arrays.
|
|
3
|
+
*
|
|
4
|
+
* This function takes two arrays, merges them into a single array, and returns a new array
|
|
5
|
+
* containing only the unique values from the merged array.
|
|
6
|
+
*
|
|
7
|
+
* @param {T[]} arr1 - The first array to merge and filter for unique values.
|
|
8
|
+
* @param {T[]} arr2 - The second array to merge and filter for unique values.
|
|
9
|
+
* @returns {T[]} A new array of unique values.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const array1 = [1, 2, 3];
|
|
13
|
+
* const array2 = [3, 4, 5];
|
|
14
|
+
* const result = union(array1, array2);
|
|
15
|
+
* // result will be [1, 2, 3, 4, 5]
|
|
16
|
+
*/
|
|
17
|
+
export declare function union<T>(arr1: T[], arr2: T[]): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an array of unique values, in order, from all given arrays using a provided mapping function to determine equality.
|
|
3
|
+
*
|
|
4
|
+
* @param {T[]} arr1 - The first array.
|
|
5
|
+
* @param {T[]} arr2 - The second array.
|
|
6
|
+
* @param {function(item: T): U} mapper - The function to map array elements to comparison values.
|
|
7
|
+
* @returns {T[]} A new array containing the union of unique elements from `arr1` and `arr2`, based on the values returned by the mapping function.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // Custom mapping function for numbers (modulo comparison)
|
|
11
|
+
* const moduloMapper = (x) => x % 3;
|
|
12
|
+
* unionBy([1, 2, 3], [4, 5, 6], moduloMapper);
|
|
13
|
+
* // Returns [1, 2, 3]
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Custom mapping function for objects with an 'id' property
|
|
17
|
+
* const idMapper = (obj) => obj.id;
|
|
18
|
+
* unionBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], idMapper);
|
|
19
|
+
* // Returns [{ id: 1 }, { id: 2 }, { id: 3 }]
|
|
20
|
+
*/
|
|
21
|
+
export declare function unionBy<T, U>(arr1: T[], arr2: T[], mapper: (item: T) => U): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an array of unique values from two given arrays based on a custom equality function.
|
|
3
|
+
*
|
|
4
|
+
* This function takes two arrays and a custom equality function, merges the arrays, and returns
|
|
5
|
+
* a new array containing only the unique values as determined by the custom equality function.
|
|
6
|
+
*
|
|
7
|
+
* @param {T[]} arr1 - The first array to merge and filter for unique values.
|
|
8
|
+
* @param {T[]} arr2 - The second array to merge and filter for unique values.
|
|
9
|
+
* @param {(item1: T, item2: T) => boolean} areItemsEqual - A custom function to determine if two elements are equal.
|
|
10
|
+
* It takes two arguments and returns `true` if the elements are considered equal, and `false` otherwise.
|
|
11
|
+
* @returns {T[]} A new array of unique values based on the custom equality function.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const array1 = [{ id: 1 }, { id: 2 }];
|
|
15
|
+
* const array2 = [{ id: 2 }, { id: 3 }];
|
|
16
|
+
* const areItemsEqual = (a, b) => a.id === b.id;
|
|
17
|
+
* const result = unionWith(array1, array2, areItemsEqual);
|
|
18
|
+
* // result will be [{ id: 1 }, { id: 2 }, { id: 3 }] since { id: 2 } is considered equal in both arrays
|
|
19
|
+
*/
|
|
20
|
+
export declare function unionWith<T>(arr1: T[], arr2: T[], areItemsEqual: (item1: T, item2: T) => boolean): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a duplicate-free version of an array.
|
|
3
|
+
*
|
|
4
|
+
* This function takes an array and returns a new array containing only the unique values
|
|
5
|
+
* from the original array, preserving the order of first occurrence.
|
|
6
|
+
*
|
|
7
|
+
* @param {T[]} arr - The array to process.
|
|
8
|
+
* @returns {T[]} A new array with only unique values from the original array.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* * const array = [1, 2, 2, 3, 4, 4, 5];
|
|
12
|
+
* const result = uniq(array);
|
|
13
|
+
* // result will be [1, 2, 3, 4, 5]
|
|
14
|
+
*/
|
|
15
|
+
export declare function uniq<T>(arr: T[]): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `uniqBy` function takes an array as its first argument and a 'converter' function as the second. It maps the array elements using the converter function, then removes any duplicates.
|
|
3
|
+
*
|
|
4
|
+
* It filters out elements with the same value, meaning it does not check for duplicates in data types like Objects.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* uniqBy([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], Math.floor);
|
|
9
|
+
* // [1.2, 2.1, 3.3, 5.7, 7.19]
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare function uniqBy<T, U>(arr: T[], converter: (item: T) => U): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `uniqWith` function takes an array as its first argument and a 'comparator' function as the second.
|
|
3
|
+
*
|
|
4
|
+
* It evaluates the elements of the array using the comparator function, and if true is returned, it considers those elements as duplicates and removes them.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* uniqWith([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], (a, b) => );
|
|
9
|
+
* // [1, 2, 3, 5, 7]
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare function uniqWith<T>(arr: T[], areItemsEqual: (item1: T, item2: T) => boolean): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Computes the symmetric difference between two arrays. The symmetric difference is the set of elements
|
|
3
|
+
* which are in either of the arrays, but not in their intersection.
|
|
4
|
+
*
|
|
5
|
+
* @param {T[]} arr1 - The first array.
|
|
6
|
+
* @param {T[]} arr2 - The second array.
|
|
7
|
+
* @returns {T[]} An array containing the elements that are present in either `arr1` or `arr2` but not in both.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // Returns [1, 2, 5, 6]
|
|
11
|
+
* xor([1, 2, 3, 4], [3, 4, 5, 6]);
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // Returns ['a', 'c']
|
|
15
|
+
* xor(['a', 'b'], ['b', 'c']);
|
|
16
|
+
*/
|
|
17
|
+
export declare function xor<T>(arr1: T[], arr2: T[]): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Computes the symmetric difference between two arrays using a custom mapping function.
|
|
3
|
+
* The symmetric difference is the set of elements which are in either of the arrays,
|
|
4
|
+
* but not in their intersection, determined by the result of the mapping function.
|
|
5
|
+
*
|
|
6
|
+
* @template T - Type of elements in the input arrays.
|
|
7
|
+
* @template U - Type of the values returned by the mapping function.
|
|
8
|
+
*
|
|
9
|
+
* @param {T[]} arr1 - The first array.
|
|
10
|
+
* @param {T[]} arr2 - The second array.
|
|
11
|
+
* @param {function(item: T): U} mapper - The function to map array elements to comparison values.
|
|
12
|
+
* @returns {T[]} An array containing the elements that are present in either `arr1` or `arr2` but not in both, based on the values returned by the mapping function.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // Custom mapping function for objects with an 'id' property
|
|
16
|
+
* const idMapper = obj => obj.id;
|
|
17
|
+
* xorBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], idMapper);
|
|
18
|
+
* // Returns [{ id: 1 }, { id: 3 }]
|
|
19
|
+
*/
|
|
20
|
+
export declare function xorBy<T, U>(arr1: T[], arr2: T[], mapper: (item: T) => U): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Computes the symmetric difference between two arrays using a custom equality function.
|
|
3
|
+
* The symmetric difference is the set of elements which are in either of the arrays,
|
|
4
|
+
* but not in their intersection.
|
|
5
|
+
*
|
|
6
|
+
* @template T - Type of elements in the input arrays.
|
|
7
|
+
*
|
|
8
|
+
* @param {T[]} arr1 - The first array.
|
|
9
|
+
* @param {T[]} arr2 - The second array.
|
|
10
|
+
* @param {(item1: T, item2: T) => boolean} areElementsEqual - The custom equality function to compare elements.
|
|
11
|
+
* @returns {T[]} An array containing the elements that are present in either `arr1` or `arr2` but not in both, based on the custom equality function.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // Custom equality function for objects with an 'id' property
|
|
15
|
+
* const areObjectsEqual = (a, b) => a.id === b.id;
|
|
16
|
+
* xorWith([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], areObjectsEqual);
|
|
17
|
+
* // Returns [{ id: 1 }, { id: 3 }]
|
|
18
|
+
*/
|
|
19
|
+
export declare function xorWith<T>(arr1: T[], arr2: T[], areElementsEqual: (item1: T, item2: T) => boolean): T[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Combines multiple arrays into a single array of tuples.
|
|
3
|
+
*
|
|
4
|
+
* This function takes multiple arrays and returns a new array where each element is a tuple
|
|
5
|
+
* containing the corresponding elements from the input arrays. If the input arrays are of
|
|
6
|
+
* different lengths, the resulting array will have the length of the longest input array,
|
|
7
|
+
* with undefined values for missing elements.
|
|
8
|
+
*
|
|
9
|
+
* @param {...T[][]} arrs - The arrays to zip together.
|
|
10
|
+
* @returns {T[][]} A new array of tuples containing the corresponding elements from the input arrays.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const arr1 = [1, 2, 3];
|
|
14
|
+
* const arr2 = ['a', 'b', 'c'];
|
|
15
|
+
* const result = zip(arr1, arr2);
|
|
16
|
+
* // result will be [[1, 'a'], [2, 'b'], [3, 'c']]
|
|
17
|
+
*
|
|
18
|
+
* const arr3 = [true, false];
|
|
19
|
+
* const result2 = zip(arr1, arr2, arr3);
|
|
20
|
+
* // result2 will be [[1, 'a', true], [2, 'b', false], [3, 'c', undefined]]
|
|
21
|
+
*/
|
|
22
|
+
export declare function zip<T>(arr1: T[]): [T][];
|
|
23
|
+
export declare function zip<T, U>(arr1: T[], arr2: U[]): [T, U][];
|
|
24
|
+
export declare function zip<T, U, V>(arr1: T[], arr2: U[], arr3: V[]): [T, U, V][];
|
|
25
|
+
export declare function zip<T, U, V, W>(arr1: T[], arr2: U[], arr3: V[], arr4: W[]): [T, U, V, W][];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Combines multiple arrays into a single array using a custom combiner function.
|
|
3
|
+
*
|
|
4
|
+
* This function takes multiple arrays and a combiner function, and returns a new array where each element
|
|
5
|
+
* is the result of applying the combiner function to the corresponding elements of the input arrays.
|
|
6
|
+
*
|
|
7
|
+
* @param {T[]} arr1 - The first array to zip.
|
|
8
|
+
* @param {U[]} [arr2] - The second array to zip (optional).
|
|
9
|
+
* @param {V[]} [arr3] - The third array to zip (optional).
|
|
10
|
+
* @param {W[]} [arr4] - The fourth array to zip (optional).
|
|
11
|
+
* @param {function} combine - The combiner function that takes corresponding elements from each array and returns a single value.
|
|
12
|
+
* @returns {R[]} A new array where each element is the result of applying the combiner function to the corresponding elements of the input arrays.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* // Example usage with two arrays:
|
|
16
|
+
* const arr1 = [1, 2, 3];
|
|
17
|
+
* const arr2 = [4, 5, 6];
|
|
18
|
+
* const result = zipWith(arr1, arr2, (a, b) => a + b);
|
|
19
|
+
* // result will be [5, 7, 9]
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* // Example usage with three arrays:
|
|
23
|
+
* const arr1 = [1, 2];
|
|
24
|
+
* const arr2 = [3, 4];
|
|
25
|
+
* const arr3 = [5, 6];
|
|
26
|
+
* const result = zipWith(arr1, arr2, arr3, (a, b, c) => `${a}${b}${c}`);
|
|
27
|
+
* // result will be [`135`, `246`]
|
|
28
|
+
*/
|
|
29
|
+
export declare function zipWith<T, R>(arr1: T[], combine: (item: T) => R): R[];
|
|
30
|
+
export declare function zipWith<T, U, R>(arr1: T[], arr2: U[], combine: (item1: T, item2: U) => R): R[];
|
|
31
|
+
export declare function zipWith<T, U, V, R>(arr1: T[], arr2: U[], arr3: V[], combine: (item1: T, item2: U, item3: V) => R): R[];
|
|
32
|
+
export declare function zipWith<T, U, V, W, R>(arr1: T[], arr2: U[], arr3: V[], arr4: W[], combine: (item1: T, item2: U, item3: V, item4: W) => R): R[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
|
|
3
|
+
* have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
|
|
4
|
+
* method to cancel any pending execution.
|
|
5
|
+
*
|
|
6
|
+
* @param {() => void} func - The function to debounce.
|
|
7
|
+
* @param {number} debounceMs - The number of milliseconds to delay.
|
|
8
|
+
* @returns {{ (): void; cancel: () => void }} A new debounced function with a `cancel` method.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const debouncedFunction = debounce(() => {
|
|
12
|
+
* console.log('Function executed');
|
|
13
|
+
* }, 1000);
|
|
14
|
+
*
|
|
15
|
+
* // Will log 'Function executed' after 1 second if not called again in that time
|
|
16
|
+
* debouncedFunction();
|
|
17
|
+
*
|
|
18
|
+
* // Will not log anything as the previous call is canceled
|
|
19
|
+
* debouncedFunction.cancel();
|
|
20
|
+
*/
|
|
21
|
+
export declare function debounce(func: () => void, debounceMs: number): {
|
|
22
|
+
(): void;
|
|
23
|
+
cancel: () => void;
|
|
24
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
|
|
7
|
+
* have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
|
|
8
|
+
* method to cancel any pending execution.
|
|
9
|
+
*
|
|
10
|
+
* @param {() => void} func - The function to debounce.
|
|
11
|
+
* @param {number} debounceMs - The number of milliseconds to delay.
|
|
12
|
+
* @returns {{ (): void; cancel: () => void }} A new debounced function with a `cancel` method.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const debouncedFunction = debounce(() => {
|
|
16
|
+
* console.log('Function executed');
|
|
17
|
+
* }, 1000);
|
|
18
|
+
*
|
|
19
|
+
* // Will log 'Function executed' after 1 second if not called again in that time
|
|
20
|
+
* debouncedFunction();
|
|
21
|
+
*
|
|
22
|
+
* // Will not log anything as the previous call is canceled
|
|
23
|
+
* debouncedFunction.cancel();
|
|
24
|
+
*/
|
|
25
|
+
function debounce(func, debounceMs) {
|
|
26
|
+
var timeoutId = null;
|
|
27
|
+
var debounced = function debounced() {
|
|
28
|
+
if (timeoutId != null) {
|
|
29
|
+
clearTimeout(timeoutId);
|
|
30
|
+
}
|
|
31
|
+
timeoutId = setTimeout(function () {
|
|
32
|
+
func();
|
|
33
|
+
}, debounceMs);
|
|
34
|
+
};
|
|
35
|
+
debounced.cancel = function () {
|
|
36
|
+
if (timeoutId != null) {
|
|
37
|
+
clearTimeout(timeoutId);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
return debounced;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Creates a function that is restricted to invoking the provided function `func` once.
|
|
45
|
+
* Repeated calls to the function will return the value from the first invocation.
|
|
46
|
+
*
|
|
47
|
+
* @param {F} func - The function to restrict.
|
|
48
|
+
* @returns {F} A new function that invokes `func` once and caches the result.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const initialize = once(() => {
|
|
52
|
+
* console.log('Initialized!');
|
|
53
|
+
* return true;
|
|
54
|
+
* });
|
|
55
|
+
*
|
|
56
|
+
* initialize(); // Logs: 'Initialized!' and returns true
|
|
57
|
+
* initialize(); // Returns true without logging
|
|
58
|
+
*/
|
|
59
|
+
function once(func) {
|
|
60
|
+
var called = false;
|
|
61
|
+
var cache;
|
|
62
|
+
return function () {
|
|
63
|
+
if (called) {
|
|
64
|
+
return cache;
|
|
65
|
+
}
|
|
66
|
+
var result = func();
|
|
67
|
+
called = true;
|
|
68
|
+
cache = result;
|
|
69
|
+
return result;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Creates a throttled function that only invokes the provided function at most once
|
|
75
|
+
* per every `throttleMs` milliseconds. Subsequent calls to the throttled function
|
|
76
|
+
* within the wait time will not trigger the execution of the original function.
|
|
77
|
+
*
|
|
78
|
+
* @param {() => void} func - The function to throttle.
|
|
79
|
+
* @param {number} throttleMs - The number of milliseconds to throttle executions to.
|
|
80
|
+
* @returns {() => void} A new throttled function.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* const throttledFunction = throttle(() => {
|
|
84
|
+
* console.log('Function executed');
|
|
85
|
+
* }, 1000);
|
|
86
|
+
*
|
|
87
|
+
* // Will log 'Function executed' immediately
|
|
88
|
+
* throttledFunction();
|
|
89
|
+
*
|
|
90
|
+
* // Will not log anything as it is within the throttle time
|
|
91
|
+
* throttledFunction();
|
|
92
|
+
*
|
|
93
|
+
* // After 1 second
|
|
94
|
+
* setTimeout(() => {
|
|
95
|
+
* throttledFunction(); // Will log 'Function executed'
|
|
96
|
+
* }, 1000);
|
|
97
|
+
*/
|
|
98
|
+
function throttle(func, throttleMs) {
|
|
99
|
+
var lastCallTime;
|
|
100
|
+
var throttledFunction = function throttledFunction() {
|
|
101
|
+
var now = Date.now();
|
|
102
|
+
if (lastCallTime == null || now - lastCallTime >= throttleMs) {
|
|
103
|
+
lastCallTime = now;
|
|
104
|
+
func();
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
return throttledFunction;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
exports.debounce = debounce;
|
|
111
|
+
exports.once = once;
|
|
112
|
+
exports.throttle = throttle;
|