es-toolkit 0.0.1 → 1.0.0-dev.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/README.md +31 -0
- package/dist/array/chunk.d.ts +21 -5
- package/dist/array/difference.d.ts +20 -4
- package/dist/array/differenceBy.d.ts +23 -5
- package/dist/array/differenceWith.d.ts +19 -5
- package/dist/array/drop.d.ts +13 -4
- package/dist/array/dropRight.d.ts +13 -4
- package/dist/array/dropRightWhile.d.ts +15 -4
- package/dist/array/dropWhile.d.ts +15 -4
- package/dist/array/groupBy.d.ts +31 -1
- package/dist/array/groupBy.spec.d.ts +1 -0
- package/dist/array/index.js +437 -224
- package/dist/array/intersection.d.ts +13 -6
- package/dist/array/intersectionBy.d.ts +17 -8
- package/dist/array/intersectionWith.d.ts +18 -8
- package/dist/array/partition.d.ts +19 -5
- package/dist/array/sample.d.ts +10 -3
- package/dist/array/shuffle.d.ts +11 -4
- package/dist/array/takeRight.d.ts +0 -2
- package/dist/array/union.d.ts +13 -6
- package/dist/array/unionBy.d.ts +0 -3
- package/dist/array/unionWith.d.ts +15 -9
- package/dist/array/uniq.d.ts +9 -8
- package/dist/array/xor.d.ts +0 -2
- package/dist/array/zip.d.ts +18 -6
- package/dist/array/zipWith.d.ts +26 -3
- package/dist/function/debounce.d.ts +4 -4
- package/dist/function/index.js +4 -5
- package/dist/function/once.d.ts +0 -1
- package/dist/index.js +651 -248
- package/dist/math/clamp.d.ts +16 -8
- package/dist/math/index.js +114 -29
- package/dist/math/round.d.ts +11 -8
- package/dist/math/sum.d.ts +10 -9
- package/dist/object/index.js +122 -12
- package/dist/object/omit.d.ts +13 -4
- package/dist/object/omitBy.d.ts +19 -1
- package/dist/object/omitBy.spec.d.ts +1 -0
- package/dist/object/pick.d.ts +13 -4
- package/dist/object/pickBy.d.ts +19 -1
- package/dist/object/pickBy.spec.d.ts +1 -0
- package/dist/predicate/index.js +58 -10
- package/dist/predicate/isNil.d.ts +17 -3
- package/dist/predicate/isNotNil.d.ts +7 -3
- package/dist/predicate/isNull.d.ts +17 -2
- package/dist/predicate/isUndefined.d.ts +17 -2
- package/dist/promise/delay.d.ts +17 -4
- package/dist/promise/index.js +16 -3
- package/esm/array/_virtual/_rollupPluginBabelHelpers.mjs +1 -37
- package/esm/array/chunk.mjs +28 -25
- package/esm/array/difference.mjs +20 -4
- package/esm/array/differenceBy.mjs +23 -5
- package/esm/array/differenceWith.mjs +19 -5
- package/esm/array/drop.mjs +13 -4
- package/esm/array/dropRight.mjs +13 -4
- package/esm/array/dropRightWhile.mjs +15 -4
- package/esm/array/dropWhile.mjs +15 -4
- package/esm/array/groupBy.mjs +51 -2
- package/esm/array/intersection.mjs +13 -6
- package/esm/array/intersectionBy.mjs +17 -8
- package/esm/array/intersectionWith.mjs +18 -8
- package/esm/array/partition.mjs +19 -5
- package/esm/array/sample.mjs +10 -3
- package/esm/array/shuffle.mjs +12 -5
- package/esm/array/takeRight.mjs +0 -2
- package/esm/array/union.mjs +14 -10
- package/esm/array/unionBy.mjs +0 -3
- package/esm/array/unionWith.mjs +16 -11
- package/esm/array/uniq.mjs +28 -9
- package/esm/array/xor.mjs +0 -2
- package/esm/array/zip.mjs +18 -6
- package/esm/array/zipWith.mjs +26 -3
- package/esm/function/debounce.mjs +4 -4
- package/esm/function/once.mjs +0 -1
- package/esm/math/_virtual/_rollupPluginBabelHelpers.mjs +66 -0
- package/esm/math/clamp.mjs +15 -7
- package/esm/math/round.mjs +11 -8
- package/esm/math/sum.mjs +25 -14
- package/esm/object/_virtual/_rollupPluginBabelHelpers.mjs +37 -1
- package/esm/object/omit.mjs +13 -4
- package/esm/object/omitBy.mjs +32 -2
- package/esm/object/pick.mjs +13 -4
- package/esm/object/pickBy.mjs +32 -2
- package/esm/predicate/isNil.mjs +17 -3
- package/esm/predicate/isNotNil.mjs +7 -3
- package/esm/predicate/isNull.mjs +17 -2
- package/esm/predicate/isUndefined.mjs +17 -2
- package/esm/promise/delay.mjs +16 -3
- package/package.json +17 -3
- package/.vscode/extensions.json +0 -7
- package/.vscode/settings.json +0 -10
- package/.yarn/sdks/eslint/bin/eslint.js +0 -20
- package/.yarn/sdks/eslint/lib/api.js +0 -20
- package/.yarn/sdks/eslint/lib/unsupported-api.js +0 -20
- package/.yarn/sdks/eslint/package.json +0 -14
- package/.yarn/sdks/integrations.yml +0 -5
- package/.yarn/sdks/prettier/bin/prettier.cjs +0 -20
- package/.yarn/sdks/prettier/index.cjs +0 -20
- package/.yarn/sdks/prettier/package.json +0 -7
- package/.yarn/sdks/typescript/bin/tsc +0 -20
- package/.yarn/sdks/typescript/bin/tsserver +0 -20
- package/.yarn/sdks/typescript/lib/tsc.js +0 -20
- package/.yarn/sdks/typescript/lib/tsserver.js +0 -225
- package/.yarn/sdks/typescript/lib/tsserverlibrary.js +0 -225
- package/.yarn/sdks/typescript/lib/typescript.js +0 -20
- package/.yarn/sdks/typescript/package.json +0 -10
- package/babel.config.js +0 -6
- package/coverage/.tmp/coverage-0.json +0 -1
- package/coverage/.tmp/coverage-1.json +0 -1
- package/coverage/.tmp/coverage-10.json +0 -1
- package/coverage/.tmp/coverage-11.json +0 -1
- package/coverage/.tmp/coverage-12.json +0 -1
- package/coverage/.tmp/coverage-13.json +0 -1
- package/coverage/.tmp/coverage-14.json +0 -1
- package/coverage/.tmp/coverage-15.json +0 -1
- package/coverage/.tmp/coverage-16.json +0 -1
- package/coverage/.tmp/coverage-17.json +0 -1
- package/coverage/.tmp/coverage-18.json +0 -1
- package/coverage/.tmp/coverage-19.json +0 -1
- package/coverage/.tmp/coverage-2.json +0 -1
- package/coverage/.tmp/coverage-20.json +0 -1
- package/coverage/.tmp/coverage-21.json +0 -1
- package/coverage/.tmp/coverage-22.json +0 -1
- package/coverage/.tmp/coverage-23.json +0 -1
- package/coverage/.tmp/coverage-24.json +0 -1
- package/coverage/.tmp/coverage-25.json +0 -1
- package/coverage/.tmp/coverage-26.json +0 -1
- package/coverage/.tmp/coverage-27.json +0 -1
- package/coverage/.tmp/coverage-28.json +0 -1
- package/coverage/.tmp/coverage-29.json +0 -1
- package/coverage/.tmp/coverage-3.json +0 -1
- package/coverage/.tmp/coverage-30.json +0 -1
- package/coverage/.tmp/coverage-4.json +0 -1
- package/coverage/.tmp/coverage-5.json +0 -1
- package/coverage/.tmp/coverage-6.json +0 -1
- package/coverage/.tmp/coverage-7.json +0 -1
- package/coverage/.tmp/coverage-8.json +0 -1
- package/coverage/.tmp/coverage-9.json +0 -1
- package/rollup.config.js +0 -5
- package/src/array/chunk.spec.ts +0 -27
- package/src/array/chunk.ts +0 -24
- package/src/array/difference.spec.ts +0 -10
- package/src/array/difference.ts +0 -11
- package/src/array/differenceBy.spec.ts +0 -9
- package/src/array/differenceBy.ts +0 -14
- package/src/array/differenceWith.spec.ts +0 -8
- package/src/array/differenceWith.ts +0 -14
- package/src/array/drop.spec.ts +0 -9
- package/src/array/drop.ts +0 -9
- package/src/array/dropRight.spec.ts +0 -9
- package/src/array/dropRight.ts +0 -9
- package/src/array/dropRightWhile.spec.ts +0 -16
- package/src/array/dropRightWhile.ts +0 -13
- package/src/array/dropWhile.spec.ts +0 -14
- package/src/array/dropWhile.ts +0 -10
- package/src/array/groupBy.ts +0 -3
- package/src/array/index.ts +0 -30
- package/src/array/intersection.spec.ts +0 -11
- package/src/array/intersection.ts +0 -15
- package/src/array/intersectionBy.spec.ts +0 -9
- package/src/array/intersectionBy.ts +0 -18
- package/src/array/intersectionWith.spec.ts +0 -9
- package/src/array/intersectionWith.ts +0 -18
- package/src/array/partition.spec.ts +0 -23
- package/src/array/partition.ts +0 -21
- package/src/array/sample.spec.ts +0 -11
- package/src/array/sample.ts +0 -10
- package/src/array/shuffle.spec.ts +0 -11
- package/src/array/shuffle.ts +0 -18
- package/src/array/take.spec.ts +0 -25
- package/src/array/take.ts +0 -25
- package/src/array/takeRight.spec.ts +0 -25
- package/src/array/takeRight.ts +0 -29
- package/src/array/takeRightWhile.spec.ts +0 -16
- package/src/array/takeRightWhile.ts +0 -26
- package/src/array/takeWhile.spec.ts +0 -34
- package/src/array/takeWhile.ts +0 -31
- package/src/array/union.spec.ts +0 -10
- package/src/array/union.ts +0 -14
- package/src/array/unionBy.spec.ts +0 -12
- package/src/array/unionBy.ts +0 -35
- package/src/array/unionWith.spec.ts +0 -23
- package/src/array/unionWith.ts +0 -18
- package/src/array/uniq.spec.ts +0 -8
- package/src/array/uniq.ts +0 -16
- package/src/array/uniqBy.spec.ts +0 -8
- package/src/array/uniqBy.ts +0 -17
- package/src/array/uniqWith.spec.ts +0 -16
- package/src/array/uniqWith.ts +0 -29
- package/src/array/xor.spec.ts +0 -13
- package/src/array/xor.ts +0 -25
- package/src/array/xorBy.spec.ts +0 -16
- package/src/array/xorBy.ts +0 -29
- package/src/array/xorWith.spec.ts +0 -16
- package/src/array/xorWith.ts +0 -28
- package/src/array/zip.spec.ts +0 -14
- package/src/array/zip.ts +0 -34
- package/src/array/zipWith.spec.ts +0 -13
- package/src/array/zipWith.ts +0 -23
- package/src/function/debounce.spec.ts +0 -76
- package/src/function/debounce.ts +0 -42
- package/src/function/index.ts +0 -3
- package/src/function/once.spec.ts +0 -31
- package/src/function/once.ts +0 -34
- package/src/function/throttle.spec.ts +0 -32
- package/src/function/throttle.ts +0 -39
- package/src/index.ts +0 -6
- package/src/math/clamp.spec.ts +0 -18
- package/src/math/clamp.ts +0 -17
- package/src/math/index.ts +0 -3
- package/src/math/round.spec.ts +0 -53
- package/src/math/round.ts +0 -16
- package/src/math/sum.spec.ts +0 -29
- package/src/math/sum.ts +0 -17
- package/src/object/index.ts +0 -4
- package/src/object/omit.spec.ts +0 -10
- package/src/object/omit.ts +0 -15
- package/src/object/omitBy.ts +0 -3
- package/src/object/pick.spec.ts +0 -10
- package/src/object/pick.ts +0 -15
- package/src/object/pickBy.ts +0 -3
- package/src/predicate/index.ts +0 -4
- package/src/predicate/isNil.spec.ts +0 -23
- package/src/predicate/isNil.ts +0 -8
- package/src/predicate/isNotNil.spec.ts +0 -29
- package/src/predicate/isNotNil.ts +0 -14
- package/src/predicate/isNull.spec.ts +0 -23
- package/src/predicate/isNull.ts +0 -8
- package/src/predicate/isUndefined.spec.ts +0 -23
- package/src/predicate/isUndefined.ts +0 -8
- package/src/promise/delay.spec.ts +0 -13
- package/src/promise/delay.ts +0 -10
- package/src/promise/index.ts +0 -1
- package/tsconfig.json +0 -12
- package/vitest.config.mts +0 -13
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"/Users/raon0211/dev/js-utils/src/array/intersectionWith.ts":{"path":"/Users/raon0211/dev/js-utils/src/array/intersectionWith.ts","statementMap":{"0":{"start":{"line":2,"column":2},"end":{"line":6,"column":5}},"1":{"start":{"line":3,"column":4},"end":{"line":5,"column":7}},"2":{"start":{"line":4,"column":6},"end":{"line":4,"column":50}}},"fnMap":{"0":{"name":"intersectionWith","decl":{"start":{"line":1,"column":16},"end":{"line":1,"column":32}},"loc":{"start":{"line":1,"column":69},"end":{"line":7,"column":1}},"line":1},"1":{"name":"(anonymous_1)","decl":{"start":{"line":2,"column":25},"end":{"line":2,"column":26}},"loc":{"start":{"line":2,"column":40},"end":{"line":6,"column":3}},"line":2},"2":{"name":"(anonymous_2)","decl":{"start":{"line":3,"column":26},"end":{"line":3,"column":27}},"loc":{"start":{"line":3,"column":42},"end":{"line":5,"column":5}},"line":3}},"branchMap":{},"s":{"0":2,"1":4,"2":6},"f":{"0":2,"1":4,"2":6},"b":{},"inputSourceMap":{"version":3,"sources":["/Users/raon0211/dev/js-utils/src/array/intersectionWith.ts"],"sourcesContent":["/**\n * @name intersectionWith\n * @description\n * Returns the intersection of two arrays. The identity of two elements is compared with `areItemsEqual`.\n * @param firstArr The first array to get the intersection\n * @param secondArr The second array to get the intersection\n * @param mapper The function to compare the identity of two elements\n * @example\n * intersectionWith([1.2, 2.1], [1.4, 3.1], (x, y) => Math.floor(x) === Math.floor(y)) === [1.2]\n * intersectionWith([{ foo: 1 }, { foo: 2 }], [{ foo: 1 }, { foo: 3 }], (x, y) => x.foo === y.foo) === [{ foo: 1 }]\n */\nexport function intersectionWith<T>(firstArr: T[], secondArr: T[], areItemsEqual: (x: T, y: T) => boolean) {\n return firstArr.filter(firstItem => {\n return secondArr.some(secondItem => {\n return areItemsEqual(firstItem, secondItem);\n });\n });\n}"],"mappings":"AAWO,gBAAS,iBAAoB,UAAe,WAAgB,eAAwC;AACzG,SAAO,SAAS,OAAO,eAAa;AAClC,WAAO,UAAU,KAAK,gBAAc;AAClC,aAAO,cAAc,WAAW,UAAU;AAAA,IAC5C,CAAC;AAAA,EACH,CAAC;AACH;","names":[]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"0548ef75075d65c1d72742e7787cfccbca9a954d"}}
|
package/rollup.config.js
DELETED
package/src/array/chunk.spec.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { chunk } from './chunk';
|
|
3
|
-
|
|
4
|
-
describe('chunk', () => {
|
|
5
|
-
it('should return an empty array when the input array is empty', () => {
|
|
6
|
-
expect(chunk([], 3)).toEqual([]);
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
it('should throw if the size is not an integer of is less than 1', () => {
|
|
10
|
-
expect(() => chunk([1, 2, 3], 0)).toThrowErrorMatchingInlineSnapshot(`[Error: Size must be an integer greater than zero.]`);
|
|
11
|
-
expect(() => chunk([1, 2, 3], -1)).toThrowErrorMatchingInlineSnapshot(`[Error: Size must be an integer greater than zero.]`)
|
|
12
|
-
expect(() => chunk([1, 2, 3], 0.5)).toThrowErrorMatchingInlineSnapshot(`[Error: Size must be an integer greater than zero.]`)
|
|
13
|
-
expect(() => chunk([1, 2, 3], Math.PI)).toThrowErrorMatchingInlineSnapshot(`[Error: Size must be an integer greater than zero.]`)
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it('should evenly divide all elements into chunks of the specified size when the total length is a multiple of the size', () => {
|
|
17
|
-
expect(chunk([1, 2, 3, 4, 5, 6], 3)).toEqual([
|
|
18
|
-
[1, 2, 3],
|
|
19
|
-
[4, 5, 6],
|
|
20
|
-
]);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('should place the remaining elements in the last chunk when the total length is not a multiple of the size', () => {
|
|
24
|
-
expect(chunk([1, 2, 3, 4], 6)).toEqual([[1, 2, 3, 4]]);
|
|
25
|
-
expect(chunk([1, 2, 3, 4, 5, 6, 7], 2)).toEqual([[1, 2], [3, 4], [5, 6], [7]]);
|
|
26
|
-
});
|
|
27
|
-
});
|
package/src/array/chunk.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name chunk
|
|
3
|
-
* Separate an array into smaller arrays of length `size`.
|
|
4
|
-
* If the array cannot be separated in equal size, the final sub-array will have the remaining elements.
|
|
5
|
-
* @param arr The array to chunk
|
|
6
|
-
* @param size The size of smaller arrays
|
|
7
|
-
*/
|
|
8
|
-
export function chunk<T>(arr: T[], size: number): T[][] {
|
|
9
|
-
if (!Number.isInteger(size) || size <= 0) {
|
|
10
|
-
throw new Error(`Size must be an integer greater than zero.`)
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const result: T[][] = [];
|
|
14
|
-
|
|
15
|
-
for (const [index, item] of arr.entries()) {
|
|
16
|
-
if (index % size === 0) {
|
|
17
|
-
result.push([]);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
result[result.length - 1].push(item);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return result;
|
|
24
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { difference } from './difference';
|
|
3
|
-
|
|
4
|
-
describe('difference', () => {
|
|
5
|
-
it('should the difference of two arrays', () => {
|
|
6
|
-
expect(difference([1, 2, 3], [1])).toEqual([2, 3]);
|
|
7
|
-
expect(difference([], [1, 2, 3])).toEqual([]);
|
|
8
|
-
expect(difference([1, 2, 3, 4], [2, 4])).toEqual([1, 3]);
|
|
9
|
-
});
|
|
10
|
-
});
|
package/src/array/difference.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name difference
|
|
3
|
-
* Returns the difference of two arrays.
|
|
4
|
-
* @param firstArr The array to get the difference from
|
|
5
|
-
* @param secondArr The elements to exclude from `firstArr`
|
|
6
|
-
*/
|
|
7
|
-
export function difference<T>(firstArr: T[], secondArr: T[]): T[] {
|
|
8
|
-
return firstArr.filter(item => {
|
|
9
|
-
return !secondArr.includes(item)
|
|
10
|
-
});
|
|
11
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { differenceBy } from './differenceBy';
|
|
3
|
-
|
|
4
|
-
describe('differenceBy', () => {
|
|
5
|
-
it('should the difference of two arrays using the `mapper` function', () => {
|
|
6
|
-
expect(differenceBy([1.2, 2.3, 3.4], [1.2], Math.floor)).toEqual([2.3, 3.4]);
|
|
7
|
-
expect(differenceBy([], [1.2], Math.floor)).toEqual([]);
|
|
8
|
-
});
|
|
9
|
-
});
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name differenceBy
|
|
3
|
-
* Returns the difference of two arrays. The identity of two elements is calculated after the elements are mapped with `mapper`.
|
|
4
|
-
* @param firstArr The array to get the difference from
|
|
5
|
-
* @param secondArr The elements to exclude from `firstArr`
|
|
6
|
-
* @param mapper The function to map the elements when calculating the difference
|
|
7
|
-
*/
|
|
8
|
-
export function differenceBy<T, U>(firstArr: T[], secondArr: T[], mapper: (value: T) => U): T[] {
|
|
9
|
-
const mappedSecondArr = secondArr.map(item => mapper(item));
|
|
10
|
-
|
|
11
|
-
return firstArr.filter(item => {
|
|
12
|
-
return !mappedSecondArr.includes(mapper(item));
|
|
13
|
-
});
|
|
14
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { differenceWith } from './differenceWith';
|
|
3
|
-
|
|
4
|
-
describe('differenceWith', () => {
|
|
5
|
-
it('should the difference of two arrays using the `areItemsEqual` function', () => {
|
|
6
|
-
expect(differenceWith([1.2, 2.3, 3.4], [1.2], (x, y) => Math.floor(x) === Math.floor(y))).toEqual([2.3, 3.4]);
|
|
7
|
-
});
|
|
8
|
-
});
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name differenceWith
|
|
3
|
-
* Returns the difference of two arrays. The identity of two elements is calculated with the function `areItemsEqual`.
|
|
4
|
-
* @param firstArr The array to get the difference from
|
|
5
|
-
* @param secondArr The elements to exclude from `firstArr`
|
|
6
|
-
* @param areItemsEqual The function to determine whether two items are equal
|
|
7
|
-
*/
|
|
8
|
-
export function differenceWith<T>(firstArr: T[], secondArr: T[], areItemsEqual: (x: T, y: T) => boolean): T[] {
|
|
9
|
-
return firstArr.filter(firstItem => {
|
|
10
|
-
return secondArr.every(secondItem => {
|
|
11
|
-
return !areItemsEqual(firstItem, secondItem);
|
|
12
|
-
})
|
|
13
|
-
})
|
|
14
|
-
}
|
package/src/array/drop.spec.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { drop } from './drop';
|
|
3
|
-
|
|
4
|
-
describe('drop', () => {
|
|
5
|
-
it('should drop `itemsCount` elements from an array from the beginning', () => {
|
|
6
|
-
expect(drop([1.2, 2.3, 3.4], 1)).toEqual([2.3, 3.4]);
|
|
7
|
-
expect(drop(['a', 'b', 'c', 'd'], 2)).toEqual(['c', 'd']);
|
|
8
|
-
});
|
|
9
|
-
});
|
package/src/array/drop.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name drop
|
|
3
|
-
* Drop some elements from an array from the beginning. This function returns a new array.
|
|
4
|
-
* @param arr The element to drop items from the beginning
|
|
5
|
-
* @param itemsCount The amount of elements to drop
|
|
6
|
-
*/
|
|
7
|
-
export function drop<T>(arr: T[], itemsCount: number) {
|
|
8
|
-
return arr.slice(itemsCount);
|
|
9
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { dropRight } from './dropRight';
|
|
3
|
-
|
|
4
|
-
describe('dropRight', () => {
|
|
5
|
-
it('should drop `itemsCount` elements from an array from the end', () => {
|
|
6
|
-
expect(dropRight([1.2, 2.3, 3.4], 1)).toEqual([1.2, 2.3]);
|
|
7
|
-
expect(dropRight(['a', 'b', 'c', 'd'], 2)).toEqual(['a', 'b']);
|
|
8
|
-
});
|
|
9
|
-
});
|
package/src/array/dropRight.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name dropRight
|
|
3
|
-
* Drop some elements from an array from the end. This function returns a new array.
|
|
4
|
-
* @param arr The element to drop items from the end
|
|
5
|
-
* @param itemsCount The amount of elements to drop
|
|
6
|
-
*/
|
|
7
|
-
export function dropRight<T>(arr: T[], itemsCount: number) {
|
|
8
|
-
return arr.slice(0, -itemsCount);
|
|
9
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { dropRightWhile } from './dropRightWhile';
|
|
3
|
-
|
|
4
|
-
describe('dropRightWhile', () => {
|
|
5
|
-
it('should drop elements from an array until `canContinueDropping` returns false, from the end', () => {
|
|
6
|
-
expect(dropRightWhile([1.2, 2.3, 3.4], x => x < 2)).toEqual([1.2, 2.3, 3.4]);
|
|
7
|
-
|
|
8
|
-
const items = [{ id: 1, enabled: false }, { id: 2, enabled: true }, { id: 3, enabled: false }];
|
|
9
|
-
|
|
10
|
-
expect(dropRightWhile(items, x => !x.enabled)).toEqual([{
|
|
11
|
-
id: 1, enabled: false
|
|
12
|
-
}, {
|
|
13
|
-
id: 2, enabled: true
|
|
14
|
-
}]);
|
|
15
|
-
});
|
|
16
|
-
});
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { dropWhile } from "./dropWhile";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @name dropRightWhile
|
|
5
|
-
* Drop some elements from an array from the end, until `predicate` returns false.
|
|
6
|
-
* @param arr The element to drop items from the end
|
|
7
|
-
* @param canContinueDropping The predicate to check if the function can continue dropping elements.
|
|
8
|
-
*/
|
|
9
|
-
export function dropRightWhile<T>(arr: T[], canContinueDropping: (item: T) => boolean) {
|
|
10
|
-
const reversed = arr.slice().reverse();
|
|
11
|
-
const dropped = dropWhile(reversed, canContinueDropping);
|
|
12
|
-
return dropped.slice().reverse();
|
|
13
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { dropWhile } from './dropWhile';
|
|
3
|
-
|
|
4
|
-
describe('dropWhile', () => {
|
|
5
|
-
it('should drop elements from an array until `canContinueDropping` returns false, from the beginning', () => {
|
|
6
|
-
expect(dropWhile([1.2, 2.3, 3.4], x => x < 2)).toEqual([2.3, 3.4]);
|
|
7
|
-
|
|
8
|
-
const items = [{ id: 1, enabled: false }, { id: 2, enabled: true }, { id: 3, enabled: false }];
|
|
9
|
-
|
|
10
|
-
expect(dropWhile(items, x => !x.enabled)).toEqual([{
|
|
11
|
-
id: 2, enabled: true
|
|
12
|
-
}, { id: 3, enabled: false }]);
|
|
13
|
-
});
|
|
14
|
-
});
|
package/src/array/dropWhile.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name dropWhile
|
|
3
|
-
* Drop some elements from an array from the beginning, until `predicate` returns false.
|
|
4
|
-
* @param arr The element to drop items from the beginning
|
|
5
|
-
* @param canContinueDropping The predicate to check if the function can continue dropping elements.
|
|
6
|
-
*/
|
|
7
|
-
export function dropWhile<T>(arr: T[], canContinueDropping: (item: T) => boolean) {
|
|
8
|
-
const dropEndIndex = arr.findIndex(item => !canContinueDropping(item));
|
|
9
|
-
return arr.slice(dropEndIndex);
|
|
10
|
-
}
|
package/src/array/groupBy.ts
DELETED
package/src/array/index.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
export { chunk } from './chunk';
|
|
2
|
-
export { difference } from './difference';
|
|
3
|
-
export { differenceBy } from './differenceBy';
|
|
4
|
-
export { differenceWith } from './differenceWith';
|
|
5
|
-
export { drop } from './drop';
|
|
6
|
-
export { dropRight } from './dropRight';
|
|
7
|
-
export { dropRightWhile } from './dropRightWhile';
|
|
8
|
-
export { dropWhile } from './dropWhile';
|
|
9
|
-
export { groupBy } from './groupBy';
|
|
10
|
-
export { intersection } from './intersection';
|
|
11
|
-
export { intersectionBy } from './intersectionBy';
|
|
12
|
-
export { intersectionWith } from './intersectionWith';
|
|
13
|
-
export { partition } from './partition';
|
|
14
|
-
export { sample } from './sample';
|
|
15
|
-
export { shuffle } from './shuffle';
|
|
16
|
-
export { take } from './take';
|
|
17
|
-
export { takeRight } from './takeRight';
|
|
18
|
-
export { takeRightWhile } from './takeRightWhile';
|
|
19
|
-
export { takeWhile } from './takeWhile';
|
|
20
|
-
export { union } from './union';
|
|
21
|
-
export { unionBy } from './unionBy';
|
|
22
|
-
export { unionWith } from './unionWith';
|
|
23
|
-
export { uniq } from './uniq';
|
|
24
|
-
export { uniqBy } from './uniqBy';
|
|
25
|
-
export { uniqWith } from './uniqWith';
|
|
26
|
-
export { xor } from './xor';
|
|
27
|
-
export { xorBy } from './xorBy';
|
|
28
|
-
export { xorWith } from './xorWith';
|
|
29
|
-
export { zip } from './zip';
|
|
30
|
-
export { zipWith } from './zipWith';
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { intersection } from './intersection';
|
|
3
|
-
|
|
4
|
-
describe('intersection', () => {
|
|
5
|
-
it('should return the intersection of two arrays', () => {
|
|
6
|
-
expect(intersection([1, 2], [1, 3])).toEqual([1]);
|
|
7
|
-
expect(intersection([1, 2], [3, 1])).toEqual([1]);
|
|
8
|
-
expect(intersection([1, 2], [3, 4])).toEqual([]);
|
|
9
|
-
expect(intersection([], [1, 2])).toEqual([]);
|
|
10
|
-
});
|
|
11
|
-
})
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name intersection
|
|
3
|
-
* @description
|
|
4
|
-
* Returns the intersection of two arrays.
|
|
5
|
-
* @param firstArr The first array to get the intersection
|
|
6
|
-
* @param secondArr The second array to get the intersection
|
|
7
|
-
* @example
|
|
8
|
-
* intersection([1, 2], [1, 3]) === [1]
|
|
9
|
-
* intersection([1, 2, 3], [2, 3, 4]) === [2, 3]
|
|
10
|
-
*/
|
|
11
|
-
export function intersection<T>(firstArr: T[], secondArr: T[]): T[] {
|
|
12
|
-
return firstArr.filter(item => {
|
|
13
|
-
return secondArr.includes(item);
|
|
14
|
-
});
|
|
15
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { intersectionBy } from './intersectionBy';
|
|
3
|
-
|
|
4
|
-
describe('intersectionBy', () => {
|
|
5
|
-
it('should return the intersection of two arrays with `mapper`', () => {
|
|
6
|
-
expect(intersectionBy([1.2, 2.1], [1.4, 3.1], Math.floor)).toStrictEqual([1.2]);
|
|
7
|
-
expect(intersectionBy([{ foo: 1 }, { foo: 2 }], [{ foo: 1 }, { foo: 3 }], x => x.foo)).toStrictEqual([{ foo: 1 }]);
|
|
8
|
-
});
|
|
9
|
-
})
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name intersectionBy
|
|
3
|
-
* @description
|
|
4
|
-
* Returns the intersection of two arrays. The identity of two elements is calculated after the elements are mapped with `mapper`.
|
|
5
|
-
* @param firstArr The first array to get the intersection
|
|
6
|
-
* @param secondArr The second array to get the intersection
|
|
7
|
-
* @param mapper The function to map the elements when calculating the intersection
|
|
8
|
-
* @example
|
|
9
|
-
* intersectionBy([1.2, 2.1], [1.4, 3.1], Math.floor) === [1.2]
|
|
10
|
-
* intersectionBy([{ foo: 1 }, { foo: 2 }], [{ foo: 1 }, { foo: 3 }], x => x.foo) === [{ foo: 1 }]
|
|
11
|
-
*/
|
|
12
|
-
export function intersectionBy<T, U>(firstArr: T[], secondArr: T[], mapper: (item: T) => U): T[] {
|
|
13
|
-
const mappedSecondArr = secondArr.map(x => mapper(x));
|
|
14
|
-
|
|
15
|
-
return firstArr.filter(item => {
|
|
16
|
-
return mappedSecondArr.includes(mapper(item));
|
|
17
|
-
})
|
|
18
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { intersectionWith } from './intersectionWith';
|
|
3
|
-
|
|
4
|
-
describe('intersectionWith', () => {
|
|
5
|
-
it('should return the intersection of two arrays with `mapper`', () => {
|
|
6
|
-
expect(intersectionWith([1.2, 2.1], [1.4, 3.1], (x, y) => Math.floor(x) === Math.floor(y))).toStrictEqual([1.2]);
|
|
7
|
-
expect(intersectionWith([{ foo: 1 }, { foo: 2 }], [{ foo: 1 }, { foo: 3 }], (x, y) => x.foo === y.foo)).toStrictEqual([{ foo: 1 }]);
|
|
8
|
-
});
|
|
9
|
-
})
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name intersectionWith
|
|
3
|
-
* @description
|
|
4
|
-
* Returns the intersection of two arrays. The identity of two elements is compared with `areItemsEqual`.
|
|
5
|
-
* @param firstArr The first array to get the intersection
|
|
6
|
-
* @param secondArr The second array to get the intersection
|
|
7
|
-
* @param areItemsEqual The function to compare the identity of two elements
|
|
8
|
-
* @example
|
|
9
|
-
* intersectionWith([1.2, 2.1], [1.4, 3.1], (x, y) => Math.floor(x) === Math.floor(y)) === [1.2]
|
|
10
|
-
* intersectionWith([{ foo: 1 }, { foo: 2 }], [{ foo: 1 }, { foo: 3 }], (x, y) => x.foo === y.foo) === [{ foo: 1 }]
|
|
11
|
-
*/
|
|
12
|
-
export function intersectionWith<T>(firstArr: T[], secondArr: T[], areItemsEqual: (x: T, y: T) => boolean) {
|
|
13
|
-
return firstArr.filter(firstItem => {
|
|
14
|
-
return secondArr.some(secondItem => {
|
|
15
|
-
return areItemsEqual(firstItem, secondItem);
|
|
16
|
-
});
|
|
17
|
-
});
|
|
18
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { partition } from './partition';
|
|
3
|
-
|
|
4
|
-
describe('partition', () => {
|
|
5
|
-
it('creates two arrays: the first array includes items for which isInTruthy returns true, and the second array includes items for which isInTruthy returns false.', () => {
|
|
6
|
-
expect(partition([true, true, false], x => x)).toEqual([[true, true], [false]])
|
|
7
|
-
expect(partition([
|
|
8
|
-
{ id: 1, enabled: true },
|
|
9
|
-
{ id: 2, enabled: false },
|
|
10
|
-
{ id: 3, enabled: false },
|
|
11
|
-
{ id: 4, enabled: true },
|
|
12
|
-
], x => x.enabled)).toEqual([
|
|
13
|
-
[
|
|
14
|
-
{ id: 1, enabled: true },
|
|
15
|
-
{ id: 4, enabled: true },
|
|
16
|
-
],
|
|
17
|
-
[
|
|
18
|
-
{ id: 2, enabled: false },
|
|
19
|
-
{ id: 3, enabled: false },
|
|
20
|
-
]
|
|
21
|
-
])
|
|
22
|
-
});
|
|
23
|
-
})
|
package/src/array/partition.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name partition
|
|
3
|
-
* @description
|
|
4
|
-
* Creates two arrays: the first array includes items for which isInTruthy returns true, and the second array includes items for which isInTruthy returns false.
|
|
5
|
-
* @param arr The array to split into two arrays
|
|
6
|
-
* @param isInTruthy The predicate to tell whether an item is included in the first array
|
|
7
|
-
*/
|
|
8
|
-
export function partition<T>(arr: T[], isInTruthy: (value: T) => boolean): [truthy: T[], falsy: T[]] {
|
|
9
|
-
const truthy: T[] = [];
|
|
10
|
-
const falsy: T[] = [];
|
|
11
|
-
|
|
12
|
-
for (const item of arr) {
|
|
13
|
-
if (isInTruthy(item)) {
|
|
14
|
-
truthy.push(item)
|
|
15
|
-
} else {
|
|
16
|
-
falsy.push(item)
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return [truthy, falsy];
|
|
21
|
-
}
|
package/src/array/sample.spec.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { partition } from './partition';
|
|
3
|
-
import { sample } from './sample';
|
|
4
|
-
|
|
5
|
-
describe('sample', () => {
|
|
6
|
-
it('selects a random element from an array', () => {
|
|
7
|
-
const arr = [1, 2, 3, 4, 5];
|
|
8
|
-
|
|
9
|
-
expect(arr.includes(sample(arr))).toBe(true);
|
|
10
|
-
});
|
|
11
|
-
})
|
package/src/array/sample.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name sample
|
|
3
|
-
* @description
|
|
4
|
-
* Returns a random element from an array.
|
|
5
|
-
* @param arr The element to get a random element
|
|
6
|
-
*/
|
|
7
|
-
export function sample<T>(arr: T[]): T {
|
|
8
|
-
const randomIndex = Math.floor(Math.random() * arr.length)
|
|
9
|
-
return arr[randomIndex];
|
|
10
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { sample } from './sample';
|
|
3
|
-
import { shuffle } from './shuffle';
|
|
4
|
-
|
|
5
|
-
describe('shuffle', () => {
|
|
6
|
-
it('randomizes the order of an array', () => {
|
|
7
|
-
const arr = [1, 2, 3, 4, 5];
|
|
8
|
-
|
|
9
|
-
expect(shuffle(arr).slice().sort()).toEqual(arr.slice().sort());
|
|
10
|
-
});
|
|
11
|
-
})
|
package/src/array/shuffle.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @name shuffle
|
|
3
|
-
* @description
|
|
4
|
-
* Randomize the order of an array.
|
|
5
|
-
* @param arr The array to shuffle
|
|
6
|
-
*/
|
|
7
|
-
export function shuffle<T>(arr: T[]): T[] {
|
|
8
|
-
/**
|
|
9
|
-
* https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
|
|
10
|
-
*/
|
|
11
|
-
for (let i = arr.length - 1; i >= 1; i--) {
|
|
12
|
-
const j = Math.floor(Math.random() * i);
|
|
13
|
-
|
|
14
|
-
[arr[i], arr[j]] = [arr[j], arr[i]];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return arr;
|
|
18
|
-
}
|
package/src/array/take.spec.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest"
|
|
2
|
-
import { take } from "./take"
|
|
3
|
-
|
|
4
|
-
describe('take', () => {
|
|
5
|
-
it('returns the first n elements from the array', () => {
|
|
6
|
-
expect(take([1, 2, 3, 4, 5], 3)).toEqual([1, 2, 3]);
|
|
7
|
-
expect(take(['a', 'b', 'c', 'd'], 2)).toEqual(['a', 'b']);
|
|
8
|
-
expect(take([true, false, true], 1)).toEqual([true]);
|
|
9
|
-
expect(take([1, 2, 3], 5)).toEqual([1, 2, 3]);
|
|
10
|
-
expect(take([1, 2, 3], 0)).toEqual([]);
|
|
11
|
-
expect(take([], 3)).toEqual([]);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it('handles cases where count is greater than array length', () => {
|
|
15
|
-
expect(take([1, 2, 3], 5)).toEqual([1, 2, 3]);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('handles cases where count is zero', () => {
|
|
19
|
-
expect(take([1, 2, 3], 0)).toEqual([]);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('handles empty arrays', () => {
|
|
23
|
-
expect(take([], 3)).toEqual([]);
|
|
24
|
-
});
|
|
25
|
-
});
|
package/src/array/take.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
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
|
-
});
|
package/src/array/takeRight.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,26 +0,0 @@
|
|
|
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
|
-
}
|