es-toolkit 1.25.0-dev.791 → 1.25.0-dev.792

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.
@@ -2,7 +2,7 @@
2
2
  * Filters items from a array and returns an array of elements.
3
3
  *
4
4
  * @template T
5
- * @param {T[]} arr - The array to iterate over.
5
+ * @param {ArrayLike<T> | null | undefined} arr - The array to iterate over.
6
6
  * @param {(item: T, index: number, arr: T[]) => unknown} doesMatch - The function invoked per iteration.
7
7
  * @returns {T[]} - Returns a new array of elements that satisfy the given doesMatch function.
8
8
  *
@@ -10,12 +10,12 @@
10
10
  * filter([1, 2, 3], n => n % 2 === 0)
11
11
  * // => [2]
12
12
  */
13
- declare function filter<T>(arr: readonly T[], doesMatch?: (item: T, index: number, arr: readonly T[]) => unknown): T[];
13
+ declare function filter<T>(arr: ArrayLike<T> | null | undefined, doesMatch?: (item: T, index: number, arr: readonly T[]) => unknown): T[];
14
14
  /**
15
15
  * Filters elements in a arr that match the properties of the given partial object.
16
16
  *
17
17
  * @template T
18
- * @param {T[]} arr - The array to iterate over.
18
+ * @param {ArrayLike<T> | null | undefined} arr - The array to iterate over.
19
19
  * @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
20
20
  * @returns {T[]} - Returns a new array of elements that match the given properties.
21
21
  *
@@ -24,12 +24,12 @@ declare function filter<T>(arr: readonly T[], doesMatch?: (item: T, index: numbe
24
24
  * filter(arr, { name: 'Bob' });
25
25
  * // => [{ id: 2, name: 'Bob' }]
26
26
  */
27
- declare function filter<T>(arr: readonly T[], doesMatch: Partial<T>): T[];
27
+ declare function filter<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>): T[];
28
28
  /**
29
29
  * Filters elements in a arr that match the given key-value pair.
30
30
  *
31
31
  * @template T
32
- * @param {T[]} arr - The array to iterate over.
32
+ * @param {ArrayLike<T> | null | undefined} arr - The array to iterate over.
33
33
  * @param {[keyof T, unknown]} doesMatchProperty - The key-value pair to match.
34
34
  * @returns {T[]} - Returns a new array of elements that match the given key-value pair.
35
35
  *
@@ -38,12 +38,12 @@ declare function filter<T>(arr: readonly T[], doesMatch: Partial<T>): T[];
38
38
  * filter(arr, ['name', 'Alice']);
39
39
  * // => [{ id: 1, name: 'Alice' }]
40
40
  */
41
- declare function filter<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): T[];
41
+ declare function filter<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown]): T[];
42
42
  /**
43
43
  * Filters the arr, returning elements that contain the given property name.
44
44
  *
45
45
  * @template T
46
- * @param {T[]} arr - The array to iterate over.
46
+ * @param {ArrayLike<T> | null | undefined} arr - The array to iterate over.
47
47
  * @param {string} propertyToCheck - The property name to check.
48
48
  * @returns {T[]} - Returns a new array of elements that match the given property name.
49
49
  *
@@ -52,7 +52,7 @@ declare function filter<T>(arr: readonly T[], doesMatchProperty: [keyof T, unkno
52
52
  * filter(arr, 'name');
53
53
  * // => [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
54
54
  */
55
- declare function filter<T>(arr: readonly T[], propertyToCheck: string): T[];
55
+ declare function filter<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: string): T[];
56
56
  /**
57
57
  * Filters items from a object and returns an array of elements that match the given predicate function.
58
58
  *
@@ -70,12 +70,12 @@ declare function filter<T>(arr: readonly T[], propertyToCheck: string): T[];
70
70
  * filter(obj, value => value > 2)
71
71
  * // => [3]
72
72
  */
73
- declare function filter<T extends Record<string, unknown>>(object: T, doesMatch: (value: T[keyof T], key: keyof T, object: T) => unknown): T[];
73
+ declare function filter<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: (value: T[keyof T], key: keyof T, object: T) => unknown): T[];
74
74
  /**
75
75
  * Filters elements in a object that match the properties of the given partial object.
76
76
  *
77
77
  * @template T
78
- * @param {T} object - The object to iterate over.
78
+ * @param {T | null | undefined} object - The object to iterate over.
79
79
  * @param {Partial<T[keyof T]>} doesMatch - The partial object to match
80
80
  * @returns {T[]} - Returns a new array of elements that match the given properties.pair.
81
81
  *
@@ -84,12 +84,12 @@ declare function filter<T extends Record<string, unknown>>(object: T, doesMatch:
84
84
  * filter(obj, { name: 'Bob' });
85
85
  * // => [{ id: 2, name: 'Bob' }]
86
86
  */
87
- declare function filter<T extends Record<string, unknown>>(object: T, doesMatch: Partial<T[keyof T]>): T[];
87
+ declare function filter<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: Partial<T[keyof T]>): T[];
88
88
  /**
89
89
  * Filters elements in a arr that match the given key-value pair.
90
90
  *
91
91
  * @template T
92
- * @param {T} object - The object to iterate over.
92
+ * @param {T | null | undefined} object - The object to iterate over.
93
93
  * @param {[keyof T, unknown]} doesMatchProperty - The key-value pair to match.
94
94
  * @returns {T[]} - Returns a new array of elements that match the given key-value pair.
95
95
  *
@@ -98,12 +98,12 @@ declare function filter<T extends Record<string, unknown>>(object: T, doesMatch:
98
98
  * filter(obj, ['name', 'Alice']);
99
99
  * // => [{ id: 1, name: 'Alice' }]
100
100
  */
101
- declare function filter<T extends Record<string, unknown>>(object: T, doesMatchProperty: [keyof T, unknown]): T[];
101
+ declare function filter<T extends Record<string, unknown>>(object: T | null | undefined, doesMatchProperty: [keyof T, unknown]): T[];
102
102
  /**
103
103
  * Filters the object, returning elements that contain the given property name.
104
104
  *
105
105
  * @template T
106
- * @param {T} object - The object to iterate over.
106
+ * @param {T | null | undefined} object - The object to iterate over.
107
107
  * @param {string} propertyToCheck - The property name to check.
108
108
  * @returns {T[]} - Returns a new array of elements that match the given property name.
109
109
  *
@@ -112,6 +112,6 @@ declare function filter<T extends Record<string, unknown>>(object: T, doesMatchP
112
112
  * filter(obj, 'name');
113
113
  * // => [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
114
114
  */
115
- declare function filter<T extends Record<string, unknown>>(object: T, propertyToCheck: string): T[];
115
+ declare function filter<T extends Record<string, unknown>>(object: T | null | undefined, propertyToCheck: string): T[];
116
116
 
117
117
  export { filter };
@@ -5,6 +5,9 @@ import { matches } from '../predicate/matches.mjs';
5
5
  import { matchesProperty } from '../predicate/matchesProperty.mjs';
6
6
 
7
7
  function filter(source, predicate) {
8
+ if (!source) {
9
+ return [];
10
+ }
8
11
  if (!predicate) {
9
12
  predicate = identity;
10
13
  }
@@ -2,7 +2,7 @@
2
2
  * Finds the first item in an array that matches the given predicate function.
3
3
  *
4
4
  * @template T
5
- * @param {T[]} arr - The array to search through.
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
7
  * @returns {T | undefined} - The first item that matches the predicate, or `undefined` if no match is found.
8
8
  *
@@ -12,12 +12,12 @@
12
12
  * const result = find(items, (item) => item > 3);
13
13
  * console.log(result); // 4
14
14
  */
15
- declare function find<T>(arr: readonly T[], doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): T | undefined;
15
+ declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): T | undefined;
16
16
  /**
17
17
  * Finds the first item in an array that matches the given partial object.
18
18
  *
19
19
  * @template T
20
- * @param {T[]} arr - The array to search through.
20
+ * @param {ArrayLike<T> | null | undefined} arr - The array to search through.
21
21
  * @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
22
22
  * @returns {T | undefined} - The first item that matches the partial object, or `undefined` if no match is found.
23
23
  *
@@ -27,12 +27,12 @@ declare function find<T>(arr: readonly T[], doesMatch: (item: T, index: number,
27
27
  * const result = find(items, { name: 'Bob' });
28
28
  * console.log(result); // { id: 2, name: 'Bob' }
29
29
  */
30
- declare function find<T>(arr: readonly T[], doesMatch: Partial<T>): T | undefined;
30
+ declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>): T | undefined;
31
31
  /**
32
32
  * Finds the first item in an array that matches a property with a specific value.
33
33
  *
34
34
  * @template T
35
- * @param {readonly T[]} arr - The array to search through.
35
+ * @param {ArrayLike<T> | null | undefined} arr - The array to search through.
36
36
  * @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
37
37
  * @returns {T | undefined} - The first item that has the specified property value, or `undefined` if no match is found.
38
38
  *
@@ -42,12 +42,12 @@ declare function find<T>(arr: readonly T[], doesMatch: Partial<T>): T | undefine
42
42
  * const result = find(items, ['name', 'Alice']);
43
43
  * console.log(result); // { id: 1, name: 'Alice' }
44
44
  */
45
- declare function find<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): T | undefined;
45
+ declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown]): T | undefined;
46
46
  /**
47
47
  * Finds the first item in an array that has a specific property, where the property name is provided as a string.
48
48
  *
49
49
  * @template T
50
- * @param {readonly T[]} arr - The array to search through.
50
+ * @param {ArrayLike<T> | null | undefined} arr - The array to search through.
51
51
  * @param {string} propertyToCheck - The property name to check.
52
52
  * @returns {T | undefined} - The first item that has the specified property, or `undefined` if no match is found.
53
53
  *
@@ -57,12 +57,12 @@ declare function find<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown
57
57
  * const result = find(items, 'name');
58
58
  * console.log(result); // { id: 1, name: 'Alice' }
59
59
  */
60
- declare function find<T>(arr: readonly T[], propertyToCheck: string): T | undefined;
60
+ declare function find<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: string): T | undefined;
61
61
  /**
62
62
  * Finds the first item in an object that matches the given predicate function.
63
63
  *
64
64
  * @template T
65
- * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
65
+ * @param {T extends Record<string, unknown> ? T | null | undefined: never} object - The object to search through.
66
66
  * @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.
67
67
  * @returns {T | undefined} - The first property value that matches the predicate, or `undefined` if no match is found.
68
68
  *
@@ -72,12 +72,12 @@ declare function find<T>(arr: readonly T[], propertyToCheck: string): T | undefi
72
72
  * const result = find(obj, (item) => item > 2);
73
73
  * console.log(result); // 3
74
74
  */
75
- declare function find<T extends Record<string, unknown>>(object: T, doesMatch: (item: T[keyof T], index: keyof T, object: T) => unknown): T | undefined;
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;
76
76
  /**
77
77
  * Finds the first item in an object that matches the given partial value.
78
78
  *
79
79
  * @template T
80
- * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
80
+ * @param {T extends Record<string, unknown> ? T | null | undefined: never} object - The object to search through.
81
81
  * @param {Partial<T[keyof T]>} doesMatch - A partial value to match against the values of the object.
82
82
  * @returns {T | undefined} - The first property value that matches the partial value, or `undefined` if no match is found.
83
83
  *
@@ -87,12 +87,12 @@ declare function find<T extends Record<string, unknown>>(object: T, doesMatch: (
87
87
  * const result = find(obj, { name: 'Bob' });
88
88
  * console.log(result); // { id: 2, name: 'Bob' }
89
89
  */
90
- declare function find<T extends Record<string, unknown>>(object: T, doesMatch: Partial<T[keyof T]>): T | undefined;
90
+ declare function find<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: Partial<T[keyof T]>): T | undefined;
91
91
  /**
92
92
  * Finds the first item in an object that matches a property with a specific value.
93
93
  *
94
94
  * @template T
95
- * @param {readonly T[]} object - The object to search through.
95
+ * @param {T extends Record<string, unknown> ? T | null | undefined: never} object - The object to search through.
96
96
  * @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
97
97
  * @returns {T | undefined} - The first item that has the specified property value, or `undefined` if no match is found.
98
98
  *
@@ -102,12 +102,12 @@ declare function find<T extends Record<string, unknown>>(object: T, doesMatch: P
102
102
  * const result = find(items, ['name', 'Alice']);
103
103
  * console.log(result); // { id: 1, name: 'Alice' }
104
104
  */
105
- declare function find<T extends Record<string, unknown>>(object: T, doesMatchProperty: [keyof T, unknown]): T | undefined;
105
+ declare function find<T extends Record<string, unknown>>(object: T | null | undefined, doesMatchProperty: [keyof T, unknown]): T | undefined;
106
106
  /**
107
107
  * Finds the first item in an object that has a specific property, where the property name is provided as a string.
108
108
  *
109
109
  * @template T
110
- * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
110
+ * @param {T extends Record<string, unknown> ? T | null | undefined: never} object - The object to search through.
111
111
  * @param {string} propertyToCheck - The property name to check.
112
112
  * @returns {T | undefined} - The first property value that has the specified property, or `undefined` if no match is found.
113
113
  *
@@ -117,6 +117,6 @@ declare function find<T extends Record<string, unknown>>(object: T, doesMatchPro
117
117
  * const result = find(obj, 'name');
118
118
  * console.log(result); // { id: 1, name: 'Alice' }
119
119
  */
120
- declare function find<T extends Record<string, unknown>>(object: T, propertyToCheck: string): T | undefined;
120
+ declare function find<T extends Record<string, unknown>>(object: T | null | undefined, propertyToCheck: string): T | undefined;
121
121
 
122
122
  export { find };
@@ -2,7 +2,7 @@
2
2
  * Finds the first item in an array that matches the given predicate function.
3
3
  *
4
4
  * @template T
5
- * @param {T[]} arr - The array to search through.
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
7
  * @returns {T | undefined} - The first item that matches the predicate, or `undefined` if no match is found.
8
8
  *
@@ -12,12 +12,12 @@
12
12
  * const result = find(items, (item) => item > 3);
13
13
  * console.log(result); // 4
14
14
  */
15
- declare function find<T>(arr: readonly T[], doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): T | undefined;
15
+ declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): T | undefined;
16
16
  /**
17
17
  * Finds the first item in an array that matches the given partial object.
18
18
  *
19
19
  * @template T
20
- * @param {T[]} arr - The array to search through.
20
+ * @param {ArrayLike<T> | null | undefined} arr - The array to search through.
21
21
  * @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
22
22
  * @returns {T | undefined} - The first item that matches the partial object, or `undefined` if no match is found.
23
23
  *
@@ -27,12 +27,12 @@ declare function find<T>(arr: readonly T[], doesMatch: (item: T, index: number,
27
27
  * const result = find(items, { name: 'Bob' });
28
28
  * console.log(result); // { id: 2, name: 'Bob' }
29
29
  */
30
- declare function find<T>(arr: readonly T[], doesMatch: Partial<T>): T | undefined;
30
+ declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>): T | undefined;
31
31
  /**
32
32
  * Finds the first item in an array that matches a property with a specific value.
33
33
  *
34
34
  * @template T
35
- * @param {readonly T[]} arr - The array to search through.
35
+ * @param {ArrayLike<T> | null | undefined} arr - The array to search through.
36
36
  * @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
37
37
  * @returns {T | undefined} - The first item that has the specified property value, or `undefined` if no match is found.
38
38
  *
@@ -42,12 +42,12 @@ declare function find<T>(arr: readonly T[], doesMatch: Partial<T>): T | undefine
42
42
  * const result = find(items, ['name', 'Alice']);
43
43
  * console.log(result); // { id: 1, name: 'Alice' }
44
44
  */
45
- declare function find<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): T | undefined;
45
+ declare function find<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown]): T | undefined;
46
46
  /**
47
47
  * Finds the first item in an array that has a specific property, where the property name is provided as a string.
48
48
  *
49
49
  * @template T
50
- * @param {readonly T[]} arr - The array to search through.
50
+ * @param {ArrayLike<T> | null | undefined} arr - The array to search through.
51
51
  * @param {string} propertyToCheck - The property name to check.
52
52
  * @returns {T | undefined} - The first item that has the specified property, or `undefined` if no match is found.
53
53
  *
@@ -57,12 +57,12 @@ declare function find<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown
57
57
  * const result = find(items, 'name');
58
58
  * console.log(result); // { id: 1, name: 'Alice' }
59
59
  */
60
- declare function find<T>(arr: readonly T[], propertyToCheck: string): T | undefined;
60
+ declare function find<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: string): T | undefined;
61
61
  /**
62
62
  * Finds the first item in an object that matches the given predicate function.
63
63
  *
64
64
  * @template T
65
- * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
65
+ * @param {T extends Record<string, unknown> ? T | null | undefined: never} object - The object to search through.
66
66
  * @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.
67
67
  * @returns {T | undefined} - The first property value that matches the predicate, or `undefined` if no match is found.
68
68
  *
@@ -72,12 +72,12 @@ declare function find<T>(arr: readonly T[], propertyToCheck: string): T | undefi
72
72
  * const result = find(obj, (item) => item > 2);
73
73
  * console.log(result); // 3
74
74
  */
75
- declare function find<T extends Record<string, unknown>>(object: T, doesMatch: (item: T[keyof T], index: keyof T, object: T) => unknown): T | undefined;
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;
76
76
  /**
77
77
  * Finds the first item in an object that matches the given partial value.
78
78
  *
79
79
  * @template T
80
- * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
80
+ * @param {T extends Record<string, unknown> ? T | null | undefined: never} object - The object to search through.
81
81
  * @param {Partial<T[keyof T]>} doesMatch - A partial value to match against the values of the object.
82
82
  * @returns {T | undefined} - The first property value that matches the partial value, or `undefined` if no match is found.
83
83
  *
@@ -87,12 +87,12 @@ declare function find<T extends Record<string, unknown>>(object: T, doesMatch: (
87
87
  * const result = find(obj, { name: 'Bob' });
88
88
  * console.log(result); // { id: 2, name: 'Bob' }
89
89
  */
90
- declare function find<T extends Record<string, unknown>>(object: T, doesMatch: Partial<T[keyof T]>): T | undefined;
90
+ declare function find<T extends Record<string, unknown>>(object: T | null | undefined, doesMatch: Partial<T[keyof T]>): T | undefined;
91
91
  /**
92
92
  * Finds the first item in an object that matches a property with a specific value.
93
93
  *
94
94
  * @template T
95
- * @param {readonly T[]} object - The object to search through.
95
+ * @param {T extends Record<string, unknown> ? T | null | undefined: never} object - The object to search through.
96
96
  * @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
97
97
  * @returns {T | undefined} - The first item that has the specified property value, or `undefined` if no match is found.
98
98
  *
@@ -102,12 +102,12 @@ declare function find<T extends Record<string, unknown>>(object: T, doesMatch: P
102
102
  * const result = find(items, ['name', 'Alice']);
103
103
  * console.log(result); // { id: 1, name: 'Alice' }
104
104
  */
105
- declare function find<T extends Record<string, unknown>>(object: T, doesMatchProperty: [keyof T, unknown]): T | undefined;
105
+ declare function find<T extends Record<string, unknown>>(object: T | null | undefined, doesMatchProperty: [keyof T, unknown]): T | undefined;
106
106
  /**
107
107
  * Finds the first item in an object that has a specific property, where the property name is provided as a string.
108
108
  *
109
109
  * @template T
110
- * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
110
+ * @param {T extends Record<string, unknown> ? T | null | undefined: never} object - The object to search through.
111
111
  * @param {string} propertyToCheck - The property name to check.
112
112
  * @returns {T | undefined} - The first property value that has the specified property, or `undefined` if no match is found.
113
113
  *
@@ -117,6 +117,6 @@ declare function find<T extends Record<string, unknown>>(object: T, doesMatchPro
117
117
  * const result = find(obj, 'name');
118
118
  * console.log(result); // { id: 1, name: 'Alice' }
119
119
  */
120
- declare function find<T extends Record<string, unknown>>(object: T, propertyToCheck: string): T | undefined;
120
+ declare function find<T extends Record<string, unknown>>(object: T | null | undefined, propertyToCheck: string): T | undefined;
121
121
 
122
122
  export { find };
@@ -3,10 +3,10 @@ import { matches } from '../predicate/matches.mjs';
3
3
  import { matchesProperty } from '../predicate/matchesProperty.mjs';
4
4
 
5
5
  function find(source, doesMatch) {
6
- let values = source;
7
- if (!Array.isArray(source)) {
8
- values = Object.values(source);
6
+ if (!source) {
7
+ return undefined;
9
8
  }
9
+ const values = Array.isArray(source) ? source : Object.values(source);
10
10
  switch (typeof doesMatch) {
11
11
  case 'function': {
12
12
  if (!Array.isArray(source)) {
@@ -2,8 +2,9 @@
2
2
  * Finds the index of the first item in an array that matches the given predicate function.
3
3
  *
4
4
  * @template T
5
- * @param {T[]} arr - The array to search through.
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 {number} - The index of 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 findIndex<T>(arr: readonly T[], doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): number;
16
+ declare function findIndex<T>(arr: ArrayLike<T> | null | undefined, doesMatch: (item: T, index: number, arr: readonly T[]) => unknown, fromIndex?: number): number;
16
17
  /**
17
18
  * Finds the index of the first item in an array that matches the given partial object.
18
19
  *
19
20
  * @template T
20
- * @param {T[]} arr - The array to search through.
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 {number} - The index of 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 findIndex<T>(arr: readonly T[], doesMatch: (item: T, index: num
27
29
  * const result = findIndex(items, { name: 'Bob' });
28
30
  * console.log(result); // 1
29
31
  */
30
- declare function findIndex<T>(arr: readonly T[], doesMatch: Partial<T>): number;
32
+ declare function findIndex<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>, fromIndex?: number): number;
31
33
  /**
32
34
  * Finds the index of the first item in an array that matches a property with a specific value.
33
35
  *
34
36
  * @template T
35
- * @param {readonly T[]} arr - The array to search through.
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 {number} - The index of 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 findIndex<T>(arr: readonly T[], doesMatch: Partial<T>): number;
42
45
  * const result = findIndex(items, ['name', 'Alice']);
43
46
  * console.log(result); // 0
44
47
  */
45
- declare function findIndex<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): number;
48
+ declare function findIndex<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown], fromIndex?: number): number;
46
49
  /**
47
50
  * Finds the index of the first item in an array that has a specific property, where the property name is provided as a string.
48
51
  *
49
52
  * @template T
50
- * @param {readonly T[]} arr - The array to search through.
53
+ * @param {ArrayLike<T> | null | undefined} arr - The array to search through.
51
54
  * @param {string} propertyToCheck - The property name to check.
55
+ * @param {number} [fromIndex=0] - The index to start the search from, defaults to 0.
52
56
  * @returns {number} - The index of the first item that has the specified property, or `undefined` if no match is found.
53
57
  *
54
58
  * @example
@@ -57,6 +61,6 @@ declare function findIndex<T>(arr: readonly T[], doesMatchProperty: [keyof T, un
57
61
  * const result = findIndex(items, 'name');
58
62
  * console.log(result); // 0
59
63
  */
60
- declare function findIndex<T>(arr: readonly T[], propertyToCheck: string): number;
64
+ declare function findIndex<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: string, fromIndex?: number): number;
61
65
 
62
66
  export { findIndex };
@@ -2,8 +2,9 @@
2
2
  * Finds the index of the first item in an array that matches the given predicate function.
3
3
  *
4
4
  * @template T
5
- * @param {T[]} arr - The array to search through.
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 {number} - The index of 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 findIndex<T>(arr: readonly T[], doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): number;
16
+ declare function findIndex<T>(arr: ArrayLike<T> | null | undefined, doesMatch: (item: T, index: number, arr: readonly T[]) => unknown, fromIndex?: number): number;
16
17
  /**
17
18
  * Finds the index of the first item in an array that matches the given partial object.
18
19
  *
19
20
  * @template T
20
- * @param {T[]} arr - The array to search through.
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 {number} - The index of 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 findIndex<T>(arr: readonly T[], doesMatch: (item: T, index: num
27
29
  * const result = findIndex(items, { name: 'Bob' });
28
30
  * console.log(result); // 1
29
31
  */
30
- declare function findIndex<T>(arr: readonly T[], doesMatch: Partial<T>): number;
32
+ declare function findIndex<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>, fromIndex?: number): number;
31
33
  /**
32
34
  * Finds the index of the first item in an array that matches a property with a specific value.
33
35
  *
34
36
  * @template T
35
- * @param {readonly T[]} arr - The array to search through.
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 {number} - The index of 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 findIndex<T>(arr: readonly T[], doesMatch: Partial<T>): number;
42
45
  * const result = findIndex(items, ['name', 'Alice']);
43
46
  * console.log(result); // 0
44
47
  */
45
- declare function findIndex<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): number;
48
+ declare function findIndex<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown], fromIndex?: number): number;
46
49
  /**
47
50
  * Finds the index of the first item in an array that has a specific property, where the property name is provided as a string.
48
51
  *
49
52
  * @template T
50
- * @param {readonly T[]} arr - The array to search through.
53
+ * @param {ArrayLike<T> | null | undefined} arr - The array to search through.
51
54
  * @param {string} propertyToCheck - The property name to check.
55
+ * @param {number} [fromIndex=0] - The index to start the search from, defaults to 0.
52
56
  * @returns {number} - The index of the first item that has the specified property, or `undefined` if no match is found.
53
57
  *
54
58
  * @example
@@ -57,6 +61,6 @@ declare function findIndex<T>(arr: readonly T[], doesMatchProperty: [keyof T, un
57
61
  * const result = findIndex(items, 'name');
58
62
  * console.log(result); // 0
59
63
  */
60
- declare function findIndex<T>(arr: readonly T[], propertyToCheck: string): number;
64
+ declare function findIndex<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: string, fromIndex?: number): number;
61
65
 
62
66
  export { findIndex };
@@ -2,25 +2,36 @@ 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 findIndex(source, doesMatch) {
5
+ function findIndex(arr, doesMatch, fromIndex = 0) {
6
+ if (!arr) {
7
+ return -1;
8
+ }
9
+ if (fromIndex < 0) {
10
+ fromIndex = Math.max(arr.length + fromIndex, 0);
11
+ }
12
+ const subArray = Array.from(arr).slice(fromIndex);
13
+ let index = -1;
6
14
  switch (typeof doesMatch) {
7
15
  case 'function': {
8
- return source.findIndex(doesMatch);
16
+ index = subArray.findIndex(doesMatch);
17
+ break;
9
18
  }
10
19
  case 'object': {
11
20
  if (Array.isArray(doesMatch) && doesMatch.length === 2) {
12
21
  const key = doesMatch[0];
13
22
  const value = doesMatch[1];
14
- return source.findIndex(matchesProperty(key, value));
23
+ index = subArray.findIndex(matchesProperty(key, value));
15
24
  }
16
25
  else {
17
- return source.findIndex(matches(doesMatch));
26
+ index = subArray.findIndex(matches(doesMatch));
18
27
  }
28
+ break;
19
29
  }
20
30
  case 'string': {
21
- return source.findIndex(property(doesMatch));
31
+ index = subArray.findIndex(property(doesMatch));
22
32
  }
23
33
  }
34
+ return index === -1 ? -1 : index + fromIndex;
24
35
  }
25
36
 
26
37
  export { findIndex };
@@ -2,7 +2,7 @@
2
2
  * Iterates through an array in reverse order and returns the index of the first item that matches the given predicate function.
3
3
  *
4
4
  * @template T
5
- * @param {T[]} arr - The array to search through.
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
7
  * @param {number} [fromIndex=arr.length - 1] - The index to start the search from, defaults to the last index of the array.
8
8
  * @returns {number} - The index of the first item that matches the predicate, or `undefined` if no match is found.
@@ -13,12 +13,12 @@
13
13
  * const result = findLastIndex(items, (item) => item > 3)
14
14
  * console.log(result); // 4
15
15
  */
16
- declare function findLastIndex<T>(arr: readonly T[], doesMatch: (item: T, index: number, arr: readonly T[]) => unknown, fromIndex?: number): number;
16
+ declare function findLastIndex<T>(arr: ArrayLike<T> | null | undefined, doesMatch: (item: T, index: number, arr: readonly T[]) => unknown, fromIndex?: number): number;
17
17
  /**
18
18
  * Finds the index of the first item in an array that matches the given partial object.
19
19
  *
20
20
  * @template T
21
- * @param {readonly T[]} arr - The array to search through.
21
+ * @param {ArrayLike<T> | null | undefined} arr - The array to search through.
22
22
  * @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
23
23
  * @param {number} [fromIndex=arr.length - 1] - The index to start the search from, defaults to the last index of the array.
24
24
  * @returns {number} - The index of the first item that matches the partial object, or `undefined` if no match is found.
@@ -29,12 +29,12 @@ declare function findLastIndex<T>(arr: readonly T[], doesMatch: (item: T, index:
29
29
  * const result = findLastIndex(items, { name: 'Bob' });
30
30
  * console.log(result); // 1
31
31
  */
32
- declare function findLastIndex<T>(arr: readonly T[], doesMatch: Partial<T>, fromIndex?: number): number;
32
+ declare function findLastIndex<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>, fromIndex?: number): number;
33
33
  /**
34
34
  * Finds the index of the first item in an array that matches a property with a specific value.
35
35
  *
36
36
  * @template T
37
- * @param {readonly T[]} arr - The array to search through.
37
+ * @param {ArrayLike<T> | null | undefined} arr - The array to search through.
38
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
39
  * @param {number} [fromIndex=arr.length - 1] - The index to start the search from, defaults to the last index of the array.
40
40
  * @returns {number} - The index of the first item that has the specified property value, or `undefined` if no match is found.
@@ -45,12 +45,12 @@ declare function findLastIndex<T>(arr: readonly T[], doesMatch: Partial<T>, from
45
45
  * const result = findLastIndex(items, ['name', 'Alice']);
46
46
  * console.log(result); // 0
47
47
  */
48
- declare function findLastIndex<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown], fromIndex?: number): number;
48
+ declare function findLastIndex<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown], fromIndex?: number): number;
49
49
  /**
50
50
  * Finds the index of the first item in an array that has a specific property, where the property name is provided as a string.
51
51
  *
52
52
  * @template T
53
- * @param {readonly T[]} arr - The array to search through.
53
+ * @param {ArrayLike<T> | null | undefined} arr - The array to search through.
54
54
  * @param {string} propertyToCheck - The property name to check.
55
55
  * @param {number} [fromIndex=arr.length - 1] - The index to start the search from, defaults to the last index of the array.
56
56
  * @returns {number} - The index of the first item that has the specified property, or `undefined` if no match is found.
@@ -61,6 +61,6 @@ declare function findLastIndex<T>(arr: readonly T[], doesMatchProperty: [keyof T
61
61
  * const result = findLastIndex(items, 'name');
62
62
  * console.log(result); // 1
63
63
  */
64
- declare function findLastIndex<T>(arr: readonly T[], propertyToCheck: string, fromIndex?: number): number;
64
+ declare function findLastIndex<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: string, fromIndex?: number): number;
65
65
 
66
66
  export { findLastIndex };