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,26 @@
|
|
|
1
|
+
import { dropWhile } from './dropWhile.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Removes elements from the end of an array until the predicate returns false.
|
|
5
|
+
*
|
|
6
|
+
* This function iterates over an array from the end and drops elements until the provided
|
|
7
|
+
* predicate function returns false. It then returns a new array with the remaining elements.
|
|
8
|
+
*
|
|
9
|
+
* @param {T[]} arr - The array from which to drop elements.
|
|
10
|
+
* @param {function(item: T): boolean} canContinueDropping - A predicate function that determines
|
|
11
|
+
* whether to continue dropping elements. The function is called with each element from the end,
|
|
12
|
+
* and dropping continues as long as it returns true.
|
|
13
|
+
* @returns {T[]} A new array with the elements remaining after the predicate returns false.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const array = [1, 2, 3, 4, 5];
|
|
17
|
+
* const result = dropRightWhile(array, x => x > 3);
|
|
18
|
+
* // result will be [1, 2, 3] since elements greater than 3 are dropped from the end.
|
|
19
|
+
*/
|
|
20
|
+
function dropRightWhile(arr, canContinueDropping) {
|
|
21
|
+
var reversed = arr.slice().reverse();
|
|
22
|
+
var dropped = dropWhile(reversed, canContinueDropping);
|
|
23
|
+
return dropped.slice().reverse();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { dropRightWhile };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes elements from the beginning of an array until the predicate returns false.
|
|
3
|
+
*
|
|
4
|
+
* This function iterates over an array and drops elements from the start until the provided
|
|
5
|
+
* predicate function returns false. It then returns a new array with the remaining elements.
|
|
6
|
+
*
|
|
7
|
+
* @param {T[]} arr - The array from which to drop elements.
|
|
8
|
+
* @param {(item: T) => boolean} canContinueDropping - A predicate function that determines
|
|
9
|
+
* whether to continue dropping elements. The function is called with each element, and dropping
|
|
10
|
+
* continues as long as it returns true.
|
|
11
|
+
* @returns {T[]} A new array with the elements remaining after the predicate returns false.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const array = [1, 2, 3, 4, 5];
|
|
15
|
+
* const result = dropWhile(array, x => x < 3);
|
|
16
|
+
* // result will be [3, 4, 5] since elements less than 3 are dropped.
|
|
17
|
+
*/
|
|
18
|
+
function dropWhile(arr, canContinueDropping) {
|
|
19
|
+
var dropEndIndex = arr.findIndex(function (item) {
|
|
20
|
+
return !canContinueDropping(item);
|
|
21
|
+
});
|
|
22
|
+
return arr.slice(dropEndIndex);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { dropWhile };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { createForOfIteratorHelper as _createForOfIteratorHelper } from './_virtual/_rollupPluginBabelHelpers.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Groups the elements of an array based on a provided key-generating function.
|
|
5
|
+
*
|
|
6
|
+
* This function takes an array and a function that generates a key from each element. It returns
|
|
7
|
+
* an object where the keys are the generated keys and the values are arrays of elements that share
|
|
8
|
+
* the same key.
|
|
9
|
+
*
|
|
10
|
+
* @param {T[]} arr - The array to group.
|
|
11
|
+
* @param {(item: T) => K} getKeyFromItem - A function that generates a key from an element.
|
|
12
|
+
* @returns {Record<K, T[]>} An object where each key is associated with an array of elements that
|
|
13
|
+
* share that key.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const array = [
|
|
17
|
+
* { category: 'fruit', name: 'apple' },
|
|
18
|
+
* { category: 'fruit', name: 'banana' },
|
|
19
|
+
* { category: 'vegetable', name: 'carrot' }
|
|
20
|
+
* ];
|
|
21
|
+
* const result = groupBy(array, item => item.category);
|
|
22
|
+
* // result will be:
|
|
23
|
+
* // {
|
|
24
|
+
* // fruit: [
|
|
25
|
+
* // { category: 'fruit', name: 'apple' },
|
|
26
|
+
* // { category: 'fruit', name: 'banana' }
|
|
27
|
+
* // ],
|
|
28
|
+
* // vegetable: [
|
|
29
|
+
* // { category: 'vegetable', name: 'carrot' }
|
|
30
|
+
* // ]
|
|
31
|
+
* // }
|
|
32
|
+
*/
|
|
33
|
+
function groupBy(arr, getKeyFromItem) {
|
|
34
|
+
var result = {};
|
|
35
|
+
var _iterator = _createForOfIteratorHelper(arr),
|
|
36
|
+
_step;
|
|
37
|
+
try {
|
|
38
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
39
|
+
var item = _step.value;
|
|
40
|
+
var key = getKeyFromItem(item);
|
|
41
|
+
if (result[key] == null) {
|
|
42
|
+
result[key] = [];
|
|
43
|
+
}
|
|
44
|
+
result[key].push(item);
|
|
45
|
+
}
|
|
46
|
+
} catch (err) {
|
|
47
|
+
_iterator.e(err);
|
|
48
|
+
} finally {
|
|
49
|
+
_iterator.f();
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { groupBy };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export { chunk } from './chunk.mjs';
|
|
2
|
+
export { difference } from './difference.mjs';
|
|
3
|
+
export { differenceBy } from './differenceBy.mjs';
|
|
4
|
+
export { differenceWith } from './differenceWith.mjs';
|
|
5
|
+
export { drop } from './drop.mjs';
|
|
6
|
+
export { dropRight } from './dropRight.mjs';
|
|
7
|
+
export { dropRightWhile } from './dropRightWhile.mjs';
|
|
8
|
+
export { dropWhile } from './dropWhile.mjs';
|
|
9
|
+
export { groupBy } from './groupBy.mjs';
|
|
10
|
+
export { intersection } from './intersection.mjs';
|
|
11
|
+
export { intersectionBy } from './intersectionBy.mjs';
|
|
12
|
+
export { intersectionWith } from './intersectionWith.mjs';
|
|
13
|
+
export { partition } from './partition.mjs';
|
|
14
|
+
export { sample } from './sample.mjs';
|
|
15
|
+
export { shuffle } from './shuffle.mjs';
|
|
16
|
+
export { take } from './take.mjs';
|
|
17
|
+
export { takeRight } from './takeRight.mjs';
|
|
18
|
+
export { takeRightWhile } from './takeRightWhile.mjs';
|
|
19
|
+
export { takeWhile } from './takeWhile.mjs';
|
|
20
|
+
export { union } from './union.mjs';
|
|
21
|
+
export { unionBy } from './unionBy.mjs';
|
|
22
|
+
export { unionWith } from './unionWith.mjs';
|
|
23
|
+
export { uniq } from './uniq.mjs';
|
|
24
|
+
export { uniqBy } from './uniqBy.mjs';
|
|
25
|
+
export { uniqWith } from './uniqWith.mjs';
|
|
26
|
+
export { xor } from './xor.mjs';
|
|
27
|
+
export { xorBy } from './xorBy.mjs';
|
|
28
|
+
export { xorWith } from './xorWith.mjs';
|
|
29
|
+
export { zip } from './zip.mjs';
|
|
30
|
+
export { zipWith } from './zipWith.mjs';
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
function intersection(firstArr, secondArr) {
|
|
19
|
+
return firstArr.filter(function (item) {
|
|
20
|
+
return secondArr.includes(item);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { intersection };
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
function intersectionBy(firstArr, secondArr, mapper) {
|
|
22
|
+
var mappedSecondArr = secondArr.map(function (x) {
|
|
23
|
+
return mapper(x);
|
|
24
|
+
});
|
|
25
|
+
return firstArr.filter(function (item) {
|
|
26
|
+
return mappedSecondArr.includes(mapper(item));
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { intersectionBy };
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
function intersectionWith(firstArr, secondArr, areItemsEqual) {
|
|
23
|
+
return firstArr.filter(function (firstItem) {
|
|
24
|
+
return secondArr.some(function (secondItem) {
|
|
25
|
+
return areItemsEqual(firstItem, secondItem);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { intersectionWith };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createForOfIteratorHelper as _createForOfIteratorHelper } from './_virtual/_rollupPluginBabelHelpers.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Splits an array into two groups based on a predicate function.
|
|
5
|
+
*
|
|
6
|
+
* This function takes an array and a predicate function. It returns a tuple of two arrays:
|
|
7
|
+
* the first array contains elements for which the predicate function returns true, and
|
|
8
|
+
* the second array contains elements for which the predicate function returns false.
|
|
9
|
+
*
|
|
10
|
+
* @param {T[]} arr - The array to partition.
|
|
11
|
+
* @param {(value: T) => boolean} isInTruthy - A predicate function that determines
|
|
12
|
+
* whether an element should be placed in the truthy array. The function is called with each
|
|
13
|
+
* element of the array.
|
|
14
|
+
* @returns {[T[], T[]]} A tuple containing two arrays: the first array contains elements for
|
|
15
|
+
* which the predicate returned true, and the second array contains elements for which the
|
|
16
|
+
* predicate returned false.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const array = [1, 2, 3, 4, 5];
|
|
20
|
+
* const isEven = x => x % 2 === 0;
|
|
21
|
+
* const [even, odd] = partition(array, isEven);
|
|
22
|
+
* // even will be [2, 4], and odd will be [1, 3, 5]
|
|
23
|
+
*/
|
|
24
|
+
function partition(arr, isInTruthy) {
|
|
25
|
+
var truthy = [];
|
|
26
|
+
var falsy = [];
|
|
27
|
+
var _iterator = _createForOfIteratorHelper(arr),
|
|
28
|
+
_step;
|
|
29
|
+
try {
|
|
30
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
31
|
+
var item = _step.value;
|
|
32
|
+
if (isInTruthy(item)) {
|
|
33
|
+
truthy.push(item);
|
|
34
|
+
} else {
|
|
35
|
+
falsy.push(item);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
} catch (err) {
|
|
39
|
+
_iterator.e(err);
|
|
40
|
+
} finally {
|
|
41
|
+
_iterator.f();
|
|
42
|
+
}
|
|
43
|
+
return [truthy, falsy];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { partition };
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
function sample(arr) {
|
|
15
|
+
var randomIndex = Math.floor(Math.random() * arr.length);
|
|
16
|
+
return arr[randomIndex];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { sample };
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
function shuffle(arr) {
|
|
15
|
+
/**
|
|
16
|
+
* https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
|
|
17
|
+
*/
|
|
18
|
+
for (var i = arr.length - 1; i >= 1; i--) {
|
|
19
|
+
var j = Math.floor(Math.random() * (i + 1));
|
|
20
|
+
var _ref = [arr[j], arr[i]];
|
|
21
|
+
arr[i] = _ref[0];
|
|
22
|
+
arr[j] = _ref[1];
|
|
23
|
+
}
|
|
24
|
+
return arr;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { shuffle };
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
function take(arr, count) {
|
|
24
|
+
return arr.slice(0, count);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { take };
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
function takeRight(arr, count) {
|
|
22
|
+
if (count === 0) {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
return arr.slice(-count);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { takeRight };
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
function takeRightWhile(arr, shouldContinueTaking) {
|
|
19
|
+
for (var i = arr.length - 1; i >= 0; i--) {
|
|
20
|
+
if (!shouldContinueTaking(arr[i])) {
|
|
21
|
+
return arr.slice(i + 1);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return arr.slice();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { takeRightWhile };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { createForOfIteratorHelper as _createForOfIteratorHelper } from './_virtual/_rollupPluginBabelHelpers.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns a new array containing the leading elements of the provided array
|
|
5
|
+
* that satisfy the provided predicate function. It stops taking elements as soon
|
|
6
|
+
* as an element does not satisfy the predicate.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of elements in the array.
|
|
9
|
+
* @param {T[]} arr - The array to process.
|
|
10
|
+
* @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.
|
|
11
|
+
* @returns {T[]} A new array containing the leading elements that satisfy the predicate.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // Returns [1, 2]
|
|
15
|
+
* takeWhile([1, 2, 3, 4], x => x < 3);
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // Returns []
|
|
19
|
+
* takeWhile([1, 2, 3, 4], x => x > 3);
|
|
20
|
+
*/
|
|
21
|
+
function takeWhile(arr, shouldContinueTaking) {
|
|
22
|
+
var result = [];
|
|
23
|
+
var _iterator = _createForOfIteratorHelper(arr),
|
|
24
|
+
_step;
|
|
25
|
+
try {
|
|
26
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
27
|
+
var item = _step.value;
|
|
28
|
+
if (!shouldContinueTaking(item)) {
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
result.push(item);
|
|
32
|
+
}
|
|
33
|
+
} catch (err) {
|
|
34
|
+
_iterator.e(err);
|
|
35
|
+
} finally {
|
|
36
|
+
_iterator.f();
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { takeWhile };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { uniq } from './uniq.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates an array of unique values from all given arrays.
|
|
5
|
+
*
|
|
6
|
+
* This function takes two arrays, merges them into a single array, and returns a new array
|
|
7
|
+
* containing only the unique values from the merged array.
|
|
8
|
+
*
|
|
9
|
+
* @param {T[]} arr1 - The first array to merge and filter for unique values.
|
|
10
|
+
* @param {T[]} arr2 - The second array to merge and filter for unique values.
|
|
11
|
+
* @returns {T[]} A new array of unique values.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const array1 = [1, 2, 3];
|
|
15
|
+
* const array2 = [3, 4, 5];
|
|
16
|
+
* const result = union(array1, array2);
|
|
17
|
+
* // result will be [1, 2, 3, 4, 5]
|
|
18
|
+
*/
|
|
19
|
+
function union(arr1, arr2) {
|
|
20
|
+
return uniq(arr1.concat(arr2));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { union };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { toConsumableArray as _toConsumableArray } from './_virtual/_rollupPluginBabelHelpers.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates an array of unique values, in order, from all given arrays using a provided mapping function to determine equality.
|
|
5
|
+
*
|
|
6
|
+
* @param {T[]} arr1 - The first array.
|
|
7
|
+
* @param {T[]} arr2 - The second array.
|
|
8
|
+
* @param {function(item: T): U} mapper - The function to map array elements to comparison values.
|
|
9
|
+
* @returns {T[]} A new array containing the union of unique elements from `arr1` and `arr2`, based on the values returned by the mapping function.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // Custom mapping function for numbers (modulo comparison)
|
|
13
|
+
* const moduloMapper = (x) => x % 3;
|
|
14
|
+
* unionBy([1, 2, 3], [4, 5, 6], moduloMapper);
|
|
15
|
+
* // Returns [1, 2, 3]
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // Custom mapping function for objects with an 'id' property
|
|
19
|
+
* const idMapper = (obj) => obj.id;
|
|
20
|
+
* unionBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], idMapper);
|
|
21
|
+
* // Returns [{ id: 1 }, { id: 2 }, { id: 3 }]
|
|
22
|
+
*/
|
|
23
|
+
function unionBy(arr1, arr2, mapper) {
|
|
24
|
+
var map = new Map();
|
|
25
|
+
for (var _i = 0, _arr = [].concat(_toConsumableArray(arr1), _toConsumableArray(arr2)); _i < _arr.length; _i++) {
|
|
26
|
+
var item = _arr[_i];
|
|
27
|
+
var key = mapper(item);
|
|
28
|
+
if (!map.has(key)) {
|
|
29
|
+
map.set(key, item);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return _toConsumableArray(map.values());
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { unionBy };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { uniqWith } from './uniqWith.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates an array of unique values from two given arrays based on a custom equality function.
|
|
5
|
+
*
|
|
6
|
+
* This function takes two arrays and a custom equality function, merges the arrays, and returns
|
|
7
|
+
* a new array containing only the unique values as determined by the custom equality function.
|
|
8
|
+
*
|
|
9
|
+
* @param {T[]} arr1 - The first array to merge and filter for unique values.
|
|
10
|
+
* @param {T[]} arr2 - The second array to merge and filter for unique values.
|
|
11
|
+
* @param {(item1: T, item2: T) => boolean} areItemsEqual - A custom function to determine if two elements are equal.
|
|
12
|
+
* It takes two arguments and returns `true` if the elements are considered equal, and `false` otherwise.
|
|
13
|
+
* @returns {T[]} A new array of unique values based on the custom equality function.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const array1 = [{ id: 1 }, { id: 2 }];
|
|
17
|
+
* const array2 = [{ id: 2 }, { id: 3 }];
|
|
18
|
+
* const areItemsEqual = (a, b) => a.id === b.id;
|
|
19
|
+
* const result = unionWith(array1, array2, areItemsEqual);
|
|
20
|
+
* // result will be [{ id: 1 }, { id: 2 }, { id: 3 }] since { id: 2 } is considered equal in both arrays
|
|
21
|
+
*/
|
|
22
|
+
function unionWith(arr1, arr2, areItemsEqual) {
|
|
23
|
+
return uniqWith(arr1.concat(arr2), areItemsEqual);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { unionWith };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { createForOfIteratorHelper as _createForOfIteratorHelper } from './_virtual/_rollupPluginBabelHelpers.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a duplicate-free version of an array.
|
|
5
|
+
*
|
|
6
|
+
* This function takes an array and returns a new array containing only the unique values
|
|
7
|
+
* from the original array, preserving the order of first occurrence.
|
|
8
|
+
*
|
|
9
|
+
* @param {T[]} arr - The array to process.
|
|
10
|
+
* @returns {T[]} A new array with only unique values from the original array.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* * const array = [1, 2, 2, 3, 4, 4, 5];
|
|
14
|
+
* const result = uniq(array);
|
|
15
|
+
* // result will be [1, 2, 3, 4, 5]
|
|
16
|
+
*/
|
|
17
|
+
function uniq(arr) {
|
|
18
|
+
var result = [];
|
|
19
|
+
var _iterator = _createForOfIteratorHelper(arr),
|
|
20
|
+
_step;
|
|
21
|
+
try {
|
|
22
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
23
|
+
var item = _step.value;
|
|
24
|
+
if (result.includes(item)) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
result.push(item);
|
|
28
|
+
}
|
|
29
|
+
} catch (err) {
|
|
30
|
+
_iterator.e(err);
|
|
31
|
+
} finally {
|
|
32
|
+
_iterator.f();
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { uniq };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { uniqWith } from './uniqWith.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 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.
|
|
5
|
+
*
|
|
6
|
+
* It filters out elements with the same value, meaning it does not check for duplicates in data types like Objects.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* uniqBy([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], Math.floor);
|
|
11
|
+
* // [1.2, 2.1, 3.3, 5.7, 7.19]
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
function uniqBy(arr, converter) {
|
|
15
|
+
return uniqWith(arr, function (item1, item2) {
|
|
16
|
+
return converter(item1) === converter(item2);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { uniqBy };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { createForOfIteratorHelper as _createForOfIteratorHelper } from './_virtual/_rollupPluginBabelHelpers.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The `uniqWith` function takes an array as its first argument and a 'comparator' function as the second.
|
|
5
|
+
*
|
|
6
|
+
* 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.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* uniqWith([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], (a, b) => );
|
|
11
|
+
* // [1, 2, 3, 5, 7]
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
function uniqWith(arr, areItemsEqual) {
|
|
15
|
+
var result = [];
|
|
16
|
+
var _iterator = _createForOfIteratorHelper(arr),
|
|
17
|
+
_step;
|
|
18
|
+
try {
|
|
19
|
+
var _loop = function _loop() {
|
|
20
|
+
var item = _step.value;
|
|
21
|
+
var isUniq = result.every(function (v) {
|
|
22
|
+
return !areItemsEqual(v, item);
|
|
23
|
+
});
|
|
24
|
+
if (isUniq) {
|
|
25
|
+
result.push(item);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
29
|
+
_loop();
|
|
30
|
+
}
|
|
31
|
+
} catch (err) {
|
|
32
|
+
_iterator.e(err);
|
|
33
|
+
} finally {
|
|
34
|
+
_iterator.f();
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export { uniqWith };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { difference } from './difference.mjs';
|
|
2
|
+
import { intersection } from './intersection.mjs';
|
|
3
|
+
import { union } from './union.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Computes the symmetric difference between two arrays. The symmetric difference is the set of elements
|
|
7
|
+
* which are in either of the arrays, but not in their intersection.
|
|
8
|
+
*
|
|
9
|
+
* @param {T[]} arr1 - The first array.
|
|
10
|
+
* @param {T[]} arr2 - The second array.
|
|
11
|
+
* @returns {T[]} An array containing the elements that are present in either `arr1` or `arr2` but not in both.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // Returns [1, 2, 5, 6]
|
|
15
|
+
* xor([1, 2, 3, 4], [3, 4, 5, 6]);
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // Returns ['a', 'c']
|
|
19
|
+
* xor(['a', 'b'], ['b', 'c']);
|
|
20
|
+
*/
|
|
21
|
+
function xor(arr1, arr2) {
|
|
22
|
+
return difference(union(arr1, arr2), intersection(arr1, arr2));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { xor };
|