es-toolkit 1.32.0-dev.1005 → 1.32.0-dev.1007
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/dist/browser.global.js +1 -1
- package/dist/browser.global.js.map +1 -1
- package/dist/compat/array/find.mjs +12 -32
- package/dist/compat/array/findLast.d.mts +130 -0
- package/dist/compat/array/findLast.d.ts +130 -0
- package/dist/compat/array/findLast.mjs +32 -0
- package/dist/compat/array/intersectionWith.d.mts +96 -0
- package/dist/compat/array/intersectionWith.d.ts +96 -0
- package/dist/compat/array/intersectionWith.mjs +43 -0
- package/dist/compat/index.d.mts +2 -1
- package/dist/compat/index.d.ts +2 -1
- package/dist/compat/index.js +85 -37
- package/dist/compat/index.mjs +2 -1
- package/package.json +1 -1
|
@@ -1,46 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { matches } from '../predicate/matches.mjs';
|
|
3
|
-
import { matchesProperty } from '../predicate/matchesProperty.mjs';
|
|
1
|
+
import { iteratee } from '../util/iteratee.mjs';
|
|
4
2
|
|
|
5
|
-
function find(source,
|
|
3
|
+
function find(source, _doesMatch, fromIndex = 0) {
|
|
6
4
|
if (!source) {
|
|
7
5
|
return undefined;
|
|
8
6
|
}
|
|
9
7
|
if (fromIndex < 0) {
|
|
10
8
|
fromIndex = Math.max(source.length + fromIndex, 0);
|
|
11
9
|
}
|
|
10
|
+
const doesMatch = iteratee(_doesMatch);
|
|
12
11
|
const values = Array.isArray(source) ? source.slice(fromIndex) : Object.values(source).slice(fromIndex);
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (doesMatch(value, key, source)) {
|
|
21
|
-
return value;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return undefined;
|
|
12
|
+
if (typeof doesMatch === 'function' && !Array.isArray(source)) {
|
|
13
|
+
const keys = Object.keys(source).slice(fromIndex);
|
|
14
|
+
for (let i = 0; i < keys.length; i++) {
|
|
15
|
+
const key = keys[i];
|
|
16
|
+
const value = source[key];
|
|
17
|
+
if (doesMatch(value, key, source)) {
|
|
18
|
+
return value;
|
|
25
19
|
}
|
|
26
|
-
return values.find(doesMatch);
|
|
27
|
-
}
|
|
28
|
-
case 'object': {
|
|
29
|
-
if (Array.isArray(doesMatch) && doesMatch.length === 2) {
|
|
30
|
-
const key = doesMatch[0];
|
|
31
|
-
const value = doesMatch[1];
|
|
32
|
-
return values.find(matchesProperty(key, value));
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
return values.find(matches(doesMatch));
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
case 'symbol':
|
|
39
|
-
case 'number':
|
|
40
|
-
case 'string': {
|
|
41
|
-
return values.find(property(doesMatch));
|
|
42
20
|
}
|
|
21
|
+
return undefined;
|
|
43
22
|
}
|
|
23
|
+
return values.find(doesMatch);
|
|
44
24
|
}
|
|
45
25
|
|
|
46
26
|
export { find };
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds the last item in an array that matches the given predicate function.
|
|
3
|
+
*
|
|
4
|
+
* @template T
|
|
5
|
+
* @param {ArrayLike<T> | null | undefined} arr - The array to search through.
|
|
6
|
+
* @param {(item: T, index: number, arr: T[]) => unknown} doesMatch - A function that takes an item, its index, and the array, and returns a truthy value if the item matches the criteria.
|
|
7
|
+
* @param {number} [fromIndex=arr.length-1] - The index to start the search from, defaults to arr.length-1.
|
|
8
|
+
* @returns {T | undefined} - The last item that matches the predicate, or `undefined` if no match is found.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* // Using a predicate function
|
|
12
|
+
* const items = [1, 2, 3, 4, 5];
|
|
13
|
+
* const result = findLast(items, (item) => item > 3);
|
|
14
|
+
* console.log(result); // 5
|
|
15
|
+
*/
|
|
16
|
+
declare function findLast<T>(arr: ArrayLike<T> | null | undefined, doesMatch: (item: T, index: number, arr: readonly T[]) => unknown, fromIndex?: number): T | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Finds the last item in an array that matches the given partial object.
|
|
19
|
+
*
|
|
20
|
+
* @template T
|
|
21
|
+
* @param {ArrayLike<T> | null | undefined} arr - The array to search through.
|
|
22
|
+
* @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
|
|
23
|
+
* @param {number} [fromIndex=arr.length-1] - The index to start the search from, defaults to arr.length-1.
|
|
24
|
+
* @returns {T | undefined} - The last item that matches the partial object, or `undefined` if no match is found.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // Using a partial object
|
|
28
|
+
* const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Bob' }];
|
|
29
|
+
* const result = findLast(items, { name: 'Bob' });
|
|
30
|
+
* console.log(result); // { id: 3, name: 'Bob' }
|
|
31
|
+
*/
|
|
32
|
+
declare function findLast<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>, fromIndex?: number): T | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Finds the last item in an array that matches a property with a specific value.
|
|
35
|
+
*
|
|
36
|
+
* @template T
|
|
37
|
+
* @param {ArrayLike<T> | null | undefined} arr - The array to search through.
|
|
38
|
+
* @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
|
|
39
|
+
* @param {number} [fromIndex=arr.length-1] - The index to start the search from, defaults to arr.length-1.
|
|
40
|
+
* @returns {T | undefined} - The last item that has the specified property value, or `undefined` if no match is found.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* // Using a property-value pair
|
|
44
|
+
* const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Alice' }];
|
|
45
|
+
* const result = findLast(items, ['name', 'Alice']);
|
|
46
|
+
* console.log(result); // { id: 3, name: 'Alice' }
|
|
47
|
+
*/
|
|
48
|
+
declare function findLast<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown], fromIndex?: number): T | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Finds the last item in an array that has a specific property, where the property name is provided as a PropertyKey.
|
|
51
|
+
*
|
|
52
|
+
* @template T
|
|
53
|
+
* @param {ArrayLike<T> | null | undefined} arr - The array to search through.
|
|
54
|
+
* @param {PropertyKey} propertyToCheck - The property name to check.
|
|
55
|
+
* @param {number} [fromIndex=arr.length-1] - The index to start the search from, defaults to arr.length-1.
|
|
56
|
+
* @returns {T | undefined} - The last item that has the specified property, or `undefined` if no match is found.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* // Using a property name
|
|
60
|
+
* const items = [{ id: 1, name: 'Alice' }, { id: 2 }, { id: 3, name: 'Bob' }];
|
|
61
|
+
* const result = findLast(items, 'name');
|
|
62
|
+
* console.log(result); // { id: 3, name: 'Bob' }
|
|
63
|
+
*/
|
|
64
|
+
declare function findLast<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: PropertyKey, fromIndex?: number): T | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Finds the last item in an object that matches the given predicate function.
|
|
67
|
+
*
|
|
68
|
+
* @template T
|
|
69
|
+
* @param {T | null | undefined} object - The object to search through.
|
|
70
|
+
* @param {(item: T[keyof T], index: number, arr: T) => unknown} doesMatch - A function that takes an item, its key, and the object, and returns a truthy value if the item matches the criteria.
|
|
71
|
+
* @param {number} [fromIndex=Object.keys(object).length-1] - The index to start the search from, defaults to Object.keys(object).length-1.
|
|
72
|
+
* @returns {T | undefined} - The last property value that matches the predicate, or `undefined` if no match is found.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* // Using a predicate function
|
|
76
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
77
|
+
* const result = findLast(obj, (item) => item > 1);
|
|
78
|
+
* console.log(result); // 3
|
|
79
|
+
*/
|
|
80
|
+
declare function findLast<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: (item: T[keyof T], index: keyof T, object: T) => unknown, fromIndex?: number): T | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* Finds the last item in an object that matches the given partial value.
|
|
83
|
+
*
|
|
84
|
+
* @template T
|
|
85
|
+
* @param {T | null | undefined} object - The object to search through.
|
|
86
|
+
* @param {Partial<T[keyof T]>} doesMatch - A partial value to match against the values of the object.
|
|
87
|
+
* @param {number} [fromIndex=Object.keys(object).length-1] - The index to start the search from, defaults to Object.keys(object).length-1.
|
|
88
|
+
* @returns {T | undefined} - The last property value that matches the partial value, or `undefined` if no match is found.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* // Using a partial value
|
|
92
|
+
* const obj = { a: { id: 1, name: 'Bob' }, b: { id: 2, name: 'Alice' }, c: { id: 3, name: 'Bob' } };
|
|
93
|
+
* const result = findLast(obj, { name: 'Bob' });
|
|
94
|
+
* console.log(result); // { id: 3, name: 'Bob' }
|
|
95
|
+
*/
|
|
96
|
+
declare function findLast<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: Partial<T[keyof T]>, fromIndex?: number): T | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* Finds the last item in an object that matches a property with a specific value.
|
|
99
|
+
*
|
|
100
|
+
* @template T
|
|
101
|
+
* @param {T | null | undefined} object - The object to search through.
|
|
102
|
+
* @param {[keyof T[keyof T], unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
|
|
103
|
+
* @param {number} [fromIndex=Object.keys(object).length-1] - The index to start the search from, defaults to Object.keys(object).length-1.
|
|
104
|
+
* @returns {T | undefined} - The last item that has the specified property value, or `undefined` if no match is found.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* // Using a property-value pair
|
|
108
|
+
* const items = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' }, c: { id: 3, name: 'Alice' } };
|
|
109
|
+
* const result = findLast(items, ['name', 'Alice']);
|
|
110
|
+
* console.log(result); // { id: 3, name: 'Alice' }
|
|
111
|
+
*/
|
|
112
|
+
declare function findLast<T extends Record<string, unknown>>(object: T | null | undefined, doesMatchProperty: [keyof T[keyof T], unknown], fromIndex?: number): T | undefined;
|
|
113
|
+
/**
|
|
114
|
+
* Finds the last item in an object that has a specific property, where the property name is provided as a PropertyKey.
|
|
115
|
+
*
|
|
116
|
+
* @template T
|
|
117
|
+
* @param {T | null | undefined} object - The object to search through.
|
|
118
|
+
* @param {PropertyKey} propertyToCheck - The property name to check.
|
|
119
|
+
* @param {number} [fromIndex=Object.keys(object).length-1] - The index to start the search from, defaults to Object.keys(object).length-1.
|
|
120
|
+
* @returns {T | undefined} - The last property value that has the specified property, or `undefined` if no match is found.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* // Using a property name
|
|
124
|
+
* const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2 }, c: { id: 3, name: 'Bob' } };
|
|
125
|
+
* const result = findLast(obj, 'name');
|
|
126
|
+
* console.log(result); // { id: 3, name: 'Bob' }
|
|
127
|
+
*/
|
|
128
|
+
declare function findLast<T extends Record<string, unknown>>(object: T | null | undefined, propertyToCheck: PropertyKey, fromIndex?: number): T | undefined;
|
|
129
|
+
|
|
130
|
+
export { findLast };
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds the last item in an array that matches the given predicate function.
|
|
3
|
+
*
|
|
4
|
+
* @template T
|
|
5
|
+
* @param {ArrayLike<T> | null | undefined} arr - The array to search through.
|
|
6
|
+
* @param {(item: T, index: number, arr: T[]) => unknown} doesMatch - A function that takes an item, its index, and the array, and returns a truthy value if the item matches the criteria.
|
|
7
|
+
* @param {number} [fromIndex=arr.length-1] - The index to start the search from, defaults to arr.length-1.
|
|
8
|
+
* @returns {T | undefined} - The last item that matches the predicate, or `undefined` if no match is found.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* // Using a predicate function
|
|
12
|
+
* const items = [1, 2, 3, 4, 5];
|
|
13
|
+
* const result = findLast(items, (item) => item > 3);
|
|
14
|
+
* console.log(result); // 5
|
|
15
|
+
*/
|
|
16
|
+
declare function findLast<T>(arr: ArrayLike<T> | null | undefined, doesMatch: (item: T, index: number, arr: readonly T[]) => unknown, fromIndex?: number): T | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Finds the last item in an array that matches the given partial object.
|
|
19
|
+
*
|
|
20
|
+
* @template T
|
|
21
|
+
* @param {ArrayLike<T> | null | undefined} arr - The array to search through.
|
|
22
|
+
* @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
|
|
23
|
+
* @param {number} [fromIndex=arr.length-1] - The index to start the search from, defaults to arr.length-1.
|
|
24
|
+
* @returns {T | undefined} - The last item that matches the partial object, or `undefined` if no match is found.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* // Using a partial object
|
|
28
|
+
* const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Bob' }];
|
|
29
|
+
* const result = findLast(items, { name: 'Bob' });
|
|
30
|
+
* console.log(result); // { id: 3, name: 'Bob' }
|
|
31
|
+
*/
|
|
32
|
+
declare function findLast<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>, fromIndex?: number): T | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Finds the last item in an array that matches a property with a specific value.
|
|
35
|
+
*
|
|
36
|
+
* @template T
|
|
37
|
+
* @param {ArrayLike<T> | null | undefined} arr - The array to search through.
|
|
38
|
+
* @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
|
|
39
|
+
* @param {number} [fromIndex=arr.length-1] - The index to start the search from, defaults to arr.length-1.
|
|
40
|
+
* @returns {T | undefined} - The last item that has the specified property value, or `undefined` if no match is found.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* // Using a property-value pair
|
|
44
|
+
* const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Alice' }];
|
|
45
|
+
* const result = findLast(items, ['name', 'Alice']);
|
|
46
|
+
* console.log(result); // { id: 3, name: 'Alice' }
|
|
47
|
+
*/
|
|
48
|
+
declare function findLast<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown], fromIndex?: number): T | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Finds the last item in an array that has a specific property, where the property name is provided as a PropertyKey.
|
|
51
|
+
*
|
|
52
|
+
* @template T
|
|
53
|
+
* @param {ArrayLike<T> | null | undefined} arr - The array to search through.
|
|
54
|
+
* @param {PropertyKey} propertyToCheck - The property name to check.
|
|
55
|
+
* @param {number} [fromIndex=arr.length-1] - The index to start the search from, defaults to arr.length-1.
|
|
56
|
+
* @returns {T | undefined} - The last item that has the specified property, or `undefined` if no match is found.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* // Using a property name
|
|
60
|
+
* const items = [{ id: 1, name: 'Alice' }, { id: 2 }, { id: 3, name: 'Bob' }];
|
|
61
|
+
* const result = findLast(items, 'name');
|
|
62
|
+
* console.log(result); // { id: 3, name: 'Bob' }
|
|
63
|
+
*/
|
|
64
|
+
declare function findLast<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: PropertyKey, fromIndex?: number): T | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Finds the last item in an object that matches the given predicate function.
|
|
67
|
+
*
|
|
68
|
+
* @template T
|
|
69
|
+
* @param {T | null | undefined} object - The object to search through.
|
|
70
|
+
* @param {(item: T[keyof T], index: number, arr: T) => unknown} doesMatch - A function that takes an item, its key, and the object, and returns a truthy value if the item matches the criteria.
|
|
71
|
+
* @param {number} [fromIndex=Object.keys(object).length-1] - The index to start the search from, defaults to Object.keys(object).length-1.
|
|
72
|
+
* @returns {T | undefined} - The last property value that matches the predicate, or `undefined` if no match is found.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* // Using a predicate function
|
|
76
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
77
|
+
* const result = findLast(obj, (item) => item > 1);
|
|
78
|
+
* console.log(result); // 3
|
|
79
|
+
*/
|
|
80
|
+
declare function findLast<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: (item: T[keyof T], index: keyof T, object: T) => unknown, fromIndex?: number): T | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* Finds the last item in an object that matches the given partial value.
|
|
83
|
+
*
|
|
84
|
+
* @template T
|
|
85
|
+
* @param {T | null | undefined} object - The object to search through.
|
|
86
|
+
* @param {Partial<T[keyof T]>} doesMatch - A partial value to match against the values of the object.
|
|
87
|
+
* @param {number} [fromIndex=Object.keys(object).length-1] - The index to start the search from, defaults to Object.keys(object).length-1.
|
|
88
|
+
* @returns {T | undefined} - The last property value that matches the partial value, or `undefined` if no match is found.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* // Using a partial value
|
|
92
|
+
* const obj = { a: { id: 1, name: 'Bob' }, b: { id: 2, name: 'Alice' }, c: { id: 3, name: 'Bob' } };
|
|
93
|
+
* const result = findLast(obj, { name: 'Bob' });
|
|
94
|
+
* console.log(result); // { id: 3, name: 'Bob' }
|
|
95
|
+
*/
|
|
96
|
+
declare function findLast<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: Partial<T[keyof T]>, fromIndex?: number): T | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* Finds the last item in an object that matches a property with a specific value.
|
|
99
|
+
*
|
|
100
|
+
* @template T
|
|
101
|
+
* @param {T | null | undefined} object - The object to search through.
|
|
102
|
+
* @param {[keyof T[keyof T], unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
|
|
103
|
+
* @param {number} [fromIndex=Object.keys(object).length-1] - The index to start the search from, defaults to Object.keys(object).length-1.
|
|
104
|
+
* @returns {T | undefined} - The last item that has the specified property value, or `undefined` if no match is found.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* // Using a property-value pair
|
|
108
|
+
* const items = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' }, c: { id: 3, name: 'Alice' } };
|
|
109
|
+
* const result = findLast(items, ['name', 'Alice']);
|
|
110
|
+
* console.log(result); // { id: 3, name: 'Alice' }
|
|
111
|
+
*/
|
|
112
|
+
declare function findLast<T extends Record<string, unknown>>(object: T | null | undefined, doesMatchProperty: [keyof T[keyof T], unknown], fromIndex?: number): T | undefined;
|
|
113
|
+
/**
|
|
114
|
+
* Finds the last item in an object that has a specific property, where the property name is provided as a PropertyKey.
|
|
115
|
+
*
|
|
116
|
+
* @template T
|
|
117
|
+
* @param {T | null | undefined} object - The object to search through.
|
|
118
|
+
* @param {PropertyKey} propertyToCheck - The property name to check.
|
|
119
|
+
* @param {number} [fromIndex=Object.keys(object).length-1] - The index to start the search from, defaults to Object.keys(object).length-1.
|
|
120
|
+
* @returns {T | undefined} - The last property value that has the specified property, or `undefined` if no match is found.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* // Using a property name
|
|
124
|
+
* const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2 }, c: { id: 3, name: 'Bob' } };
|
|
125
|
+
* const result = findLast(obj, 'name');
|
|
126
|
+
* console.log(result); // { id: 3, name: 'Bob' }
|
|
127
|
+
*/
|
|
128
|
+
declare function findLast<T extends Record<string, unknown>>(object: T | null | undefined, propertyToCheck: PropertyKey, fromIndex?: number): T | undefined;
|
|
129
|
+
|
|
130
|
+
export { findLast };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { iteratee } from '../util/iteratee.mjs';
|
|
2
|
+
import { toInteger } from '../util/toInteger.mjs';
|
|
3
|
+
|
|
4
|
+
function findLast(source, _doesMatch, fromIndex) {
|
|
5
|
+
if (!source) {
|
|
6
|
+
return undefined;
|
|
7
|
+
}
|
|
8
|
+
const length = Array.isArray(source) ? source.length : Object.keys(source).length;
|
|
9
|
+
fromIndex = toInteger(fromIndex ?? length - 1);
|
|
10
|
+
if (fromIndex < 0) {
|
|
11
|
+
fromIndex = Math.max(length + fromIndex, 0);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
fromIndex = Math.min(fromIndex, length - 1);
|
|
15
|
+
}
|
|
16
|
+
const doesMatch = iteratee(_doesMatch);
|
|
17
|
+
const values = Array.isArray(source) ? source.slice(0, fromIndex + 1) : Object.values(source).slice(0, fromIndex + 1);
|
|
18
|
+
if (typeof doesMatch === 'function' && !Array.isArray(source)) {
|
|
19
|
+
const keys = Object.keys(source).slice(0, fromIndex + 1);
|
|
20
|
+
for (let i = fromIndex; i >= 0; i--) {
|
|
21
|
+
const key = keys[i];
|
|
22
|
+
const value = source[key];
|
|
23
|
+
if (doesMatch(value, key, source)) {
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
return values.findLast(doesMatch);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { findLast };
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the intersection of two arrays based on a custom equality function.
|
|
3
|
+
*
|
|
4
|
+
* This function takes two arrays and a custom equality function. It returns a new array containing
|
|
5
|
+
* the elements from the first array that have matching elements in the second array, as determined
|
|
6
|
+
* by the custom equality function. It effectively filters out any elements from the first array that
|
|
7
|
+
* do not have corresponding matches in the second array according to the equality function.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type of elements in the first array.
|
|
10
|
+
* @template U - The type of elements in the second array.
|
|
11
|
+
* @param {T[]} firstArr - The first array to compare.
|
|
12
|
+
* @param {U[]} secondArr - The second array to compare.
|
|
13
|
+
* @param {(x: T, y: U) => boolean} areItemsEqual - A custom function to determine if two elements are equal.
|
|
14
|
+
* This function takes two arguments, one from each array, and returns `true` if the elements are considered equal, and `false` otherwise.
|
|
15
|
+
* @returns {T[]} A new array containing the elements from the first array that have corresponding matches in the second array according to the custom equality function.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }];
|
|
19
|
+
* const array2 = [{ id: 2 }, { id: 4 }];
|
|
20
|
+
* const areItemsEqual = (a, b) => a.id === b.id;
|
|
21
|
+
* const result = intersectionWith(array1, array2, areItemsEqual);
|
|
22
|
+
* // result will be [{ id: 2 }] since this element has a matching id in both arrays.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const array1 = [
|
|
26
|
+
* { id: 1, name: 'jane' },
|
|
27
|
+
* { id: 2, name: 'amy' },
|
|
28
|
+
* { id: 3, name: 'michael' },
|
|
29
|
+
* ];
|
|
30
|
+
* const array2 = [2, 4];
|
|
31
|
+
* const areItemsEqual = (a, b) => a.id === b;
|
|
32
|
+
* const result = intersectionWith(array1, array2, areItemsEqual);
|
|
33
|
+
* // result will be [{ id: 2, name: 'amy' }] since this element has a matching id that is equal to seconds array's element.
|
|
34
|
+
*/
|
|
35
|
+
declare function intersectionWith<T, U>(firstArr: ArrayLike<T> | null | undefined, secondArr: ArrayLike<U> | null | undefined, areItemsEqual: (x: T, y: U) => boolean): T[];
|
|
36
|
+
/**
|
|
37
|
+
* Returns the intersection of three arrays based on a custom equality function.
|
|
38
|
+
*
|
|
39
|
+
* @template T - The type of elements in the first array
|
|
40
|
+
* @template U - The type of elements in the second array
|
|
41
|
+
* @template V - The type of elements in the third array
|
|
42
|
+
* @param {ArrayLike<T> | null | undefined} firstArr - The first array to compare
|
|
43
|
+
* @param {ArrayLike<U> | null | undefined} secondArr - The second array to compare
|
|
44
|
+
* @param {ArrayLike<V> | null | undefined} thirdArr - The third array to compare
|
|
45
|
+
* @param {(x: T, y: U | V) => boolean} areItemsEqual - Custom equality function
|
|
46
|
+
* @returns {T[]} Elements from first array that match in all arrays
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* const arr1 = [{id: 1}, {id: 2}];
|
|
50
|
+
* const arr2 = [{id: 2}, {id: 3}];
|
|
51
|
+
* const arr3 = [{id: 2}, {id: 4}];
|
|
52
|
+
* const result = intersectionWith(arr1, arr2, arr3, (a, b) => a.id === b.id);
|
|
53
|
+
* // result is [{id: 2}]
|
|
54
|
+
*/
|
|
55
|
+
declare function intersectionWith<T, U, V>(firstArr: ArrayLike<T> | null | undefined, secondArr: ArrayLike<U> | null | undefined, thirdArr: ArrayLike<V> | null | undefined, areItemsEqual: (x: T, y: U | V) => boolean): T[];
|
|
56
|
+
/**
|
|
57
|
+
* Returns the intersection of four arrays based on a custom equality function.
|
|
58
|
+
*
|
|
59
|
+
* @template T - The type of elements in the first array
|
|
60
|
+
* @template U - The type of elements in the second array
|
|
61
|
+
* @template V - The type of elements in the third array
|
|
62
|
+
* @template W - The type of elements in the fourth array
|
|
63
|
+
* @param {ArrayLike<T> | null | undefined} firstArr - The first array to compare
|
|
64
|
+
* @param {ArrayLike<U> | null | undefined} secondArr - The second array to compare
|
|
65
|
+
* @param {ArrayLike<V> | null | undefined} thirdArr - The third array to compare
|
|
66
|
+
* @param {ArrayLike<V> | null | undefined} fourthArr - The fourth array to compare
|
|
67
|
+
* @param {(x: T, y: U | V | W) => boolean} areItemsEqual - Custom equality function
|
|
68
|
+
* @returns {T[]} Elements from first array that match in all arrays
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* const arr1 = [{id: 1}, {id: 2}];
|
|
72
|
+
* const arr2 = [{id: 2}, {id: 3}];
|
|
73
|
+
* const arr3 = [{id: 2}, {id: 4}];
|
|
74
|
+
* const arr4 = [{id: 2}, {id: 5}];
|
|
75
|
+
* const result = intersectionWith(arr1, arr2, arr3, arr4, (a, b) => a.id === b.id);
|
|
76
|
+
* // result is [{id: 2}]
|
|
77
|
+
*/
|
|
78
|
+
declare function intersectionWith<T, U, V, W>(firstArr: ArrayLike<T> | null | undefined, secondArr: ArrayLike<U> | null | undefined, thirdArr: ArrayLike<V> | null | undefined, fourthArr: ArrayLike<V> | null | undefined, areItemsEqual: (x: T, y: U | V | W) => boolean): T[];
|
|
79
|
+
/**
|
|
80
|
+
* Returns the intersection of multiple arrays based on a custom equality function.
|
|
81
|
+
*
|
|
82
|
+
* @template T - The type of elements in the arrays
|
|
83
|
+
* @param {ArrayLike<T> | null | undefined} firstArr - The first array to compare
|
|
84
|
+
* @param {...(ArrayLike<T> | null | undefined | ((x: T, y: T) => boolean))} otherArrs - Additional arrays and optional equality function
|
|
85
|
+
* @returns {T[]} Elements from first array that match in all arrays
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* const arr1 = [{id: 1}, {id: 2}];
|
|
89
|
+
* const arr2 = [{id: 2}, {id: 3}];
|
|
90
|
+
* const arr3 = [{id: 2}, {id: 4}];
|
|
91
|
+
* const result = intersectionWith(arr1, arr2, arr3, (a, b) => a.id === b.id);
|
|
92
|
+
* // result is [{id: 2}]
|
|
93
|
+
*/
|
|
94
|
+
declare function intersectionWith<T>(firstArr: ArrayLike<T> | null | undefined, ...otherArrs: Array<ArrayLike<T> | null | undefined | ((x: T, y: T) => boolean)>): T[];
|
|
95
|
+
|
|
96
|
+
export { intersectionWith };
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the intersection of two arrays based on a custom equality function.
|
|
3
|
+
*
|
|
4
|
+
* This function takes two arrays and a custom equality function. It returns a new array containing
|
|
5
|
+
* the elements from the first array that have matching elements in the second array, as determined
|
|
6
|
+
* by the custom equality function. It effectively filters out any elements from the first array that
|
|
7
|
+
* do not have corresponding matches in the second array according to the equality function.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type of elements in the first array.
|
|
10
|
+
* @template U - The type of elements in the second array.
|
|
11
|
+
* @param {T[]} firstArr - The first array to compare.
|
|
12
|
+
* @param {U[]} secondArr - The second array to compare.
|
|
13
|
+
* @param {(x: T, y: U) => boolean} areItemsEqual - A custom function to determine if two elements are equal.
|
|
14
|
+
* This function takes two arguments, one from each array, and returns `true` if the elements are considered equal, and `false` otherwise.
|
|
15
|
+
* @returns {T[]} A new array containing the elements from the first array that have corresponding matches in the second array according to the custom equality function.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }];
|
|
19
|
+
* const array2 = [{ id: 2 }, { id: 4 }];
|
|
20
|
+
* const areItemsEqual = (a, b) => a.id === b.id;
|
|
21
|
+
* const result = intersectionWith(array1, array2, areItemsEqual);
|
|
22
|
+
* // result will be [{ id: 2 }] since this element has a matching id in both arrays.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const array1 = [
|
|
26
|
+
* { id: 1, name: 'jane' },
|
|
27
|
+
* { id: 2, name: 'amy' },
|
|
28
|
+
* { id: 3, name: 'michael' },
|
|
29
|
+
* ];
|
|
30
|
+
* const array2 = [2, 4];
|
|
31
|
+
* const areItemsEqual = (a, b) => a.id === b;
|
|
32
|
+
* const result = intersectionWith(array1, array2, areItemsEqual);
|
|
33
|
+
* // result will be [{ id: 2, name: 'amy' }] since this element has a matching id that is equal to seconds array's element.
|
|
34
|
+
*/
|
|
35
|
+
declare function intersectionWith<T, U>(firstArr: ArrayLike<T> | null | undefined, secondArr: ArrayLike<U> | null | undefined, areItemsEqual: (x: T, y: U) => boolean): T[];
|
|
36
|
+
/**
|
|
37
|
+
* Returns the intersection of three arrays based on a custom equality function.
|
|
38
|
+
*
|
|
39
|
+
* @template T - The type of elements in the first array
|
|
40
|
+
* @template U - The type of elements in the second array
|
|
41
|
+
* @template V - The type of elements in the third array
|
|
42
|
+
* @param {ArrayLike<T> | null | undefined} firstArr - The first array to compare
|
|
43
|
+
* @param {ArrayLike<U> | null | undefined} secondArr - The second array to compare
|
|
44
|
+
* @param {ArrayLike<V> | null | undefined} thirdArr - The third array to compare
|
|
45
|
+
* @param {(x: T, y: U | V) => boolean} areItemsEqual - Custom equality function
|
|
46
|
+
* @returns {T[]} Elements from first array that match in all arrays
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* const arr1 = [{id: 1}, {id: 2}];
|
|
50
|
+
* const arr2 = [{id: 2}, {id: 3}];
|
|
51
|
+
* const arr3 = [{id: 2}, {id: 4}];
|
|
52
|
+
* const result = intersectionWith(arr1, arr2, arr3, (a, b) => a.id === b.id);
|
|
53
|
+
* // result is [{id: 2}]
|
|
54
|
+
*/
|
|
55
|
+
declare function intersectionWith<T, U, V>(firstArr: ArrayLike<T> | null | undefined, secondArr: ArrayLike<U> | null | undefined, thirdArr: ArrayLike<V> | null | undefined, areItemsEqual: (x: T, y: U | V) => boolean): T[];
|
|
56
|
+
/**
|
|
57
|
+
* Returns the intersection of four arrays based on a custom equality function.
|
|
58
|
+
*
|
|
59
|
+
* @template T - The type of elements in the first array
|
|
60
|
+
* @template U - The type of elements in the second array
|
|
61
|
+
* @template V - The type of elements in the third array
|
|
62
|
+
* @template W - The type of elements in the fourth array
|
|
63
|
+
* @param {ArrayLike<T> | null | undefined} firstArr - The first array to compare
|
|
64
|
+
* @param {ArrayLike<U> | null | undefined} secondArr - The second array to compare
|
|
65
|
+
* @param {ArrayLike<V> | null | undefined} thirdArr - The third array to compare
|
|
66
|
+
* @param {ArrayLike<V> | null | undefined} fourthArr - The fourth array to compare
|
|
67
|
+
* @param {(x: T, y: U | V | W) => boolean} areItemsEqual - Custom equality function
|
|
68
|
+
* @returns {T[]} Elements from first array that match in all arrays
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* const arr1 = [{id: 1}, {id: 2}];
|
|
72
|
+
* const arr2 = [{id: 2}, {id: 3}];
|
|
73
|
+
* const arr3 = [{id: 2}, {id: 4}];
|
|
74
|
+
* const arr4 = [{id: 2}, {id: 5}];
|
|
75
|
+
* const result = intersectionWith(arr1, arr2, arr3, arr4, (a, b) => a.id === b.id);
|
|
76
|
+
* // result is [{id: 2}]
|
|
77
|
+
*/
|
|
78
|
+
declare function intersectionWith<T, U, V, W>(firstArr: ArrayLike<T> | null | undefined, secondArr: ArrayLike<U> | null | undefined, thirdArr: ArrayLike<V> | null | undefined, fourthArr: ArrayLike<V> | null | undefined, areItemsEqual: (x: T, y: U | V | W) => boolean): T[];
|
|
79
|
+
/**
|
|
80
|
+
* Returns the intersection of multiple arrays based on a custom equality function.
|
|
81
|
+
*
|
|
82
|
+
* @template T - The type of elements in the arrays
|
|
83
|
+
* @param {ArrayLike<T> | null | undefined} firstArr - The first array to compare
|
|
84
|
+
* @param {...(ArrayLike<T> | null | undefined | ((x: T, y: T) => boolean))} otherArrs - Additional arrays and optional equality function
|
|
85
|
+
* @returns {T[]} Elements from first array that match in all arrays
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* const arr1 = [{id: 1}, {id: 2}];
|
|
89
|
+
* const arr2 = [{id: 2}, {id: 3}];
|
|
90
|
+
* const arr3 = [{id: 2}, {id: 4}];
|
|
91
|
+
* const result = intersectionWith(arr1, arr2, arr3, (a, b) => a.id === b.id);
|
|
92
|
+
* // result is [{id: 2}]
|
|
93
|
+
*/
|
|
94
|
+
declare function intersectionWith<T>(firstArr: ArrayLike<T> | null | undefined, ...otherArrs: Array<ArrayLike<T> | null | undefined | ((x: T, y: T) => boolean)>): T[];
|
|
95
|
+
|
|
96
|
+
export { intersectionWith };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { last } from './last.mjs';
|
|
2
|
+
import { intersectionWith as intersectionWith$1 } from '../../array/intersectionWith.mjs';
|
|
3
|
+
import { uniq } from './uniq.mjs';
|
|
4
|
+
import { eq } from '../util/eq.mjs';
|
|
5
|
+
|
|
6
|
+
function intersectionWith(firstArr, ...otherArrs) {
|
|
7
|
+
console.log(firstArr);
|
|
8
|
+
if (firstArr == null) {
|
|
9
|
+
return [];
|
|
10
|
+
}
|
|
11
|
+
const _comparator = last(otherArrs);
|
|
12
|
+
let comparator = eq;
|
|
13
|
+
let uniq$1 = uniq;
|
|
14
|
+
if (typeof _comparator === 'function') {
|
|
15
|
+
comparator = _comparator;
|
|
16
|
+
uniq$1 = uniqPreserve0;
|
|
17
|
+
otherArrs.pop();
|
|
18
|
+
}
|
|
19
|
+
let result = uniq$1(Array.from(firstArr));
|
|
20
|
+
for (let i = 0; i < otherArrs.length; ++i) {
|
|
21
|
+
const otherArr = otherArrs[i];
|
|
22
|
+
if (otherArr == null) {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
result = intersectionWith$1(result, Array.from(otherArr), comparator);
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
function uniqPreserve0(arr) {
|
|
30
|
+
const result = [];
|
|
31
|
+
const added = new Set();
|
|
32
|
+
for (let i = 0; i < arr.length; i++) {
|
|
33
|
+
const item = arr[i];
|
|
34
|
+
if (added.has(item)) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
result.push(item);
|
|
38
|
+
added.add(item);
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { intersectionWith };
|
package/dist/compat/index.d.mts
CHANGED
|
@@ -5,7 +5,6 @@ export { flatMapDeep } from '../array/flatMapDeep.mjs';
|
|
|
5
5
|
export { forEachRight } from '../array/forEachRight.mjs';
|
|
6
6
|
export { groupBy } from '../array/groupBy.mjs';
|
|
7
7
|
export { initial } from '../array/initial.mjs';
|
|
8
|
-
export { intersectionWith } from '../array/intersectionWith.mjs';
|
|
9
8
|
export { isSubset } from '../array/isSubset.mjs';
|
|
10
9
|
export { isSubsetWith } from '../array/isSubsetWith.mjs';
|
|
11
10
|
export { keyBy } from '../array/keyBy.mjs';
|
|
@@ -85,6 +84,7 @@ export { fill } from './array/fill.mjs';
|
|
|
85
84
|
export { filter } from './array/filter.mjs';
|
|
86
85
|
export { find } from './array/find.mjs';
|
|
87
86
|
export { findIndex } from './array/findIndex.mjs';
|
|
87
|
+
export { findLast } from './array/findLast.mjs';
|
|
88
88
|
export { findLastIndex } from './array/findLastIndex.mjs';
|
|
89
89
|
export { flatten } from './array/flatten.mjs';
|
|
90
90
|
export { flattenDeep } from './array/flattenDeep.mjs';
|
|
@@ -95,6 +95,7 @@ export { includes } from './array/includes.mjs';
|
|
|
95
95
|
export { indexOf } from './array/indexOf.mjs';
|
|
96
96
|
export { intersection } from './array/intersection.mjs';
|
|
97
97
|
export { intersectionBy } from './array/intersectionBy.mjs';
|
|
98
|
+
export { intersectionWith } from './array/intersectionWith.mjs';
|
|
98
99
|
export { join } from './array/join.mjs';
|
|
99
100
|
export { last } from './array/last.mjs';
|
|
100
101
|
export { lastIndexOf } from './array/lastIndexOf.mjs';
|
package/dist/compat/index.d.ts
CHANGED
|
@@ -5,7 +5,6 @@ export { flatMapDeep } from '../array/flatMapDeep.js';
|
|
|
5
5
|
export { forEachRight } from '../array/forEachRight.js';
|
|
6
6
|
export { groupBy } from '../array/groupBy.js';
|
|
7
7
|
export { initial } from '../array/initial.js';
|
|
8
|
-
export { intersectionWith } from '../array/intersectionWith.js';
|
|
9
8
|
export { isSubset } from '../array/isSubset.js';
|
|
10
9
|
export { isSubsetWith } from '../array/isSubsetWith.js';
|
|
11
10
|
export { keyBy } from '../array/keyBy.js';
|
|
@@ -85,6 +84,7 @@ export { fill } from './array/fill.js';
|
|
|
85
84
|
export { filter } from './array/filter.js';
|
|
86
85
|
export { find } from './array/find.js';
|
|
87
86
|
export { findIndex } from './array/findIndex.js';
|
|
87
|
+
export { findLast } from './array/findLast.js';
|
|
88
88
|
export { findLastIndex } from './array/findLastIndex.js';
|
|
89
89
|
export { flatten } from './array/flatten.js';
|
|
90
90
|
export { flattenDeep } from './array/flattenDeep.js';
|
|
@@ -95,6 +95,7 @@ export { includes } from './array/includes.js';
|
|
|
95
95
|
export { indexOf } from './array/indexOf.js';
|
|
96
96
|
export { intersection } from './array/intersection.js';
|
|
97
97
|
export { intersectionBy } from './array/intersectionBy.js';
|
|
98
|
+
export { intersectionWith } from './array/intersectionWith.js';
|
|
98
99
|
export { join } from './array/join.js';
|
|
99
100
|
export { last } from './array/last.js';
|
|
100
101
|
export { lastIndexOf } from './array/lastIndexOf.js';
|