es-toolkit 0.0.1
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/.vscode/extensions.json +7 -0
- package/.vscode/settings.json +10 -0
- package/.yarn/sdks/eslint/bin/eslint.js +20 -0
- package/.yarn/sdks/eslint/lib/api.js +20 -0
- package/.yarn/sdks/eslint/lib/unsupported-api.js +20 -0
- package/.yarn/sdks/eslint/package.json +14 -0
- package/.yarn/sdks/integrations.yml +5 -0
- package/.yarn/sdks/prettier/bin/prettier.cjs +20 -0
- package/.yarn/sdks/prettier/index.cjs +20 -0
- package/.yarn/sdks/prettier/package.json +7 -0
- package/.yarn/sdks/typescript/bin/tsc +20 -0
- package/.yarn/sdks/typescript/bin/tsserver +20 -0
- package/.yarn/sdks/typescript/lib/tsc.js +20 -0
- package/.yarn/sdks/typescript/lib/tsserver.js +225 -0
- package/.yarn/sdks/typescript/lib/tsserverlibrary.js +225 -0
- package/.yarn/sdks/typescript/lib/typescript.js +20 -0
- package/.yarn/sdks/typescript/package.json +10 -0
- package/babel.config.js +6 -0
- package/coverage/.tmp/coverage-0.json +1 -0
- package/coverage/.tmp/coverage-1.json +1 -0
- package/coverage/.tmp/coverage-10.json +1 -0
- package/coverage/.tmp/coverage-11.json +1 -0
- package/coverage/.tmp/coverage-12.json +1 -0
- package/coverage/.tmp/coverage-13.json +1 -0
- package/coverage/.tmp/coverage-14.json +1 -0
- package/coverage/.tmp/coverage-15.json +1 -0
- package/coverage/.tmp/coverage-16.json +1 -0
- package/coverage/.tmp/coverage-17.json +1 -0
- package/coverage/.tmp/coverage-18.json +1 -0
- package/coverage/.tmp/coverage-19.json +1 -0
- package/coverage/.tmp/coverage-2.json +1 -0
- package/coverage/.tmp/coverage-20.json +1 -0
- package/coverage/.tmp/coverage-21.json +1 -0
- package/coverage/.tmp/coverage-22.json +1 -0
- package/coverage/.tmp/coverage-23.json +1 -0
- package/coverage/.tmp/coverage-24.json +1 -0
- package/coverage/.tmp/coverage-25.json +1 -0
- package/coverage/.tmp/coverage-26.json +1 -0
- package/coverage/.tmp/coverage-27.json +1 -0
- package/coverage/.tmp/coverage-28.json +1 -0
- package/coverage/.tmp/coverage-29.json +1 -0
- package/coverage/.tmp/coverage-3.json +1 -0
- package/coverage/.tmp/coverage-30.json +1 -0
- package/coverage/.tmp/coverage-4.json +1 -0
- package/coverage/.tmp/coverage-5.json +1 -0
- package/coverage/.tmp/coverage-6.json +1 -0
- package/coverage/.tmp/coverage-7.json +1 -0
- package/coverage/.tmp/coverage-8.json +1 -0
- package/coverage/.tmp/coverage-9.json +1 -0
- package/dist/array/chunk.d.ts +8 -0
- package/dist/array/chunk.spec.d.ts +1 -0
- package/dist/array/difference.d.ts +7 -0
- package/dist/array/difference.spec.d.ts +1 -0
- package/dist/array/differenceBy.d.ts +8 -0
- package/dist/array/differenceBy.spec.d.ts +1 -0
- package/dist/array/differenceWith.d.ts +8 -0
- package/dist/array/differenceWith.spec.d.ts +1 -0
- package/dist/array/drop.d.ts +7 -0
- package/dist/array/drop.spec.d.ts +1 -0
- package/dist/array/dropRight.d.ts +7 -0
- package/dist/array/dropRight.spec.d.ts +1 -0
- package/dist/array/dropRightWhile.d.ts +7 -0
- package/dist/array/dropRightWhile.spec.d.ts +1 -0
- package/dist/array/dropWhile.d.ts +7 -0
- package/dist/array/dropWhile.spec.d.ts +1 -0
- package/dist/array/groupBy.d.ts +1 -0
- package/dist/array/index.d.ts +30 -0
- package/dist/array/index.js +778 -0
- package/dist/array/intersection.d.ts +11 -0
- package/dist/array/intersection.spec.d.ts +1 -0
- package/dist/array/intersectionBy.d.ts +12 -0
- package/dist/array/intersectionBy.spec.d.ts +1 -0
- package/dist/array/intersectionWith.d.ts +12 -0
- package/dist/array/intersectionWith.spec.d.ts +1 -0
- package/dist/array/partition.d.ts +8 -0
- package/dist/array/partition.spec.d.ts +1 -0
- package/dist/array/sample.d.ts +7 -0
- package/dist/array/sample.spec.d.ts +1 -0
- package/dist/array/shuffle.d.ts +7 -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 +23 -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 +10 -0
- package/dist/array/union.spec.d.ts +1 -0
- package/dist/array/unionBy.d.ts +24 -0
- package/dist/array/unionBy.spec.d.ts +1 -0
- package/dist/array/unionWith.d.ts +14 -0
- package/dist/array/unionWith.spec.d.ts +1 -0
- package/dist/array/uniq.d.ts +14 -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 +19 -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 +13 -0
- package/dist/array/zip.spec.d.ts +1 -0
- package/dist/array/zipWith.d.ts +9 -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 +113 -0
- package/dist/function/once.d.ts +18 -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 +1111 -0
- package/dist/math/clamp.d.ts +11 -0
- package/dist/math/clamp.spec.d.ts +1 -0
- package/dist/math/index.d.ts +3 -0
- package/dist/math/index.js +63 -0
- package/dist/math/round.d.ts +13 -0
- package/dist/math/round.spec.d.ts +1 -0
- package/dist/math/sum.d.ts +13 -0
- package/dist/math/sum.spec.d.ts +1 -0
- package/dist/object/index.d.ts +4 -0
- package/dist/object/index.js +176 -0
- package/dist/object/omit.d.ts +7 -0
- package/dist/object/omit.spec.d.ts +1 -0
- package/dist/object/omitBy.d.ts +1 -0
- package/dist/object/pick.d.ts +7 -0
- package/dist/object/pick.spec.d.ts +1 -0
- package/dist/object/pickBy.d.ts +1 -0
- package/dist/predicate/index.d.ts +4 -0
- package/dist/predicate/index.js +50 -0
- package/dist/predicate/isNil.d.ts +6 -0
- package/dist/predicate/isNil.spec.d.ts +1 -0
- package/dist/predicate/isNotNil.d.ts +12 -0
- package/dist/predicate/isNotNil.spec.d.ts +1 -0
- package/dist/predicate/isNull.d.ts +6 -0
- package/dist/predicate/isNull.spec.d.ts +1 -0
- package/dist/predicate/isUndefined.d.ts +6 -0
- package/dist/predicate/isUndefined.spec.d.ts +1 -0
- package/dist/promise/delay.d.ts +6 -0
- package/dist/promise/delay.spec.d.ts +1 -0
- package/dist/promise/index.d.ts +1 -0
- package/dist/promise/index.js +16 -0
- package/esm/_virtual/_rollupPluginBabelHelpers.mjs +163 -0
- package/esm/array/_virtual/_rollupPluginBabelHelpers.mjs +114 -0
- package/esm/array/chunk.mjs +35 -0
- package/esm/array/difference.mjs +13 -0
- package/esm/array/differenceBy.mjs +17 -0
- package/esm/array/differenceWith.mjs +16 -0
- package/esm/array/drop.mjs +11 -0
- package/esm/array/dropRight.mjs +11 -0
- package/esm/array/dropRightWhile.mjs +15 -0
- package/esm/array/dropWhile.mjs +14 -0
- package/esm/array/groupBy.mjs +5 -0
- package/esm/array/index.mjs +30 -0
- package/esm/array/intersection.mjs +17 -0
- package/esm/array/intersectionBy.mjs +21 -0
- package/esm/array/intersectionWith.mjs +20 -0
- package/esm/array/partition.mjs +32 -0
- package/esm/array/sample.mjs +12 -0
- package/esm/array/shuffle.mjs +20 -0
- package/esm/array/take.mjs +27 -0
- package/esm/array/takeRight.mjs +30 -0
- package/esm/array/takeRightWhile.mjs +27 -0
- package/esm/array/takeWhile.mjs +41 -0
- package/esm/array/union.mjs +19 -0
- package/esm/array/unionBy.mjs +38 -0
- package/esm/array/unionWith.mjs +21 -0
- package/esm/array/uniq.mjs +18 -0
- package/esm/array/uniqBy.mjs +20 -0
- package/esm/array/uniqWith.mjs +39 -0
- package/esm/array/xor.mjs +27 -0
- package/esm/array/xorBy.mjs +30 -0
- package/esm/array/xorWith.mjs +29 -0
- package/esm/array/zip.mjs +40 -0
- package/esm/array/zipWith.mjs +31 -0
- package/esm/function/debounce.mjs +39 -0
- package/esm/function/index.mjs +3 -0
- package/esm/function/once.mjs +32 -0
- package/esm/function/throttle.mjs +38 -0
- package/esm/index.mjs +45 -0
- package/esm/math/clamp.mjs +18 -0
- package/esm/math/index.mjs +3 -0
- package/esm/math/round.mjs +19 -0
- package/esm/math/sum.mjs +22 -0
- package/esm/object/_virtual/_rollupPluginBabelHelpers.mjs +115 -0
- package/esm/object/index.mjs +4 -0
- package/esm/object/omit.mjs +26 -0
- package/esm/object/omitBy.mjs +5 -0
- package/esm/object/pick.mjs +26 -0
- package/esm/object/pickBy.mjs +5 -0
- package/esm/predicate/index.mjs +4 -0
- package/esm/predicate/isNil.mjs +10 -0
- package/esm/predicate/isNotNil.mjs +16 -0
- package/esm/predicate/isNull.mjs +10 -0
- package/esm/predicate/isUndefined.mjs +10 -0
- package/esm/promise/delay.mjs +12 -0
- package/esm/promise/index.mjs +1 -0
- package/package.json +94 -0
- package/rollup.config.js +5 -0
- package/src/array/chunk.spec.ts +27 -0
- package/src/array/chunk.ts +24 -0
- package/src/array/difference.spec.ts +10 -0
- package/src/array/difference.ts +11 -0
- package/src/array/differenceBy.spec.ts +9 -0
- package/src/array/differenceBy.ts +14 -0
- package/src/array/differenceWith.spec.ts +8 -0
- package/src/array/differenceWith.ts +14 -0
- package/src/array/drop.spec.ts +9 -0
- package/src/array/drop.ts +9 -0
- package/src/array/dropRight.spec.ts +9 -0
- package/src/array/dropRight.ts +9 -0
- package/src/array/dropRightWhile.spec.ts +16 -0
- package/src/array/dropRightWhile.ts +13 -0
- package/src/array/dropWhile.spec.ts +14 -0
- package/src/array/dropWhile.ts +10 -0
- package/src/array/groupBy.ts +3 -0
- package/src/array/index.ts +30 -0
- package/src/array/intersection.spec.ts +11 -0
- package/src/array/intersection.ts +15 -0
- package/src/array/intersectionBy.spec.ts +9 -0
- package/src/array/intersectionBy.ts +18 -0
- package/src/array/intersectionWith.spec.ts +9 -0
- package/src/array/intersectionWith.ts +18 -0
- package/src/array/partition.spec.ts +23 -0
- package/src/array/partition.ts +21 -0
- package/src/array/sample.spec.ts +11 -0
- package/src/array/sample.ts +10 -0
- package/src/array/shuffle.spec.ts +11 -0
- package/src/array/shuffle.ts +18 -0
- package/src/array/take.spec.ts +25 -0
- package/src/array/take.ts +25 -0
- package/src/array/takeRight.spec.ts +25 -0
- package/src/array/takeRight.ts +29 -0
- package/src/array/takeRightWhile.spec.ts +16 -0
- package/src/array/takeRightWhile.ts +26 -0
- package/src/array/takeWhile.spec.ts +34 -0
- package/src/array/takeWhile.ts +31 -0
- package/src/array/union.spec.ts +10 -0
- package/src/array/union.ts +14 -0
- package/src/array/unionBy.spec.ts +12 -0
- package/src/array/unionBy.ts +35 -0
- package/src/array/unionWith.spec.ts +23 -0
- package/src/array/unionWith.ts +18 -0
- package/src/array/uniq.spec.ts +8 -0
- package/src/array/uniq.ts +16 -0
- package/src/array/uniqBy.spec.ts +8 -0
- package/src/array/uniqBy.ts +17 -0
- package/src/array/uniqWith.spec.ts +16 -0
- package/src/array/uniqWith.ts +29 -0
- package/src/array/xor.spec.ts +13 -0
- package/src/array/xor.ts +25 -0
- package/src/array/xorBy.spec.ts +16 -0
- package/src/array/xorBy.ts +29 -0
- package/src/array/xorWith.spec.ts +16 -0
- package/src/array/xorWith.ts +28 -0
- package/src/array/zip.spec.ts +14 -0
- package/src/array/zip.ts +34 -0
- package/src/array/zipWith.spec.ts +13 -0
- package/src/array/zipWith.ts +23 -0
- package/src/function/debounce.spec.ts +76 -0
- package/src/function/debounce.ts +42 -0
- package/src/function/index.ts +3 -0
- package/src/function/once.spec.ts +31 -0
- package/src/function/once.ts +34 -0
- package/src/function/throttle.spec.ts +32 -0
- package/src/function/throttle.ts +39 -0
- package/src/index.ts +6 -0
- package/src/math/clamp.spec.ts +18 -0
- package/src/math/clamp.ts +17 -0
- package/src/math/index.ts +3 -0
- package/src/math/round.spec.ts +53 -0
- package/src/math/round.ts +16 -0
- package/src/math/sum.spec.ts +29 -0
- package/src/math/sum.ts +17 -0
- package/src/object/index.ts +4 -0
- package/src/object/omit.spec.ts +10 -0
- package/src/object/omit.ts +15 -0
- package/src/object/omitBy.ts +3 -0
- package/src/object/pick.spec.ts +10 -0
- package/src/object/pick.ts +15 -0
- package/src/object/pickBy.ts +3 -0
- package/src/predicate/index.ts +4 -0
- package/src/predicate/isNil.spec.ts +23 -0
- package/src/predicate/isNil.ts +8 -0
- package/src/predicate/isNotNil.spec.ts +29 -0
- package/src/predicate/isNotNil.ts +14 -0
- package/src/predicate/isNull.spec.ts +23 -0
- package/src/predicate/isNull.ts +8 -0
- package/src/predicate/isUndefined.spec.ts +23 -0
- package/src/predicate/isUndefined.ts +8 -0
- package/src/promise/delay.spec.ts +13 -0
- package/src/promise/delay.ts +10 -0
- package/src/promise/index.ts +1 -0
- package/tsconfig.json +12 -0
- package/vitest.config.mts +13 -0
|
@@ -0,0 +1,25 @@
|
|
|
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 function take<T>(arr: T[], count: number) {
|
|
24
|
+
return arr.slice(0, count);
|
|
25
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
import { takeRight } from "./takeRight"
|
|
3
|
+
|
|
4
|
+
describe('takeRight', () => {
|
|
5
|
+
it('returns the last n elements from the array', () => {
|
|
6
|
+
expect(takeRight([1, 2, 3, 4, 5], 2)).toEqual([4, 5]);
|
|
7
|
+
expect(takeRight(['a', 'b', 'c', 'd'], 2)).toEqual(['c', 'd']);
|
|
8
|
+
expect(takeRight([true, false, true], 1)).toEqual([true]);
|
|
9
|
+
expect(takeRight([1, 2, 3], 5)).toEqual([1, 2, 3]);
|
|
10
|
+
expect(takeRight([1, 2, 3], 0)).toEqual([]);
|
|
11
|
+
expect(takeRight([], 3)).toEqual([]);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('handles cases where count is greater than array length', () => {
|
|
15
|
+
expect(takeRight([1, 2, 3], 5)).toEqual([1, 2, 3]);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('handles cases where count is zero', () => {
|
|
19
|
+
expect(takeRight([1, 2, 3], 0)).toEqual([]);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('handles empty arrays', () => {
|
|
23
|
+
expect(takeRight([], 3)).toEqual([]);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
* @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 last `count` elements from `arr`.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* // Returns [4, 5]
|
|
13
|
+
* takeRight([1, 2, 3, 4, 5], 2);
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Returns ['b', 'c']
|
|
17
|
+
* takeRight(['a', 'b', 'c'], 2);
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // Returns [1, 2, 3]
|
|
21
|
+
* takeRight([1, 2, 3], 5);
|
|
22
|
+
*/
|
|
23
|
+
export function takeRight<T>(arr: T[], count: number) {
|
|
24
|
+
if (count === 0) {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return arr.slice(-count);
|
|
29
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
import { takeRightWhile } from "./takeRightWhile"
|
|
3
|
+
|
|
4
|
+
describe('takeRightWhile', () => {
|
|
5
|
+
it('returns elements from the end while the predicate returns true', () => {
|
|
6
|
+
expect(takeRightWhile([5, 4, 3, 2, 1], n => n < 4)).toEqual([3, 2, 1]);
|
|
7
|
+
expect(takeRightWhile([1, 2, 3], n => n > 3)).toEqual([]);
|
|
8
|
+
expect(takeRightWhile([1, 2, 3, 4, 5], n => n > 2)).toEqual([3, 4, 5]);
|
|
9
|
+
expect(takeRightWhile([1, 2, 3, 4, 5], n => n < 6)).toEqual([1, 2, 3, 4, 5]);
|
|
10
|
+
expect(takeRightWhile([], n => n < 6)).toEqual([]);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('handles empty arrays', () => {
|
|
14
|
+
expect(takeRightWhile([], n => n < 6)).toEqual([]);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
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 function takeRightWhile<T>(arr: T[], shouldContinueTaking: (item: T) => boolean): T[] {
|
|
19
|
+
for (let i = arr.length - 1; i >= 0; i--) {
|
|
20
|
+
if (!shouldContinueTaking(arr[i])) {
|
|
21
|
+
return arr.slice(i + 1);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return arr.slice();
|
|
26
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { takeWhile } from './takeWhile'; // adjust the import path as necessary
|
|
3
|
+
|
|
4
|
+
describe('takeWhile', () => {
|
|
5
|
+
it('should return elements while the predicate is true', () => {
|
|
6
|
+
const arr = [1, 2, 3, 4, 5];
|
|
7
|
+
const result = takeWhile(arr, x => x < 4);
|
|
8
|
+
expect(result).toEqual([1, 2, 3]);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('should return an empty array if the first element does not satisfy the predicate', () => {
|
|
12
|
+
const arr = [1, 2, 3, 4, 5];
|
|
13
|
+
const result = takeWhile(arr, x => x > 4);
|
|
14
|
+
expect(result).toEqual([]);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('should return all elements if all satisfy the predicate', () => {
|
|
18
|
+
const arr = [1, 2, 3];
|
|
19
|
+
const result = takeWhile(arr, x => x < 4);
|
|
20
|
+
expect(result).toEqual([1, 2, 3]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should return an empty array if the input array is empty', () => {
|
|
24
|
+
const arr: number[] = [];
|
|
25
|
+
const result = takeWhile(arr, x => x < 4);
|
|
26
|
+
expect(result).toEqual([]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should handle complex predicate functions', () => {
|
|
30
|
+
const arr = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }];
|
|
31
|
+
const result = takeWhile(arr, item => item.id < 3);
|
|
32
|
+
expect(result).toEqual([{ id: 1 }, { id: 2 }]);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
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 function takeWhile<T>(arr: T[], shouldContinueTaking: (element: T) => boolean): T[] {
|
|
20
|
+
const result: T[] = [];
|
|
21
|
+
|
|
22
|
+
for (const item of arr) {
|
|
23
|
+
if (!shouldContinueTaking(item)) {
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
result.push(item);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { union } from "./union";
|
|
3
|
+
|
|
4
|
+
describe("union", () => {
|
|
5
|
+
it("uniq function should return the union of two arrays", () => {
|
|
6
|
+
expect(union([2, 1], [2])).toEqual([2, 1]);
|
|
7
|
+
expect(union([2, 1], [2], [2, 3])).toEqual([2, 1, 3]);
|
|
8
|
+
expect(union([2, 1], [2], [1, [5]])).toEqual([2, 1, [5]]);
|
|
9
|
+
});
|
|
10
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { uniq } from "./uniq";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `union` function returns the union of the arrays passed as arguments.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* union([2], [1, 2]);
|
|
9
|
+
* // [2, 1]
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export function union<T>(...args: T[][]): T[] {
|
|
13
|
+
return uniq(args.flat());
|
|
14
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { unionBy } from "./unionBy";
|
|
3
|
+
|
|
4
|
+
describe("unionBy", () => {
|
|
5
|
+
it("should work with a `converter`", () => {
|
|
6
|
+
expect(unionBy([2.1], [1.2, 2.3], Math.floor)).toEqual([2.1, 1.2]);
|
|
7
|
+
expect(unionBy([{ x: 1 }], [{ x: 2 }, { x: 1 }], ({ x }) => x)).toEqual([
|
|
8
|
+
{ x: 1 },
|
|
9
|
+
{ x: 2 },
|
|
10
|
+
]);
|
|
11
|
+
});
|
|
12
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an array of unique values, in order, from all given arrays using a provided mapping function to determine equality.
|
|
3
|
+
*
|
|
4
|
+
* @template T - Type of elements in the input arrays.
|
|
5
|
+
* @template U - Type of the values returned by the mapping function.
|
|
6
|
+
*
|
|
7
|
+
* @param {T[]} arr1 - The first array.
|
|
8
|
+
* @param {T[]} arr2 - The second array.
|
|
9
|
+
* @param {function(item: T): U} mapper - The function to map array elements to comparison values.
|
|
10
|
+
* @returns {T[]} A new array containing the union of unique elements from `arr1` and `arr2`, based on the values returned by the mapping function.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // Custom mapping function for numbers (modulo comparison)
|
|
14
|
+
* const moduloMapper = (x) => x % 3;
|
|
15
|
+
* unionBy([1, 2, 3], [4, 5, 6], moduloMapper);
|
|
16
|
+
* // Returns [1, 2, 3]
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // Custom mapping function for objects with an 'id' property
|
|
20
|
+
* const idMapper = (obj) => obj.id;
|
|
21
|
+
* unionBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], idMapper);
|
|
22
|
+
* // Returns [{ id: 1 }, { id: 2 }, { id: 3 }]
|
|
23
|
+
*/
|
|
24
|
+
export function unionBy<T, U>(arr1: T[], arr2: T[], mapper: (item: T) => U): T[] {
|
|
25
|
+
const map = new Map<U, T>();
|
|
26
|
+
|
|
27
|
+
for (const item of [...arr1, ...arr2]) {
|
|
28
|
+
const key = mapper(item);
|
|
29
|
+
if (!map.has(key)) {
|
|
30
|
+
map.set(key, item);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return [...map.values()];
|
|
35
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { unionWith } from "./unionWith";
|
|
3
|
+
|
|
4
|
+
describe("unionWith", () => {
|
|
5
|
+
it("should work with a `comparator`", () => {
|
|
6
|
+
expect(
|
|
7
|
+
unionWith(
|
|
8
|
+
[
|
|
9
|
+
{ x: 1, y: 2 },
|
|
10
|
+
{ x: 2, y: 1 },
|
|
11
|
+
],
|
|
12
|
+
[
|
|
13
|
+
{ x: 1, y: 1 },
|
|
14
|
+
{ x: 1, y: 2 },
|
|
15
|
+
],
|
|
16
|
+
(a, b) => a.x === b.x
|
|
17
|
+
)
|
|
18
|
+
).toEqual([
|
|
19
|
+
{ x: 1, y: 2 },
|
|
20
|
+
{ x: 2, y: 1 },
|
|
21
|
+
]);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { uniqWith } from "./uniqWith";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `unionWith` function returns the union of multiple arrays passed as arguments. During this process, it removes any duplicated elements.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* unionWith(
|
|
9
|
+
* [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }],
|
|
10
|
+
* [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }],
|
|
11
|
+
* (x, y) => x === y
|
|
12
|
+
* );
|
|
13
|
+
* // [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export function unionWith<T>(arr1: T[], arr2: T[], areItemsEqual: (item1: T, item2: T) => boolean): T[] {
|
|
17
|
+
return uniqWith([...arr1, ...arr2], areItemsEqual);
|
|
18
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { uniq } from "./uniq";
|
|
3
|
+
|
|
4
|
+
describe("uniq", () => {
|
|
5
|
+
it("uniq function creates unique elements from the array passed as an argument.", () => {
|
|
6
|
+
expect(uniq([11, 2, 3, 44, 11, 2, 3])).toEqual([11, 2, 3, 44]);
|
|
7
|
+
});
|
|
8
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @name uniq
|
|
3
|
+
* @description
|
|
4
|
+
* `uniq` removes duplicated elements from the array given as an argument.
|
|
5
|
+
*
|
|
6
|
+
* It filters out elements with the same value, so duplicates of data types like Objects are not checked.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* uniq([1, 1, 2, 3, 5, 5, 7, 'foo', 'bar', 'bar']);
|
|
11
|
+
* // [1, 2, 3, 5, 7, 'foo', 'bar']
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export function uniq<T>(arr: T[]): T[] {
|
|
15
|
+
return Array.from(new Set(arr));
|
|
16
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { uniq } from "./uniq";
|
|
2
|
+
import { uniqWith } from "./uniqWith";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 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.
|
|
6
|
+
*
|
|
7
|
+
* It filters out elements with the same value, meaning it does not check for duplicates in data types like Objects.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* uniqBy([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], Math.floor);
|
|
12
|
+
* // [1.2, 2.1, 3.3, 5.7, 7.19]
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function uniqBy<T, U>(arr: T[], converter: (item: T) => U): T[] {
|
|
16
|
+
return uniqWith(arr, (item1, item2) => converter(item1) === converter(item2));
|
|
17
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { uniqWith } from "./uniqWith";
|
|
3
|
+
|
|
4
|
+
describe("uniqWith", () => {
|
|
5
|
+
it("should work with a `comparator`", () => {
|
|
6
|
+
expect(
|
|
7
|
+
uniqWith(
|
|
8
|
+
[
|
|
9
|
+
{ x: 1, y: 2 },
|
|
10
|
+
{ x: 1, y: 3 },
|
|
11
|
+
],
|
|
12
|
+
(a, b) => a.x === b.x
|
|
13
|
+
)
|
|
14
|
+
).toEqual([{ x: 1, y: 2 }]);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { uniq } from "./uniq";
|
|
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
|
+
export function uniqWith<T>(
|
|
15
|
+
arr: T[],
|
|
16
|
+
areItemsEqual: (item1: T, item2: T) => boolean
|
|
17
|
+
): T[] {
|
|
18
|
+
const result: T[] = [];
|
|
19
|
+
|
|
20
|
+
for (const item of arr) {
|
|
21
|
+
const isUniq = result.every((v) => !areItemsEqual(v, item));
|
|
22
|
+
|
|
23
|
+
if (isUniq) {
|
|
24
|
+
result.push(item);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
import { xor } from "./xor"
|
|
3
|
+
|
|
4
|
+
describe('xor', () => {
|
|
5
|
+
it('computes the symmetric difference between two arrays', () => {
|
|
6
|
+
expect(xor([1, 2, 3, 4], [3, 4, 5, 6])).toEqual([1, 2, 5, 6]);
|
|
7
|
+
expect(xor(['a', 'b'], ['b', 'c'])).toEqual(['a', 'c']);
|
|
8
|
+
expect(xor([1, 2, 3], [4, 5, 6])).toEqual([1, 2, 3, 4, 5, 6]);
|
|
9
|
+
expect(xor([1, 2, 3], [1, 2, 3])).toEqual([]);
|
|
10
|
+
expect(xor([], [1, 2, 3])).toEqual([1, 2, 3]);
|
|
11
|
+
expect(xor([1, 2, 3], [])).toEqual([1, 2, 3]);
|
|
12
|
+
})
|
|
13
|
+
})
|
package/src/array/xor.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { difference } from "./difference";
|
|
2
|
+
import { intersection } from "./intersection";
|
|
3
|
+
import { union } from "./union";
|
|
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
|
+
* @template T - Type of elements in the input arrays.
|
|
10
|
+
*
|
|
11
|
+
* @param {T[]} arr1 - The first array.
|
|
12
|
+
* @param {T[]} arr2 - The second array.
|
|
13
|
+
* @returns {T[]} An array containing the elements that are present in either `arr1` or `arr2` but not in both.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Returns [1, 2, 5, 6]
|
|
17
|
+
* xor([1, 2, 3, 4], [3, 4, 5, 6]);
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* // Returns ['a', 'c']
|
|
21
|
+
* xor(['a', 'b'], ['b', 'c']);
|
|
22
|
+
*/
|
|
23
|
+
export function xor<T>(arr1: T[], arr2: T[]): T[] {
|
|
24
|
+
return difference(union(arr1, arr2), intersection(arr1, arr2));
|
|
25
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
import { xorBy } from "./xorBy"
|
|
3
|
+
|
|
4
|
+
describe('xorBy', () => {
|
|
5
|
+
it('computes the symmetric difference between two arrays using a custom mapping function', () => {
|
|
6
|
+
const identity = (x: number) => x;
|
|
7
|
+
const idMapper = (obj: { id: number }) => obj.id;
|
|
8
|
+
|
|
9
|
+
expect(xorBy([1, 2, 3, 4], [3, 4, 5, 6], identity)).toEqual([1, 2, 5, 6]);
|
|
10
|
+
expect(xorBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], idMapper)).toEqual([{ id: 1 }, { id: 3 }]);
|
|
11
|
+
expect(xorBy([1, 2, 3], [4, 5, 6], identity)).toEqual([1, 2, 3, 4, 5, 6]);
|
|
12
|
+
expect(xorBy([1, 2, 3], [1, 2, 3], identity)).toEqual([]);
|
|
13
|
+
expect(xorBy([], [1, 2, 3], identity)).toEqual([1, 2, 3]);
|
|
14
|
+
expect(xorBy([1, 2, 3], [], identity)).toEqual([1, 2, 3]);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { differenceBy } from "./differenceBy";
|
|
2
|
+
import { intersectionBy } from "./intersectionBy";
|
|
3
|
+
import { unionBy } from "./unionBy";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Computes the symmetric difference between two arrays using a custom mapping function.
|
|
7
|
+
* The symmetric difference is the set of elements which are in either of the arrays,
|
|
8
|
+
* but not in their intersection, determined by the result of the mapping function.
|
|
9
|
+
*
|
|
10
|
+
* @template T - Type of elements in the input arrays.
|
|
11
|
+
* @template U - Type of the values returned by the mapping function.
|
|
12
|
+
*
|
|
13
|
+
* @param {T[]} arr1 - The first array.
|
|
14
|
+
* @param {T[]} arr2 - The second array.
|
|
15
|
+
* @param {function(item: T): U} mapper - The function to map array elements to comparison values.
|
|
16
|
+
* @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.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // Custom mapping function for objects with an 'id' property
|
|
20
|
+
* const idMapper = obj => obj.id;
|
|
21
|
+
* xorBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], idMapper);
|
|
22
|
+
* // Returns [{ id: 1 }, { id: 3 }]
|
|
23
|
+
*/
|
|
24
|
+
export function xorBy<T, U>(arr1: T[], arr2: T[], mapper: (item: T) => U): T[] {
|
|
25
|
+
const union = unionBy(arr1, arr2, mapper);
|
|
26
|
+
const intersection = intersectionBy(arr1, arr2, mapper);
|
|
27
|
+
|
|
28
|
+
return differenceBy(union, intersection, mapper);
|
|
29
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
import { xorWith } from "./xorWith"
|
|
3
|
+
|
|
4
|
+
describe('xorWith', () => {
|
|
5
|
+
it('computes the symmetric difference between two arrays using a custom equality function', () => {
|
|
6
|
+
const areNumbersEqual = (a: number, b: number) => a === b;
|
|
7
|
+
const areObjectsEqual = (a: { id: number }, b: { id: number }) => a.id === b.id;
|
|
8
|
+
|
|
9
|
+
expect(xorWith([1, 2, 3, 4], [3, 4, 5, 6], areNumbersEqual)).toEqual([1, 2, 5, 6]);
|
|
10
|
+
expect(xorWith([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], areObjectsEqual)).toEqual([{ id: 1 }, { id: 3 }]);
|
|
11
|
+
expect(xorWith([1, 2, 3], [4, 5, 6], areNumbersEqual)).toEqual([1, 2, 3, 4, 5, 6]);
|
|
12
|
+
expect(xorWith([1, 2, 3], [1, 2, 3], areNumbersEqual)).toEqual([]);
|
|
13
|
+
expect(xorWith([], [1, 2, 3], areNumbersEqual)).toEqual([1, 2, 3]);
|
|
14
|
+
expect(xorWith([1, 2, 3], [], areNumbersEqual)).toEqual([1, 2, 3]);
|
|
15
|
+
})
|
|
16
|
+
})
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { differenceWith } from "./differenceWith";
|
|
2
|
+
import { intersectionWith } from "./intersectionWith";
|
|
3
|
+
import { unionWith } from "./unionWith";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Computes the symmetric difference between two arrays using a custom equality function.
|
|
7
|
+
* The symmetric difference is the set of elements which are in either of the arrays,
|
|
8
|
+
* but not in their intersection.
|
|
9
|
+
*
|
|
10
|
+
* @template T - Type of elements in the input arrays.
|
|
11
|
+
*
|
|
12
|
+
* @param {T[]} arr1 - The first array.
|
|
13
|
+
* @param {T[]} arr2 - The second array.
|
|
14
|
+
* @param {(item1: T, item2: T) => boolean} areElementsEqual - The custom equality function to compare elements.
|
|
15
|
+
* @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.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // Custom equality function for objects with an 'id' property
|
|
19
|
+
* const areObjectsEqual = (a, b) => a.id === b.id;
|
|
20
|
+
* xorWith([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], areObjectsEqual);
|
|
21
|
+
* // Returns [{ id: 1 }, { id: 3 }]
|
|
22
|
+
*/
|
|
23
|
+
export function xorWith<T>(arr1: T[], arr2: T[], areElementsEqual: (item1: T, item2: T) => boolean): T[] {
|
|
24
|
+
const union = unionWith(arr1, arr2, areElementsEqual);
|
|
25
|
+
const intersection = intersectionWith(arr1, arr2, areElementsEqual);
|
|
26
|
+
|
|
27
|
+
return differenceWith(union, intersection, areElementsEqual);
|
|
28
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
import { zip } from "./zip"
|
|
3
|
+
|
|
4
|
+
describe('zip', () => {
|
|
5
|
+
it('zips multiple arrays to create a tuple', () => {
|
|
6
|
+
expect(zip([1, 2, 3])).toEqual([[1], [2], [3]]);
|
|
7
|
+
|
|
8
|
+
expect(zip([1, 2, 3], ['a', 'b', 'c'])).toEqual([[1, 'a'], [2, 'b'], [3, 'c']]);
|
|
9
|
+
expect(zip([1, 2, 3], ['a', 'b'])).toEqual([[1, 'a'], [2, 'b'], [3, undefined]]);
|
|
10
|
+
|
|
11
|
+
expect(zip([1, 2, 3], ['a', 'b', 'c'], [true, true, false])).toEqual([[1, 'a', true], [2, 'b', true], [3, 'c', false]]);
|
|
12
|
+
expect(zip([1], ['a'], [true], [null])).toEqual([[1, 'a', true, null]]);
|
|
13
|
+
})
|
|
14
|
+
})
|
package/src/array/zip.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { zipWith } from "./zipWith";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Merges multiple arrays element-wise. Returns an array where each element is an array containing
|
|
5
|
+
* elements from the input arrays at the corresponding index.
|
|
6
|
+
*
|
|
7
|
+
* @param {T[]} arr1 - The first array to be zipped.
|
|
8
|
+
* @param {T[]} arr2 - The second array to be zipped.
|
|
9
|
+
* @param {T[]} arr3 - The third array to be zipped.
|
|
10
|
+
* @param {T[]} arr4 - The fourth array to be zipped.
|
|
11
|
+
*/
|
|
12
|
+
export function zip<T>(arr1: T[]): [T][];
|
|
13
|
+
export function zip<T, U>(arr1: T[], arr2: U[]): [T, U][];
|
|
14
|
+
export function zip<T, U, V>(arr1: T[], arr2: U[], arr3: V[]): [T, U, V][];
|
|
15
|
+
export function zip<T, U, V, W>(arr1: T[], arr2: U[], arr3: V[], arr4: W[]): [T, U, V, W][];
|
|
16
|
+
export function zip<T>(...arrs: T[][]): T[][] {
|
|
17
|
+
const result: T[][] = [];
|
|
18
|
+
|
|
19
|
+
const maxIndex = Math.max(...arrs.map(x => x.length));
|
|
20
|
+
|
|
21
|
+
for (let i = 0; i < maxIndex; i++) {
|
|
22
|
+
const element: T[] = [];
|
|
23
|
+
|
|
24
|
+
for (const arr of arrs) {
|
|
25
|
+
element.push(arr[i]);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
result.push(element);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
import { zipWith } from "./zipWith"
|
|
3
|
+
|
|
4
|
+
describe('zipWith', () => {
|
|
5
|
+
it('zips multiple arrays with the given combine function', () => {
|
|
6
|
+
expect(zipWith([1, 2, 3], x => x)).toEqual([1, 2, 3]);
|
|
7
|
+
|
|
8
|
+
expect(zipWith([1, 2, 3], ['a', 'b', 'c'], (x, y) => `${x}${y}`)).toEqual(['1a', '2b', '3c']);
|
|
9
|
+
expect(zipWith([1, 2, 3], ['a', 'b'], (x, y) => `${x}${y}`)).toEqual(['1a', '2b', '3undefined']);
|
|
10
|
+
|
|
11
|
+
expect(zipWith([1, 2, 3], ['a', 'b', 'c'], [true, true, false], (x, y, z) => `${x}-${y}-${z}`)).toEqual([`1-a-true`, `2-b-true`, `3-c-false`]);
|
|
12
|
+
})
|
|
13
|
+
})
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Combines multiple arrays element-wise using a provided combining function.
|
|
3
|
+
* Returns an array where each element is the result of applying the combining function
|
|
4
|
+
* to the corresponding elements from the input arrays.
|
|
5
|
+
*/
|
|
6
|
+
export function zipWith<T, R>(arr1: T[], combine: (item: T) => R): R[];
|
|
7
|
+
export function zipWith<T, U, R>(arr1: T[], arr2: U[], combine: (item1: T, item2: U) => R): R[];
|
|
8
|
+
export function zipWith<T, U, V, R>(arr1: T[], arr2: U[], arr3: V[], combine: (item1: T, item2: U, item3: V) => R): R[];
|
|
9
|
+
export 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[];
|
|
10
|
+
export function zipWith<T, R>(arr1: T[], ...rest: any[]): R[] {
|
|
11
|
+
const arrs = [arr1, ...rest.slice(0, -1)];
|
|
12
|
+
const combine = rest[rest.length - 1] as (...items: T[]) => R;
|
|
13
|
+
|
|
14
|
+
const result: R[] = [];
|
|
15
|
+
const maxIndex = Math.max(...arrs.map(arr => arr.length));
|
|
16
|
+
|
|
17
|
+
for (let i = 0; i < maxIndex; i++) {
|
|
18
|
+
const elements: T[] = arrs.map(arr => arr[i]);
|
|
19
|
+
result.push(combine(...elements));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return result;
|
|
23
|
+
}
|