es-toolkit 1.31.0-dev.987 → 1.31.0-dev.991
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.
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* @template T
|
|
5
5
|
* @param {ArrayLike<T> | null | undefined} arr - The array to search through.
|
|
6
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=0] - The index to start the search from, defaults to 0.
|
|
7
8
|
* @returns {T | undefined} - The first item that matches the predicate, or `undefined` if no match is found.
|
|
8
9
|
*
|
|
9
10
|
* @example
|
|
@@ -12,13 +13,14 @@
|
|
|
12
13
|
* const result = find(items, (item) => item > 3);
|
|
13
14
|
* console.log(result); // 4
|
|
14
15
|
*/
|
|
15
|
-
declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): T | undefined;
|
|
16
|
+
declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatch: (item: T, index: number, arr: readonly T[]) => unknown, fromIndex?: number): T | undefined;
|
|
16
17
|
/**
|
|
17
18
|
* Finds the first item in an array that matches the given partial object.
|
|
18
19
|
*
|
|
19
20
|
* @template T
|
|
20
21
|
* @param {ArrayLike<T> | null | undefined} arr - The array to search through.
|
|
21
22
|
* @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
|
|
23
|
+
* @param {number} [fromIndex=0] - The index to start the search from, defaults to 0.
|
|
22
24
|
* @returns {T | undefined} - The first item that matches the partial object, or `undefined` if no match is found.
|
|
23
25
|
*
|
|
24
26
|
* @example
|
|
@@ -27,13 +29,14 @@ declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatch: (item:
|
|
|
27
29
|
* const result = find(items, { name: 'Bob' });
|
|
28
30
|
* console.log(result); // { id: 2, name: 'Bob' }
|
|
29
31
|
*/
|
|
30
|
-
declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T
|
|
32
|
+
declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>, fromIndex?: number): T | undefined;
|
|
31
33
|
/**
|
|
32
34
|
* Finds the first item in an array that matches a property with a specific value.
|
|
33
35
|
*
|
|
34
36
|
* @template T
|
|
35
37
|
* @param {ArrayLike<T> | null | undefined} arr - The array to search through.
|
|
36
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=0] - The index to start the search from, defaults to 0.
|
|
37
40
|
* @returns {T | undefined} - The first item that has the specified property value, or `undefined` if no match is found.
|
|
38
41
|
*
|
|
39
42
|
* @example
|
|
@@ -42,13 +45,14 @@ declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partia
|
|
|
42
45
|
* const result = find(items, ['name', 'Alice']);
|
|
43
46
|
* console.log(result); // { id: 1, name: 'Alice' }
|
|
44
47
|
*/
|
|
45
|
-
declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown]): T | undefined;
|
|
48
|
+
declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown], fromIndex?: number): T | undefined;
|
|
46
49
|
/**
|
|
47
50
|
* Finds the first item in an array that has a specific property, where the property name is provided as a PropertyKey.
|
|
48
51
|
*
|
|
49
52
|
* @template T
|
|
50
53
|
* @param {ArrayLike<T> | null | undefined} arr - The array to search through.
|
|
51
54
|
* @param {PropertyKey} propertyToCheck - The property name to check.
|
|
55
|
+
* @param {number} [fromIndex=0] - The index to start the search from, defaults to 0.
|
|
52
56
|
* @returns {T | undefined} - The first item that has the specified property, or `undefined` if no match is found.
|
|
53
57
|
*
|
|
54
58
|
* @example
|
|
@@ -57,13 +61,14 @@ declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty
|
|
|
57
61
|
* const result = find(items, 'name');
|
|
58
62
|
* console.log(result); // { id: 1, name: 'Alice' }
|
|
59
63
|
*/
|
|
60
|
-
declare function find<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: PropertyKey): T | undefined;
|
|
64
|
+
declare function find<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: PropertyKey, fromIndex?: number): T | undefined;
|
|
61
65
|
/**
|
|
62
66
|
* Finds the first item in an object that matches the given predicate function.
|
|
63
67
|
*
|
|
64
68
|
* @template T
|
|
65
69
|
* @param {T | null | undefined} object - The object to search through.
|
|
66
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=0] - The index to start the search from, defaults to 0.
|
|
67
72
|
* @returns {T | undefined} - The first property value that matches the predicate, or `undefined` if no match is found.
|
|
68
73
|
*
|
|
69
74
|
* @example
|
|
@@ -72,13 +77,14 @@ declare function find<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck:
|
|
|
72
77
|
* const result = find(obj, (item) => item > 2);
|
|
73
78
|
* console.log(result); // 3
|
|
74
79
|
*/
|
|
75
|
-
declare function find<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: (item: T[keyof T], index: keyof T, object: T) => unknown): T | undefined;
|
|
80
|
+
declare function find<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: (item: T[keyof T], index: keyof T, object: T) => unknown, fromIndex?: number): T | undefined;
|
|
76
81
|
/**
|
|
77
82
|
* Finds the first item in an object that matches the given partial value.
|
|
78
83
|
*
|
|
79
84
|
* @template T
|
|
80
85
|
* @param {T | null | undefined} object - The object to search through.
|
|
81
86
|
* @param {Partial<T[keyof T]>} doesMatch - A partial value to match against the values of the object.
|
|
87
|
+
* @param {number} [fromIndex=0] - The index to start the search from, defaults to 0.
|
|
82
88
|
* @returns {T | undefined} - The first property value that matches the partial value, or `undefined` if no match is found.
|
|
83
89
|
*
|
|
84
90
|
* @example
|
|
@@ -87,13 +93,14 @@ declare function find<T extends Record<string, unknown>>(object: T | null | unde
|
|
|
87
93
|
* const result = find(obj, { name: 'Bob' });
|
|
88
94
|
* console.log(result); // { id: 2, name: 'Bob' }
|
|
89
95
|
*/
|
|
90
|
-
declare function find<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: Partial<T[keyof T]
|
|
96
|
+
declare function find<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: Partial<T[keyof T]>, fromIndex?: number): T | undefined;
|
|
91
97
|
/**
|
|
92
98
|
* Finds the first item in an object that matches a property with a specific value.
|
|
93
99
|
*
|
|
94
100
|
* @template T
|
|
95
101
|
* @param {T | null | undefined} object - The object to search through.
|
|
96
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=0] - The index to start the search from, defaults to 0.
|
|
97
104
|
* @returns {T | undefined} - The first item that has the specified property value, or `undefined` if no match is found.
|
|
98
105
|
*
|
|
99
106
|
* @example
|
|
@@ -102,13 +109,14 @@ declare function find<T extends Record<string, unknown>>(object: T | null | unde
|
|
|
102
109
|
* const result = find(items, ['name', 'Alice']);
|
|
103
110
|
* console.log(result); // { id: 1, name: 'Alice' }
|
|
104
111
|
*/
|
|
105
|
-
declare function find<T extends Record<string, unknown>>(object: T | null | undefined, doesMatchProperty: [keyof T[keyof T], unknown]): T | undefined;
|
|
112
|
+
declare function find<T extends Record<string, unknown>>(object: T | null | undefined, doesMatchProperty: [keyof T[keyof T], unknown], fromIndex?: number): T | undefined;
|
|
106
113
|
/**
|
|
107
114
|
* Finds the first item in an object that has a specific property, where the property name is provided as a PropertyKey.
|
|
108
115
|
*
|
|
109
116
|
* @template T
|
|
110
117
|
* @param {T | null | undefined} object - The object to search through.
|
|
111
118
|
* @param {PropertyKey} propertyToCheck - The property name to check.
|
|
119
|
+
* @param {number} [fromIndex=0] - The index to start the search from, defaults to 0.
|
|
112
120
|
* @returns {T | undefined} - The first property value that has the specified property, or `undefined` if no match is found.
|
|
113
121
|
*
|
|
114
122
|
* @example
|
|
@@ -117,6 +125,6 @@ declare function find<T extends Record<string, unknown>>(object: T | null | unde
|
|
|
117
125
|
* const result = find(obj, 'name');
|
|
118
126
|
* console.log(result); // { id: 1, name: 'Alice' }
|
|
119
127
|
*/
|
|
120
|
-
declare function find<T extends Record<string, unknown>>(object: T | null | undefined, propertyToCheck: PropertyKey): T | undefined;
|
|
128
|
+
declare function find<T extends Record<string, unknown>>(object: T | null | undefined, propertyToCheck: PropertyKey, fromIndex?: number): T | undefined;
|
|
121
129
|
|
|
122
130
|
export { find };
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* @template T
|
|
5
5
|
* @param {ArrayLike<T> | null | undefined} arr - The array to search through.
|
|
6
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=0] - The index to start the search from, defaults to 0.
|
|
7
8
|
* @returns {T | undefined} - The first item that matches the predicate, or `undefined` if no match is found.
|
|
8
9
|
*
|
|
9
10
|
* @example
|
|
@@ -12,13 +13,14 @@
|
|
|
12
13
|
* const result = find(items, (item) => item > 3);
|
|
13
14
|
* console.log(result); // 4
|
|
14
15
|
*/
|
|
15
|
-
declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): T | undefined;
|
|
16
|
+
declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatch: (item: T, index: number, arr: readonly T[]) => unknown, fromIndex?: number): T | undefined;
|
|
16
17
|
/**
|
|
17
18
|
* Finds the first item in an array that matches the given partial object.
|
|
18
19
|
*
|
|
19
20
|
* @template T
|
|
20
21
|
* @param {ArrayLike<T> | null | undefined} arr - The array to search through.
|
|
21
22
|
* @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
|
|
23
|
+
* @param {number} [fromIndex=0] - The index to start the search from, defaults to 0.
|
|
22
24
|
* @returns {T | undefined} - The first item that matches the partial object, or `undefined` if no match is found.
|
|
23
25
|
*
|
|
24
26
|
* @example
|
|
@@ -27,13 +29,14 @@ declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatch: (item:
|
|
|
27
29
|
* const result = find(items, { name: 'Bob' });
|
|
28
30
|
* console.log(result); // { id: 2, name: 'Bob' }
|
|
29
31
|
*/
|
|
30
|
-
declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T
|
|
32
|
+
declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>, fromIndex?: number): T | undefined;
|
|
31
33
|
/**
|
|
32
34
|
* Finds the first item in an array that matches a property with a specific value.
|
|
33
35
|
*
|
|
34
36
|
* @template T
|
|
35
37
|
* @param {ArrayLike<T> | null | undefined} arr - The array to search through.
|
|
36
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=0] - The index to start the search from, defaults to 0.
|
|
37
40
|
* @returns {T | undefined} - The first item that has the specified property value, or `undefined` if no match is found.
|
|
38
41
|
*
|
|
39
42
|
* @example
|
|
@@ -42,13 +45,14 @@ declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partia
|
|
|
42
45
|
* const result = find(items, ['name', 'Alice']);
|
|
43
46
|
* console.log(result); // { id: 1, name: 'Alice' }
|
|
44
47
|
*/
|
|
45
|
-
declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown]): T | undefined;
|
|
48
|
+
declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown], fromIndex?: number): T | undefined;
|
|
46
49
|
/**
|
|
47
50
|
* Finds the first item in an array that has a specific property, where the property name is provided as a PropertyKey.
|
|
48
51
|
*
|
|
49
52
|
* @template T
|
|
50
53
|
* @param {ArrayLike<T> | null | undefined} arr - The array to search through.
|
|
51
54
|
* @param {PropertyKey} propertyToCheck - The property name to check.
|
|
55
|
+
* @param {number} [fromIndex=0] - The index to start the search from, defaults to 0.
|
|
52
56
|
* @returns {T | undefined} - The first item that has the specified property, or `undefined` if no match is found.
|
|
53
57
|
*
|
|
54
58
|
* @example
|
|
@@ -57,13 +61,14 @@ declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty
|
|
|
57
61
|
* const result = find(items, 'name');
|
|
58
62
|
* console.log(result); // { id: 1, name: 'Alice' }
|
|
59
63
|
*/
|
|
60
|
-
declare function find<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: PropertyKey): T | undefined;
|
|
64
|
+
declare function find<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: PropertyKey, fromIndex?: number): T | undefined;
|
|
61
65
|
/**
|
|
62
66
|
* Finds the first item in an object that matches the given predicate function.
|
|
63
67
|
*
|
|
64
68
|
* @template T
|
|
65
69
|
* @param {T | null | undefined} object - The object to search through.
|
|
66
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=0] - The index to start the search from, defaults to 0.
|
|
67
72
|
* @returns {T | undefined} - The first property value that matches the predicate, or `undefined` if no match is found.
|
|
68
73
|
*
|
|
69
74
|
* @example
|
|
@@ -72,13 +77,14 @@ declare function find<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck:
|
|
|
72
77
|
* const result = find(obj, (item) => item > 2);
|
|
73
78
|
* console.log(result); // 3
|
|
74
79
|
*/
|
|
75
|
-
declare function find<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: (item: T[keyof T], index: keyof T, object: T) => unknown): T | undefined;
|
|
80
|
+
declare function find<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: (item: T[keyof T], index: keyof T, object: T) => unknown, fromIndex?: number): T | undefined;
|
|
76
81
|
/**
|
|
77
82
|
* Finds the first item in an object that matches the given partial value.
|
|
78
83
|
*
|
|
79
84
|
* @template T
|
|
80
85
|
* @param {T | null | undefined} object - The object to search through.
|
|
81
86
|
* @param {Partial<T[keyof T]>} doesMatch - A partial value to match against the values of the object.
|
|
87
|
+
* @param {number} [fromIndex=0] - The index to start the search from, defaults to 0.
|
|
82
88
|
* @returns {T | undefined} - The first property value that matches the partial value, or `undefined` if no match is found.
|
|
83
89
|
*
|
|
84
90
|
* @example
|
|
@@ -87,13 +93,14 @@ declare function find<T extends Record<string, unknown>>(object: T | null | unde
|
|
|
87
93
|
* const result = find(obj, { name: 'Bob' });
|
|
88
94
|
* console.log(result); // { id: 2, name: 'Bob' }
|
|
89
95
|
*/
|
|
90
|
-
declare function find<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: Partial<T[keyof T]
|
|
96
|
+
declare function find<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: Partial<T[keyof T]>, fromIndex?: number): T | undefined;
|
|
91
97
|
/**
|
|
92
98
|
* Finds the first item in an object that matches a property with a specific value.
|
|
93
99
|
*
|
|
94
100
|
* @template T
|
|
95
101
|
* @param {T | null | undefined} object - The object to search through.
|
|
96
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=0] - The index to start the search from, defaults to 0.
|
|
97
104
|
* @returns {T | undefined} - The first item that has the specified property value, or `undefined` if no match is found.
|
|
98
105
|
*
|
|
99
106
|
* @example
|
|
@@ -102,13 +109,14 @@ declare function find<T extends Record<string, unknown>>(object: T | null | unde
|
|
|
102
109
|
* const result = find(items, ['name', 'Alice']);
|
|
103
110
|
* console.log(result); // { id: 1, name: 'Alice' }
|
|
104
111
|
*/
|
|
105
|
-
declare function find<T extends Record<string, unknown>>(object: T | null | undefined, doesMatchProperty: [keyof T[keyof T], unknown]): T | undefined;
|
|
112
|
+
declare function find<T extends Record<string, unknown>>(object: T | null | undefined, doesMatchProperty: [keyof T[keyof T], unknown], fromIndex?: number): T | undefined;
|
|
106
113
|
/**
|
|
107
114
|
* Finds the first item in an object that has a specific property, where the property name is provided as a PropertyKey.
|
|
108
115
|
*
|
|
109
116
|
* @template T
|
|
110
117
|
* @param {T | null | undefined} object - The object to search through.
|
|
111
118
|
* @param {PropertyKey} propertyToCheck - The property name to check.
|
|
119
|
+
* @param {number} [fromIndex=0] - The index to start the search from, defaults to 0.
|
|
112
120
|
* @returns {T | undefined} - The first property value that has the specified property, or `undefined` if no match is found.
|
|
113
121
|
*
|
|
114
122
|
* @example
|
|
@@ -117,6 +125,6 @@ declare function find<T extends Record<string, unknown>>(object: T | null | unde
|
|
|
117
125
|
* const result = find(obj, 'name');
|
|
118
126
|
* console.log(result); // { id: 1, name: 'Alice' }
|
|
119
127
|
*/
|
|
120
|
-
declare function find<T extends Record<string, unknown>>(object: T | null | undefined, propertyToCheck: PropertyKey): T | undefined;
|
|
128
|
+
declare function find<T extends Record<string, unknown>>(object: T | null | undefined, propertyToCheck: PropertyKey, fromIndex?: number): T | undefined;
|
|
121
129
|
|
|
122
130
|
export { find };
|
|
@@ -2,11 +2,14 @@ import { property } from '../object/property.mjs';
|
|
|
2
2
|
import { matches } from '../predicate/matches.mjs';
|
|
3
3
|
import { matchesProperty } from '../predicate/matchesProperty.mjs';
|
|
4
4
|
|
|
5
|
-
function find(source, doesMatch) {
|
|
5
|
+
function find(source, doesMatch, fromIndex = 0) {
|
|
6
6
|
if (!source) {
|
|
7
7
|
return undefined;
|
|
8
8
|
}
|
|
9
|
-
|
|
9
|
+
if (fromIndex < 0) {
|
|
10
|
+
fromIndex = Math.max(source.length + fromIndex, 0);
|
|
11
|
+
}
|
|
12
|
+
const values = Array.isArray(source) ? source.slice(fromIndex) : Object.values(source).slice(fromIndex);
|
|
10
13
|
switch (typeof doesMatch) {
|
|
11
14
|
case 'function': {
|
|
12
15
|
if (!Array.isArray(source)) {
|
package/dist/compat/index.js
CHANGED
|
@@ -726,11 +726,14 @@ function filter(source, predicate) {
|
|
|
726
726
|
}
|
|
727
727
|
}
|
|
728
728
|
|
|
729
|
-
function find(source, doesMatch) {
|
|
729
|
+
function find(source, doesMatch, fromIndex = 0) {
|
|
730
730
|
if (!source) {
|
|
731
731
|
return undefined;
|
|
732
732
|
}
|
|
733
|
-
|
|
733
|
+
if (fromIndex < 0) {
|
|
734
|
+
fromIndex = Math.max(source.length + fromIndex, 0);
|
|
735
|
+
}
|
|
736
|
+
const values = Array.isArray(source) ? source.slice(fromIndex) : Object.values(source).slice(fromIndex);
|
|
734
737
|
switch (typeof doesMatch) {
|
|
735
738
|
case 'function': {
|
|
736
739
|
if (!Array.isArray(source)) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "es-toolkit",
|
|
3
3
|
"description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
|
|
4
|
-
"version": "1.31.0-dev.
|
|
4
|
+
"version": "1.31.0-dev.991+74858bcd",
|
|
5
5
|
"homepage": "https://es-toolkit.slash.page",
|
|
6
6
|
"bugs": "https://github.com/toss/es-toolkit/issues",
|
|
7
7
|
"repository": {
|