es-toolkit 1.37.0 → 1.37.1-dev.1244
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 +6 -0
- package/dist/_chunk/{toSnakeCaseKeys-kqZCyq.js → isPlainObject-DINLyA.js} +0 -98
- package/dist/_chunk/{isWeakSet-C2NpfO.js → isWeakSet-403Sh5.js} +0 -92
- package/dist/_chunk/{range-HnEIT7.js → range-DSpBDL.js} +0 -21
- package/dist/_chunk/{unary-EIEhcF.js → unary-DzPndU.js} +0 -39
- package/dist/_chunk/{upperFirst-DbrFSz.js → upperFirst-C7IztG.js} +0 -17
- package/dist/_chunk/{zip-DDfXXG.js → zip-CIqPLd.js} +0 -78
- package/dist/array/index.js +79 -12
- package/dist/browser.global.js +1 -1
- package/dist/browser.global.js.map +1 -1
- package/dist/compat/array/initial.d.mts +16 -0
- package/dist/compat/array/initial.d.ts +16 -0
- package/dist/compat/array/initial.mjs +11 -0
- package/dist/compat/array/keyBy.d.mts +48 -0
- package/dist/compat/array/keyBy.d.ts +48 -0
- package/dist/compat/array/keyBy.mjs +21 -0
- package/dist/compat/array/shuffle.d.mts +22 -0
- package/dist/compat/array/shuffle.d.ts +22 -0
- package/dist/compat/array/shuffle.mjs +24 -0
- package/dist/compat/array/xorBy.d.mts +108 -0
- package/dist/compat/array/xorBy.d.ts +108 -0
- package/dist/compat/array/xorBy.mjs +23 -0
- package/dist/compat/array/xorWith.d.mts +89 -0
- package/dist/compat/array/xorWith.d.ts +89 -0
- package/dist/compat/array/xorWith.mjs +21 -0
- package/dist/compat/compat.d.mts +19 -54
- package/dist/compat/compat.d.ts +19 -54
- package/dist/compat/compat.mjs +22 -59
- package/dist/compat/function/wrap.d.mts +27 -0
- package/dist/compat/function/wrap.d.ts +27 -0
- package/dist/compat/function/wrap.mjs +11 -0
- package/dist/compat/index.d.mts +19 -54
- package/dist/compat/index.d.ts +19 -54
- package/dist/compat/index.js +172 -180
- package/dist/compat/index.mjs +22 -59
- package/dist/function/index.js +39 -3
- package/dist/index.js +56 -57
- package/dist/math/index.js +22 -3
- package/dist/object/index.js +108 -14
- package/dist/predicate/index.js +93 -11
- package/dist/string/index.js +18 -4
- package/dist/util/index.js +19 -4
- package/package.json +1 -1
- package/dist/_chunk/invariant-BfGFfr.js +0 -21
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a new array containing all elements except the last one from the input array.
|
|
3
|
+
* If the input array is empty or has only one element, the function returns an empty array.
|
|
4
|
+
*
|
|
5
|
+
* @template T The type of elements in the array.
|
|
6
|
+
* @param {ArrayLike<T> | null | undefined} arr - The input array.
|
|
7
|
+
* @returns {T[]} A new array containing all but the last element of the input array.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const arr = [1, 2, 3, 4];
|
|
11
|
+
* const result = initial(arr);
|
|
12
|
+
* // result will be [1, 2, 3]
|
|
13
|
+
*/
|
|
14
|
+
declare function initial<T>(arr: ArrayLike<T> | null | undefined): T[];
|
|
15
|
+
|
|
16
|
+
export { initial };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a new array containing all elements except the last one from the input array.
|
|
3
|
+
* If the input array is empty or has only one element, the function returns an empty array.
|
|
4
|
+
*
|
|
5
|
+
* @template T The type of elements in the array.
|
|
6
|
+
* @param {ArrayLike<T> | null | undefined} arr - The input array.
|
|
7
|
+
* @returns {T[]} A new array containing all but the last element of the input array.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const arr = [1, 2, 3, 4];
|
|
11
|
+
* const result = initial(arr);
|
|
12
|
+
* // result will be [1, 2, 3]
|
|
13
|
+
*/
|
|
14
|
+
declare function initial<T>(arr: ArrayLike<T> | null | undefined): T[];
|
|
15
|
+
|
|
16
|
+
export { initial };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { initial as initial$1 } from '../../array/initial.mjs';
|
|
2
|
+
import { isArrayLike } from '../predicate/isArrayLike.mjs';
|
|
3
|
+
|
|
4
|
+
function initial(arr) {
|
|
5
|
+
if (!isArrayLike(arr)) {
|
|
6
|
+
return [];
|
|
7
|
+
}
|
|
8
|
+
return initial$1(Array.from(arr));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { initial };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps each element of an array based on a provided key-generating function.
|
|
3
|
+
*
|
|
4
|
+
* This function takes an array and a function that generates a key from each element. It returns
|
|
5
|
+
* an object where the keys are the generated keys and the values are the corresponding elements.
|
|
6
|
+
* If there are multiple elements generating the same key, the last element among them is used
|
|
7
|
+
* as the value.
|
|
8
|
+
*
|
|
9
|
+
* @param {ArrayLike<T> | null | undefined} collection - The collection to iterate over.
|
|
10
|
+
* @param {Function | PropertyKey | Array | Object} [iteratee] - The iteratee to transform keys.
|
|
11
|
+
* - If a function is provided, it's invoked for each element in the collection.
|
|
12
|
+
* - If a property name (string) is provided, that property of each element is used as the key.
|
|
13
|
+
* - If a property-value pair (array) is provided, elements with matching property values are used.
|
|
14
|
+
* - If a partial object is provided, elements with matching properties are used.
|
|
15
|
+
* - If omitted, the identity function is used.
|
|
16
|
+
* @returns {Object} Returns the composed aggregate object.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // Using an array of objects
|
|
20
|
+
* keyBy([{ id: 'a' }, { id: 'b' }], 'id');
|
|
21
|
+
* // => { a: { id: 'a' }, b: { id: 'b' } }
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* // Using a function iteratee
|
|
25
|
+
* keyBy(['a', 'b', 'c'], val => val.toUpperCase());
|
|
26
|
+
* // => { A: 'a', B: 'b', C: 'c' }
|
|
27
|
+
*/
|
|
28
|
+
declare function keyBy<T>(collection: ArrayLike<T> | null | undefined, iteratee?: ((value: T) => unknown) | PropertyKey | [keyof T, unknown] | Partial<T> | null): Record<string, T>;
|
|
29
|
+
/**
|
|
30
|
+
* Maps each value of an object based on a provided key-generating function.
|
|
31
|
+
*
|
|
32
|
+
* This function takes an object and a function that generates a key from each value. It returns
|
|
33
|
+
* an object where the keys are the generated keys and the values are the corresponding values.
|
|
34
|
+
* If there are multiple values generating the same key, the last value among them is used
|
|
35
|
+
* as the value.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* // Using an object
|
|
39
|
+
* keyBy({ a: { id: 1 }, b: { id: 2 } }, 'id');
|
|
40
|
+
* // => { '1': { id: 1 }, '2': { id: 2 } }
|
|
41
|
+
*
|
|
42
|
+
* @param collection - The object to iterate over.
|
|
43
|
+
* @param iteratee - The function or shorthand to generate the key.
|
|
44
|
+
* @returns An object composed of keys generated from the iteratee.
|
|
45
|
+
*/
|
|
46
|
+
declare function keyBy<T extends object>(collection: T | null | undefined, iteratee?: ((value: T[keyof T]) => unknown) | PropertyKey | [keyof T[keyof T], unknown] | Partial<T[keyof T]> | null): Record<string, T[keyof T]>;
|
|
47
|
+
|
|
48
|
+
export { keyBy };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps each element of an array based on a provided key-generating function.
|
|
3
|
+
*
|
|
4
|
+
* This function takes an array and a function that generates a key from each element. It returns
|
|
5
|
+
* an object where the keys are the generated keys and the values are the corresponding elements.
|
|
6
|
+
* If there are multiple elements generating the same key, the last element among them is used
|
|
7
|
+
* as the value.
|
|
8
|
+
*
|
|
9
|
+
* @param {ArrayLike<T> | null | undefined} collection - The collection to iterate over.
|
|
10
|
+
* @param {Function | PropertyKey | Array | Object} [iteratee] - The iteratee to transform keys.
|
|
11
|
+
* - If a function is provided, it's invoked for each element in the collection.
|
|
12
|
+
* - If a property name (string) is provided, that property of each element is used as the key.
|
|
13
|
+
* - If a property-value pair (array) is provided, elements with matching property values are used.
|
|
14
|
+
* - If a partial object is provided, elements with matching properties are used.
|
|
15
|
+
* - If omitted, the identity function is used.
|
|
16
|
+
* @returns {Object} Returns the composed aggregate object.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // Using an array of objects
|
|
20
|
+
* keyBy([{ id: 'a' }, { id: 'b' }], 'id');
|
|
21
|
+
* // => { a: { id: 'a' }, b: { id: 'b' } }
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* // Using a function iteratee
|
|
25
|
+
* keyBy(['a', 'b', 'c'], val => val.toUpperCase());
|
|
26
|
+
* // => { A: 'a', B: 'b', C: 'c' }
|
|
27
|
+
*/
|
|
28
|
+
declare function keyBy<T>(collection: ArrayLike<T> | null | undefined, iteratee?: ((value: T) => unknown) | PropertyKey | [keyof T, unknown] | Partial<T> | null): Record<string, T>;
|
|
29
|
+
/**
|
|
30
|
+
* Maps each value of an object based on a provided key-generating function.
|
|
31
|
+
*
|
|
32
|
+
* This function takes an object and a function that generates a key from each value. It returns
|
|
33
|
+
* an object where the keys are the generated keys and the values are the corresponding values.
|
|
34
|
+
* If there are multiple values generating the same key, the last value among them is used
|
|
35
|
+
* as the value.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* // Using an object
|
|
39
|
+
* keyBy({ a: { id: 1 }, b: { id: 2 } }, 'id');
|
|
40
|
+
* // => { '1': { id: 1 }, '2': { id: 2 } }
|
|
41
|
+
*
|
|
42
|
+
* @param collection - The object to iterate over.
|
|
43
|
+
* @param iteratee - The function or shorthand to generate the key.
|
|
44
|
+
* @returns An object composed of keys generated from the iteratee.
|
|
45
|
+
*/
|
|
46
|
+
declare function keyBy<T extends object>(collection: T | null | undefined, iteratee?: ((value: T[keyof T]) => unknown) | PropertyKey | [keyof T[keyof T], unknown] | Partial<T[keyof T]> | null): Record<string, T[keyof T]>;
|
|
47
|
+
|
|
48
|
+
export { keyBy };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { reduce } from './reduce.mjs';
|
|
2
|
+
import { identity } from '../../function/identity.mjs';
|
|
3
|
+
import '../../function/partial.mjs';
|
|
4
|
+
import '../../function/partialRight.mjs';
|
|
5
|
+
import { isArrayLike } from '../predicate/isArrayLike.mjs';
|
|
6
|
+
import { isObjectLike } from '../predicate/isObjectLike.mjs';
|
|
7
|
+
import { iteratee } from '../util/iteratee.mjs';
|
|
8
|
+
|
|
9
|
+
function keyBy(collection, iteratee$1) {
|
|
10
|
+
if (!isArrayLike(collection) && !isObjectLike(collection)) {
|
|
11
|
+
return {};
|
|
12
|
+
}
|
|
13
|
+
const keyFn = iteratee(iteratee$1 ?? identity);
|
|
14
|
+
return reduce(collection, (result, value) => {
|
|
15
|
+
const key = keyFn(value);
|
|
16
|
+
result[key] = value;
|
|
17
|
+
return result;
|
|
18
|
+
}, {});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { keyBy };
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
* @template T - The type of elements in the `array`.
|
|
7
|
+
* @param {T[]} array - The `array` to shuffle.
|
|
8
|
+
* @returns {T[]} A new `array` with its elements shuffled in random order.
|
|
9
|
+
*/
|
|
10
|
+
declare function shuffle<T>(array: ArrayLike<T> | null | undefined): T[];
|
|
11
|
+
/**
|
|
12
|
+
* Randomizes the order of elements in an `object` using the Fisher-Yates algorithm.
|
|
13
|
+
*
|
|
14
|
+
* This function takes an `object` and returns a new `object` with its values shuffled in a random order.
|
|
15
|
+
*
|
|
16
|
+
* @template T - The type of elements in the `object`.
|
|
17
|
+
* @param {T} object - The `object` to shuffle.
|
|
18
|
+
* @returns {T[]} A new `Array` with the values of the `object` shuffled in a random order.
|
|
19
|
+
*/
|
|
20
|
+
declare function shuffle<T extends object>(object: T | null | undefined): Array<T[keyof T]>;
|
|
21
|
+
|
|
22
|
+
export { shuffle };
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
* @template T - The type of elements in the `array`.
|
|
7
|
+
* @param {T[]} array - The `array` to shuffle.
|
|
8
|
+
* @returns {T[]} A new `array` with its elements shuffled in random order.
|
|
9
|
+
*/
|
|
10
|
+
declare function shuffle<T>(array: ArrayLike<T> | null | undefined): T[];
|
|
11
|
+
/**
|
|
12
|
+
* Randomizes the order of elements in an `object` using the Fisher-Yates algorithm.
|
|
13
|
+
*
|
|
14
|
+
* This function takes an `object` and returns a new `object` with its values shuffled in a random order.
|
|
15
|
+
*
|
|
16
|
+
* @template T - The type of elements in the `object`.
|
|
17
|
+
* @param {T} object - The `object` to shuffle.
|
|
18
|
+
* @returns {T[]} A new `Array` with the values of the `object` shuffled in a random order.
|
|
19
|
+
*/
|
|
20
|
+
declare function shuffle<T extends object>(object: T | null | undefined): Array<T[keyof T]>;
|
|
21
|
+
|
|
22
|
+
export { shuffle };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { shuffle as shuffle$1 } from '../../array/shuffle.mjs';
|
|
2
|
+
import { values } from '../object/values.mjs';
|
|
3
|
+
import { isArray } from '../predicate/isArray.mjs';
|
|
4
|
+
import { isArrayLike } from '../predicate/isArrayLike.mjs';
|
|
5
|
+
import { isNil } from '../predicate/isNil.mjs';
|
|
6
|
+
import { isObjectLike } from '../predicate/isObjectLike.mjs';
|
|
7
|
+
|
|
8
|
+
function shuffle(collection) {
|
|
9
|
+
if (isNil(collection)) {
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
if (isArray(collection)) {
|
|
13
|
+
return shuffle$1(collection);
|
|
14
|
+
}
|
|
15
|
+
if (isArrayLike(collection)) {
|
|
16
|
+
return shuffle$1(Array.from(collection));
|
|
17
|
+
}
|
|
18
|
+
if (isObjectLike(collection)) {
|
|
19
|
+
return shuffle$1(values(collection));
|
|
20
|
+
}
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { shuffle };
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an array of unique values that is the symmetric difference of the given arrays after applying the iteratee function to each element.
|
|
3
|
+
*
|
|
4
|
+
* This function takes multiple arrays and an iteratee function (or property key) to
|
|
5
|
+
* compare the elements after transforming them. It returns a new array containing elements that are present
|
|
6
|
+
* in exactly one of the arrays after applying the iteratee to each element.
|
|
7
|
+
*
|
|
8
|
+
* @template T1, T2
|
|
9
|
+
* @param {ArrayLike<T1> | null | undefined} array - The first array to compare.
|
|
10
|
+
* @param {ArrayLike<T2>} values - The second array to compare.
|
|
11
|
+
* @param {(value: T1 | T2) => unknown | string} iteratee - The iteratee invoked on each element
|
|
12
|
+
* for comparison. It can also be a property key to compare based on that property.
|
|
13
|
+
* @returns {Array<T1 | T2>} A new array containing elements that are present in exactly one of the arrays
|
|
14
|
+
* after applying the iteratee.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* const array1 = [{ x: 1 }, { x: 2 }, { x: 3 }];
|
|
18
|
+
* const array2 = [{ x: 2 }, { x: 3 }, { x: 4 }];
|
|
19
|
+
* const result = xorBy(array1, array2, 'x');
|
|
20
|
+
* // result will be [{ x: 1 }, { x: 4 }] since these elements are unique to their respective arrays.
|
|
21
|
+
*
|
|
22
|
+
* const array1 = [{ x: 1 }, { x: 2 }, { x: 3 }];
|
|
23
|
+
* const array2 = [{ x: 2 }, { x: 3 }, { x: 4 }];
|
|
24
|
+
* const result = xorBy(array1, array2, value => value.x);
|
|
25
|
+
* // result will be [{ x: 1 }, { x: 4 }] since these elements are unique to their respective arrays.
|
|
26
|
+
*/
|
|
27
|
+
declare function xorBy<T1, T2>(array: ArrayLike<T1> | null | undefined, values: ArrayLike<T2>, iteratee: ((value: T1 | T2) => unknown) | string): Array<T1 | T2>;
|
|
28
|
+
/**
|
|
29
|
+
* Creates an array of unique values that is the symmetric difference of the given arrays after applying the iteratee function to each element.
|
|
30
|
+
*
|
|
31
|
+
* This function takes multiple arrays and an iteratee function (or property key) to
|
|
32
|
+
* compare the elements after transforming them. It returns a new array containing elements that are present
|
|
33
|
+
* in exactly one of the arrays after applying the iteratee to each element.
|
|
34
|
+
*
|
|
35
|
+
* @template T1, T2, T3
|
|
36
|
+
* @param {ArrayLike<T1> | null | undefined} array - The first array to compare.
|
|
37
|
+
* @param {ArrayLike<T2>} values1 - The second array to compare.
|
|
38
|
+
* @param {ArrayLike<T3>} values2 - The third array to compare.
|
|
39
|
+
* @param {(value: T1 | T2 | T3) => unknown | string} iteratee - The iteratee invoked on each element
|
|
40
|
+
* for comparison. It can also be a property key to compare based on that property.
|
|
41
|
+
* @returns {Array<T1 | T2 | T3>} A new array containing elements that are present in exactly one of the arrays
|
|
42
|
+
* after applying the iteratee.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* const array1 = [{ x: 1 }, { x: 2 }, { x: 3 }];
|
|
46
|
+
* const array2 = [{ x: 2 }, { x: 3 }];
|
|
47
|
+
* const array3 = [{ x: 3 }, { x: 4 }];
|
|
48
|
+
* const result = xorBy(array1, array2, array3, 'x');
|
|
49
|
+
* // result will be [{ x: 1 }, { x: 4 }] since these elements are unique to their respective arrays.
|
|
50
|
+
*
|
|
51
|
+
* const array1 = [{ x: 1 }, { x: 2 }, { x: 3 }];
|
|
52
|
+
* const array2 = [{ x: 2 }, { x: 3 }];
|
|
53
|
+
* const array3 = [{ x: 3 }, { x: 4 }];
|
|
54
|
+
* const result = xorBy(array1, array2, array3, value => value.x);
|
|
55
|
+
* // result will be [{ x: 1 }, { x: 4 }] since these elements are unique to their respective arrays.
|
|
56
|
+
*/
|
|
57
|
+
declare function xorBy<T1, T2, T3>(array: ArrayLike<T1> | null | undefined, values1: ArrayLike<T2>, values2: ArrayLike<T3>, iteratee: ((value: T1 | T2 | T3) => unknown) | string): Array<T1 | T2 | T3>;
|
|
58
|
+
/**
|
|
59
|
+
* Creates an array of unique values that is the symmetric difference of the given arrays after applying the iteratee function to each element.
|
|
60
|
+
*
|
|
61
|
+
* This function takes multiple arrays and an iteratee function (or property key) to
|
|
62
|
+
* compare the elements after transforming them. It returns a new array containing elements that are present
|
|
63
|
+
* in exactly one of the arrays after applying the iteratee to each element.
|
|
64
|
+
*
|
|
65
|
+
* @template T1, T2, T3, T4
|
|
66
|
+
* @param {ArrayLike<T1> | null | undefined} array - The first array to compare.
|
|
67
|
+
* @param {ArrayLike<T2>} values1 - The second array to compare.
|
|
68
|
+
* @param {ArrayLike<T3>} values2 - The third array to compare.
|
|
69
|
+
* @param {...(ArrayLike<T4> | ((value: T1 | T2 | T3 | T4) => unknown) | string)} values - Additional arrays to compare, or the iteratee function.
|
|
70
|
+
* @returns {Array<T1 | T2 | T3 | T4>} A new array containing elements that are present in exactly one of the arrays
|
|
71
|
+
* after applying the iteratee.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* const array1 = [{ x: 1 }, { x: 2 }];
|
|
75
|
+
* const array2 = [{ x: 2 }, { x: 3 }];
|
|
76
|
+
* const array3 = [{ x: 3 }, { x: 4 }];
|
|
77
|
+
* const array4 = [{ x: 4 }, { x: 5 }];
|
|
78
|
+
* const result = xorBy(array1, array2, array3, array4, 'x');
|
|
79
|
+
* // result will be [{ x: 1 }, { x: 5 }] since these elements are unique to their respective arrays.
|
|
80
|
+
*
|
|
81
|
+
* const array1 = [{ x: 1 }, { x: 2 }];
|
|
82
|
+
* const array2 = [{ x: 2 }, { x: 3 }];
|
|
83
|
+
* const array3 = [{ x: 3 }, { x: 4 }];
|
|
84
|
+
* const array4 = [{ x: 4 }, { x: 5 }];
|
|
85
|
+
* const result = xorBy(array1, array2, array3, array4, value => value.x);
|
|
86
|
+
* // result will be [{ x: 1 }, { x: 5 }] since these elements are unique to their respective arrays.
|
|
87
|
+
*/
|
|
88
|
+
declare function xorBy<T1, T2, T3, T4>(array: ArrayLike<T1> | null | undefined, values1: ArrayLike<T2>, values2: ArrayLike<T3>, ...values: Array<ArrayLike<T4> | ((value: T1 | T2 | T3 | T4) => unknown) | string>): Array<T1 | T2 | T3 | T4>;
|
|
89
|
+
/**
|
|
90
|
+
* Computes the symmetric difference between two arrays using a custom mapping function.
|
|
91
|
+
* The symmetric difference is the set of elements which are in either of the arrays,
|
|
92
|
+
* but not in their intersection, determined by the result of the mapping function.
|
|
93
|
+
*
|
|
94
|
+
* @template T - Type of elements in the input arrays.
|
|
95
|
+
* @template U - Type of the values returned by the mapping function.
|
|
96
|
+
*
|
|
97
|
+
* @param {...(ArrayLike<T> | null | undefined | PropertyKey | Partial<T> | ((value: T) => unknown))} values - The arrays to inspect, or the function to map array elements to comparison values.
|
|
98
|
+
* @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.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* // Custom mapping function for objects with an 'id' property
|
|
102
|
+
* const idMapper = obj => obj.id;
|
|
103
|
+
* xorBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], idMapper);
|
|
104
|
+
* // Returns [{ id: 1 }, { id: 3 }]
|
|
105
|
+
*/
|
|
106
|
+
declare function xorBy<T>(...values: Array<ArrayLike<T> | null | undefined | PropertyKey | Partial<T> | ((value: T) => unknown)>): T[];
|
|
107
|
+
|
|
108
|
+
export { xorBy };
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an array of unique values that is the symmetric difference of the given arrays after applying the iteratee function to each element.
|
|
3
|
+
*
|
|
4
|
+
* This function takes multiple arrays and an iteratee function (or property key) to
|
|
5
|
+
* compare the elements after transforming them. It returns a new array containing elements that are present
|
|
6
|
+
* in exactly one of the arrays after applying the iteratee to each element.
|
|
7
|
+
*
|
|
8
|
+
* @template T1, T2
|
|
9
|
+
* @param {ArrayLike<T1> | null | undefined} array - The first array to compare.
|
|
10
|
+
* @param {ArrayLike<T2>} values - The second array to compare.
|
|
11
|
+
* @param {(value: T1 | T2) => unknown | string} iteratee - The iteratee invoked on each element
|
|
12
|
+
* for comparison. It can also be a property key to compare based on that property.
|
|
13
|
+
* @returns {Array<T1 | T2>} A new array containing elements that are present in exactly one of the arrays
|
|
14
|
+
* after applying the iteratee.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* const array1 = [{ x: 1 }, { x: 2 }, { x: 3 }];
|
|
18
|
+
* const array2 = [{ x: 2 }, { x: 3 }, { x: 4 }];
|
|
19
|
+
* const result = xorBy(array1, array2, 'x');
|
|
20
|
+
* // result will be [{ x: 1 }, { x: 4 }] since these elements are unique to their respective arrays.
|
|
21
|
+
*
|
|
22
|
+
* const array1 = [{ x: 1 }, { x: 2 }, { x: 3 }];
|
|
23
|
+
* const array2 = [{ x: 2 }, { x: 3 }, { x: 4 }];
|
|
24
|
+
* const result = xorBy(array1, array2, value => value.x);
|
|
25
|
+
* // result will be [{ x: 1 }, { x: 4 }] since these elements are unique to their respective arrays.
|
|
26
|
+
*/
|
|
27
|
+
declare function xorBy<T1, T2>(array: ArrayLike<T1> | null | undefined, values: ArrayLike<T2>, iteratee: ((value: T1 | T2) => unknown) | string): Array<T1 | T2>;
|
|
28
|
+
/**
|
|
29
|
+
* Creates an array of unique values that is the symmetric difference of the given arrays after applying the iteratee function to each element.
|
|
30
|
+
*
|
|
31
|
+
* This function takes multiple arrays and an iteratee function (or property key) to
|
|
32
|
+
* compare the elements after transforming them. It returns a new array containing elements that are present
|
|
33
|
+
* in exactly one of the arrays after applying the iteratee to each element.
|
|
34
|
+
*
|
|
35
|
+
* @template T1, T2, T3
|
|
36
|
+
* @param {ArrayLike<T1> | null | undefined} array - The first array to compare.
|
|
37
|
+
* @param {ArrayLike<T2>} values1 - The second array to compare.
|
|
38
|
+
* @param {ArrayLike<T3>} values2 - The third array to compare.
|
|
39
|
+
* @param {(value: T1 | T2 | T3) => unknown | string} iteratee - The iteratee invoked on each element
|
|
40
|
+
* for comparison. It can also be a property key to compare based on that property.
|
|
41
|
+
* @returns {Array<T1 | T2 | T3>} A new array containing elements that are present in exactly one of the arrays
|
|
42
|
+
* after applying the iteratee.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* const array1 = [{ x: 1 }, { x: 2 }, { x: 3 }];
|
|
46
|
+
* const array2 = [{ x: 2 }, { x: 3 }];
|
|
47
|
+
* const array3 = [{ x: 3 }, { x: 4 }];
|
|
48
|
+
* const result = xorBy(array1, array2, array3, 'x');
|
|
49
|
+
* // result will be [{ x: 1 }, { x: 4 }] since these elements are unique to their respective arrays.
|
|
50
|
+
*
|
|
51
|
+
* const array1 = [{ x: 1 }, { x: 2 }, { x: 3 }];
|
|
52
|
+
* const array2 = [{ x: 2 }, { x: 3 }];
|
|
53
|
+
* const array3 = [{ x: 3 }, { x: 4 }];
|
|
54
|
+
* const result = xorBy(array1, array2, array3, value => value.x);
|
|
55
|
+
* // result will be [{ x: 1 }, { x: 4 }] since these elements are unique to their respective arrays.
|
|
56
|
+
*/
|
|
57
|
+
declare function xorBy<T1, T2, T3>(array: ArrayLike<T1> | null | undefined, values1: ArrayLike<T2>, values2: ArrayLike<T3>, iteratee: ((value: T1 | T2 | T3) => unknown) | string): Array<T1 | T2 | T3>;
|
|
58
|
+
/**
|
|
59
|
+
* Creates an array of unique values that is the symmetric difference of the given arrays after applying the iteratee function to each element.
|
|
60
|
+
*
|
|
61
|
+
* This function takes multiple arrays and an iteratee function (or property key) to
|
|
62
|
+
* compare the elements after transforming them. It returns a new array containing elements that are present
|
|
63
|
+
* in exactly one of the arrays after applying the iteratee to each element.
|
|
64
|
+
*
|
|
65
|
+
* @template T1, T2, T3, T4
|
|
66
|
+
* @param {ArrayLike<T1> | null | undefined} array - The first array to compare.
|
|
67
|
+
* @param {ArrayLike<T2>} values1 - The second array to compare.
|
|
68
|
+
* @param {ArrayLike<T3>} values2 - The third array to compare.
|
|
69
|
+
* @param {...(ArrayLike<T4> | ((value: T1 | T2 | T3 | T4) => unknown) | string)} values - Additional arrays to compare, or the iteratee function.
|
|
70
|
+
* @returns {Array<T1 | T2 | T3 | T4>} A new array containing elements that are present in exactly one of the arrays
|
|
71
|
+
* after applying the iteratee.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* const array1 = [{ x: 1 }, { x: 2 }];
|
|
75
|
+
* const array2 = [{ x: 2 }, { x: 3 }];
|
|
76
|
+
* const array3 = [{ x: 3 }, { x: 4 }];
|
|
77
|
+
* const array4 = [{ x: 4 }, { x: 5 }];
|
|
78
|
+
* const result = xorBy(array1, array2, array3, array4, 'x');
|
|
79
|
+
* // result will be [{ x: 1 }, { x: 5 }] since these elements are unique to their respective arrays.
|
|
80
|
+
*
|
|
81
|
+
* const array1 = [{ x: 1 }, { x: 2 }];
|
|
82
|
+
* const array2 = [{ x: 2 }, { x: 3 }];
|
|
83
|
+
* const array3 = [{ x: 3 }, { x: 4 }];
|
|
84
|
+
* const array4 = [{ x: 4 }, { x: 5 }];
|
|
85
|
+
* const result = xorBy(array1, array2, array3, array4, value => value.x);
|
|
86
|
+
* // result will be [{ x: 1 }, { x: 5 }] since these elements are unique to their respective arrays.
|
|
87
|
+
*/
|
|
88
|
+
declare function xorBy<T1, T2, T3, T4>(array: ArrayLike<T1> | null | undefined, values1: ArrayLike<T2>, values2: ArrayLike<T3>, ...values: Array<ArrayLike<T4> | ((value: T1 | T2 | T3 | T4) => unknown) | string>): Array<T1 | T2 | T3 | T4>;
|
|
89
|
+
/**
|
|
90
|
+
* Computes the symmetric difference between two arrays using a custom mapping function.
|
|
91
|
+
* The symmetric difference is the set of elements which are in either of the arrays,
|
|
92
|
+
* but not in their intersection, determined by the result of the mapping function.
|
|
93
|
+
*
|
|
94
|
+
* @template T - Type of elements in the input arrays.
|
|
95
|
+
* @template U - Type of the values returned by the mapping function.
|
|
96
|
+
*
|
|
97
|
+
* @param {...(ArrayLike<T> | null | undefined | PropertyKey | Partial<T> | ((value: T) => unknown))} values - The arrays to inspect, or the function to map array elements to comparison values.
|
|
98
|
+
* @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.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* // Custom mapping function for objects with an 'id' property
|
|
102
|
+
* const idMapper = obj => obj.id;
|
|
103
|
+
* xorBy([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], idMapper);
|
|
104
|
+
* // Returns [{ id: 1 }, { id: 3 }]
|
|
105
|
+
*/
|
|
106
|
+
declare function xorBy<T>(...values: Array<ArrayLike<T> | null | undefined | PropertyKey | Partial<T> | ((value: T) => unknown)>): T[];
|
|
107
|
+
|
|
108
|
+
export { xorBy };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { differenceBy } from './differenceBy.mjs';
|
|
2
|
+
import { intersectionBy } from './intersectionBy.mjs';
|
|
3
|
+
import { last } from './last.mjs';
|
|
4
|
+
import { unionBy } from './unionBy.mjs';
|
|
5
|
+
import { windowed } from '../../array/windowed.mjs';
|
|
6
|
+
import { identity } from '../../function/identity.mjs';
|
|
7
|
+
import { isArrayLikeObject } from '../predicate/isArrayLikeObject.mjs';
|
|
8
|
+
import { iteratee } from '../util/iteratee.mjs';
|
|
9
|
+
|
|
10
|
+
function xorBy(...values) {
|
|
11
|
+
const lastValue = last(values);
|
|
12
|
+
let mapper = identity;
|
|
13
|
+
if (!isArrayLikeObject(lastValue) && lastValue != null) {
|
|
14
|
+
mapper = iteratee(lastValue);
|
|
15
|
+
values = values.slice(0, -1);
|
|
16
|
+
}
|
|
17
|
+
const arrays = values.filter(isArrayLikeObject);
|
|
18
|
+
const union = unionBy(...arrays, mapper);
|
|
19
|
+
const intersections = windowed(arrays, 2).map(([arr1, arr2]) => intersectionBy(arr1, arr2, mapper));
|
|
20
|
+
return differenceBy(union, unionBy(...intersections, mapper), mapper);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { xorBy };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an array of unique values that is the symmetric difference of the given arrays using a custom comparator function.
|
|
3
|
+
*
|
|
4
|
+
* This function takes multiple arrays and a comparator function to determine equality between elements.
|
|
5
|
+
* It returns a new array containing elements that are present in exactly one of the arrays
|
|
6
|
+
* as determined by the comparator function.
|
|
7
|
+
*
|
|
8
|
+
* @template T1, T2
|
|
9
|
+
* @param {ArrayLike<T1> | null | undefined} array - The first array to compare.
|
|
10
|
+
* @param {ArrayLike<T2>} values - The second array to compare.
|
|
11
|
+
* @param {(a: T1 | T2, b: T1 | T2) => boolean} comparator - The comparator invoked per element to determine equality.
|
|
12
|
+
* @returns {Array<T1 | T2>} A new array containing elements that are present in exactly one of the arrays
|
|
13
|
+
* as determined by the comparator.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const array1 = [{ x: 1 }, { x: 2 }, { x: 3 }];
|
|
17
|
+
* const array2 = [{ x: 2 }, { x: 3 }, { x: 4 }];
|
|
18
|
+
* const result = xorWith(array1, array2, (a, b) => a.x === b.x);
|
|
19
|
+
* // result will be [{ x: 1 }, { x: 4 }] since these elements are unique to their respective arrays.
|
|
20
|
+
*/
|
|
21
|
+
declare function xorWith<T1, T2>(array: ArrayLike<T1> | null | undefined, values: ArrayLike<T2>, comparator: (a: T1 | T2, b: T1 | T2) => boolean): Array<T1 | T2>;
|
|
22
|
+
/**
|
|
23
|
+
* Creates an array of unique values that is the symmetric difference of the given arrays using a custom comparator function.
|
|
24
|
+
*
|
|
25
|
+
* This function takes multiple arrays and a comparator function to determine equality between elements.
|
|
26
|
+
* It returns a new array containing elements that are present in exactly one of the arrays
|
|
27
|
+
* as determined by the comparator function.
|
|
28
|
+
*
|
|
29
|
+
* @template T1, T2, T3
|
|
30
|
+
* @param {ArrayLike<T1> | null | undefined} array - The first array to compare.
|
|
31
|
+
* @param {ArrayLike<T2>} values1 - The second array to compare.
|
|
32
|
+
* @param {ArrayLike<T3>} values2 - The third array to compare.
|
|
33
|
+
* @param {(a: T1 | T2 | T3, b: T1 | T2 | T3) => boolean} comparator - The comparator invoked per element to determine equality.
|
|
34
|
+
* @returns {Array<T1 | T2 | T3>} A new array containing elements that are present in exactly one of the arrays
|
|
35
|
+
* as determined by the comparator.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* const array1 = [{ x: 1 }, { x: 2 }, { x: 3 }];
|
|
39
|
+
* const array2 = [{ x: 2 }, { x: 3 }];
|
|
40
|
+
* const array3 = [{ x: 3 }, { x: 4 }];
|
|
41
|
+
* const result = xorWith(array1, array2, array3, (a, b) => a.x === b.x);
|
|
42
|
+
* // result will be [{ x: 1 }, { x: 4 }] since these elements are unique to their respective arrays.
|
|
43
|
+
*/
|
|
44
|
+
declare function xorWith<T1, T2, T3>(array: ArrayLike<T1> | null | undefined, values1: ArrayLike<T2>, values2: ArrayLike<T3>, comparator: (a: T1 | T2 | T3, b: T1 | T2 | T3) => boolean): Array<T1 | T2 | T3>;
|
|
45
|
+
/**
|
|
46
|
+
* Creates an array of unique values that is the symmetric difference of the given arrays using a custom comparator function.
|
|
47
|
+
*
|
|
48
|
+
* This function takes multiple arrays and a comparator function to determine equality between elements.
|
|
49
|
+
* It returns a new array containing elements that are present in exactly one of the arrays
|
|
50
|
+
* as determined by the comparator function.
|
|
51
|
+
*
|
|
52
|
+
* @template T1, T2, T3, T4
|
|
53
|
+
* @param {ArrayLike<T1> | null | undefined} array - The first array to compare.
|
|
54
|
+
* @param {ArrayLike<T2>} values1 - The second array to compare.
|
|
55
|
+
* @param {ArrayLike<T3>} values2 - The third array to compare.
|
|
56
|
+
* @param {...(ArrayLike<T4> | ((a: T1 | T2 | T3 | T4, b: T1 | T2 | T3 | T4) => boolean))} values - Additional arrays to compare, or the comparator function.
|
|
57
|
+
* @returns {Array<T1 | T2 | T3 | T4>} A new array containing elements that are present in exactly one of the arrays
|
|
58
|
+
* as determined by the comparator.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* const array1 = [{ x: 1 }, { x: 2 }];
|
|
62
|
+
* const array2 = [{ x: 2 }, { x: 3 }];
|
|
63
|
+
* const array3 = [{ x: 3 }, { x: 4 }];
|
|
64
|
+
* const array4 = [{ x: 4 }, { x: 5 }];
|
|
65
|
+
* const result = xorWith(array1, array2, array3, array4, (a, b) => a.x === b.x);
|
|
66
|
+
* // result will be [{ x: 1 }, { x: 5 }] since these elements are unique to their respective arrays.
|
|
67
|
+
*/
|
|
68
|
+
declare function xorWith<T1, T2, T3, T4>(array: ArrayLike<T1> | null | undefined, values1: ArrayLike<T2>, values2: ArrayLike<T3>, ...values: Array<ArrayLike<T4> | ((a: T1 | T2 | T3 | T4, b: T1 | T2 | T3 | T4) => boolean)>): Array<T1 | T2 | T3 | T4>;
|
|
69
|
+
/**
|
|
70
|
+
* Creates an array of unique values that is the symmetric difference of the given arrays using a custom comparator function.
|
|
71
|
+
*
|
|
72
|
+
* The symmetric difference is the set of elements which are in either of the arrays,
|
|
73
|
+
* but not in their intersection, determined by the comparator function.
|
|
74
|
+
*
|
|
75
|
+
* @template T - Type of elements in the input arrays.
|
|
76
|
+
*
|
|
77
|
+
* @param {...(ArrayLike<T> | null | undefined | ((a: T, b: T) => boolean))} values - The arrays to inspect, or the comparator function.
|
|
78
|
+
* @returns {T[]} An array containing the elements that are present in exactly one of the arrays
|
|
79
|
+
* as determined by the comparator.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* // Custom comparator function for objects with an 'id' property
|
|
83
|
+
* const idComparator = (a, b) => a.id === b.id;
|
|
84
|
+
* xorWith([{ id: 1 }, { id: 2 }], [{ id: 2 }, { id: 3 }], idComparator);
|
|
85
|
+
* // Returns [{ id: 1 }, { id: 3 }]
|
|
86
|
+
*/
|
|
87
|
+
declare function xorWith<T>(...values: Array<ArrayLike<T> | null | undefined | ((a: T, b: T) => boolean)>): T[];
|
|
88
|
+
|
|
89
|
+
export { xorWith };
|