es-toolkit 1.15.1-dev.426 → 1.15.1-dev.428

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.
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Finds the first item in an array that matches the given predicate function.
3
+ *
4
+ * @template T
5
+ * @param {T[]} 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
+ * @returns {T | undefined} - The first item that matches the predicate, or `undefined` if no match is found.
8
+ *
9
+ * @example
10
+ * // Using a predicate function
11
+ * const items = [1, 2, 3, 4, 5];
12
+ * const result = find(items, (item) => item > 3);
13
+ * console.log(result); // 4
14
+ */
15
+ declare function find<T>(arr: readonly T[], doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): T | undefined;
16
+ /**
17
+ * Finds the first item in an array that matches the given partial object.
18
+ *
19
+ * @template T
20
+ * @param {readonly T[]} arr - The array to search through.
21
+ * @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
22
+ * @returns {T | undefined} - The first item that matches the partial object, or `undefined` if no match is found.
23
+ *
24
+ * @example
25
+ * // Using a partial object
26
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
27
+ * const result = find(items, { name: 'Bob' });
28
+ * console.log(result); // { id: 2, name: 'Bob' }
29
+ */
30
+ declare function find<T>(arr: readonly T[], doesMatch: Partial<T>): T | undefined;
31
+ /**
32
+ * Finds the first item in an array that matches a property with a specific value.
33
+ *
34
+ * @template T
35
+ * @param {readonly T[]} arr - The array to search through.
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
+ * @returns {T | undefined} - The first item that has the specified property value, or `undefined` if no match is found.
38
+ *
39
+ * @example
40
+ * // Using a property-value pair
41
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
42
+ * const result = find(items, ['name', 'Alice']);
43
+ * console.log(result); // { id: 1, name: 'Alice' }
44
+ */
45
+ declare function find<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): T | undefined;
46
+ /**
47
+ * Finds the first item in an array that has a specific property, where the property name is provided as a string.
48
+ *
49
+ * @template T
50
+ * @param {readonly T[]} arr - The array to search through.
51
+ * @param {string} propertyToCheck - The property name to check.
52
+ * @returns {T | undefined} - The first item that has the specified property, or `undefined` if no match is found.
53
+ *
54
+ * @example
55
+ * // Using a property name
56
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
57
+ * const result = find(items, 'name');
58
+ * console.log(result); // { id: 1, name: 'Alice' }
59
+ */
60
+ declare function find<T>(arr: readonly T[], propertyToCheck: string): T | undefined;
61
+ /**
62
+ * Finds the first item in an object that matches the given predicate function.
63
+ *
64
+ * @template T
65
+ * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
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
+ * @returns {T | undefined} - The first property value that matches the predicate, or `undefined` if no match is found.
68
+ *
69
+ * @example
70
+ * // Using a predicate function
71
+ * const obj = { a: 1, b: 2, c: 3 };
72
+ * const result = find(obj, (item) => item > 2);
73
+ * console.log(result); // 3
74
+ */
75
+ declare function find<T extends Record<string, unknown>>(object: T, doesMatch: (item: T[keyof T], index: number, object: T) => unknown): T | undefined;
76
+ /**
77
+ * Finds the first item in an object that matches the given partial value.
78
+ *
79
+ * @template T
80
+ * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
81
+ * @param {Partial<T[keyof T]>} doesMatch - A partial value to match against the values of the object.
82
+ * @returns {T | undefined} - The first property value that matches the partial value, or `undefined` if no match is found.
83
+ *
84
+ * @example
85
+ * // Using a partial value
86
+ * const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } };
87
+ * const result = find(obj, { name: 'Bob' });
88
+ * console.log(result); // { id: 2, name: 'Bob' }
89
+ */
90
+ declare function find<T extends Record<string, unknown>>(object: T, doesMatch: Partial<T[keyof T]>): T | undefined;
91
+ /**
92
+ * Finds the first item in an object that matches a property with a specific value.
93
+ *
94
+ * @template T
95
+ * @param {readonly T[]} object - The object to search through.
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
+ * @returns {T | undefined} - The first item that has the specified property value, or `undefined` if no match is found.
98
+ *
99
+ * @example
100
+ * // Using a property-value pair
101
+ * const items = { alice: { id: 1, name: 'Alice' }, bob: { id: 2, name: 'Bob' } };
102
+ * const result = find(items, ['name', 'Alice']);
103
+ * console.log(result); // { id: 1, name: 'Alice' }
104
+ */
105
+ declare function find<T extends Record<string, unknown>>(object: T, doesMatchProperty: [keyof T, unknown]): T | undefined;
106
+ /**
107
+ * Finds the first item in an object that has a specific property, where the property name is provided as a string.
108
+ *
109
+ * @template T
110
+ * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
111
+ * @param {string} propertyToCheck - The property name to check.
112
+ * @returns {T | undefined} - The first property value that has the specified property, or `undefined` if no match is found.
113
+ *
114
+ * @example
115
+ * // Using a property name
116
+ * const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } };
117
+ * const result = find(obj, 'name');
118
+ * console.log(result); // { id: 1, name: 'Alice' }
119
+ */
120
+ declare function find<T extends Record<string, unknown>>(object: T, propertyToCheck: string): T | undefined;
121
+
122
+ export { find };
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Finds the first item in an array that matches the given predicate function.
3
+ *
4
+ * @template T
5
+ * @param {T[]} 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
+ * @returns {T | undefined} - The first item that matches the predicate, or `undefined` if no match is found.
8
+ *
9
+ * @example
10
+ * // Using a predicate function
11
+ * const items = [1, 2, 3, 4, 5];
12
+ * const result = find(items, (item) => item > 3);
13
+ * console.log(result); // 4
14
+ */
15
+ declare function find<T>(arr: readonly T[], doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): T | undefined;
16
+ /**
17
+ * Finds the first item in an array that matches the given partial object.
18
+ *
19
+ * @template T
20
+ * @param {readonly T[]} arr - The array to search through.
21
+ * @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
22
+ * @returns {T | undefined} - The first item that matches the partial object, or `undefined` if no match is found.
23
+ *
24
+ * @example
25
+ * // Using a partial object
26
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
27
+ * const result = find(items, { name: 'Bob' });
28
+ * console.log(result); // { id: 2, name: 'Bob' }
29
+ */
30
+ declare function find<T>(arr: readonly T[], doesMatch: Partial<T>): T | undefined;
31
+ /**
32
+ * Finds the first item in an array that matches a property with a specific value.
33
+ *
34
+ * @template T
35
+ * @param {readonly T[]} arr - The array to search through.
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
+ * @returns {T | undefined} - The first item that has the specified property value, or `undefined` if no match is found.
38
+ *
39
+ * @example
40
+ * // Using a property-value pair
41
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
42
+ * const result = find(items, ['name', 'Alice']);
43
+ * console.log(result); // { id: 1, name: 'Alice' }
44
+ */
45
+ declare function find<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): T | undefined;
46
+ /**
47
+ * Finds the first item in an array that has a specific property, where the property name is provided as a string.
48
+ *
49
+ * @template T
50
+ * @param {readonly T[]} arr - The array to search through.
51
+ * @param {string} propertyToCheck - The property name to check.
52
+ * @returns {T | undefined} - The first item that has the specified property, or `undefined` if no match is found.
53
+ *
54
+ * @example
55
+ * // Using a property name
56
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
57
+ * const result = find(items, 'name');
58
+ * console.log(result); // { id: 1, name: 'Alice' }
59
+ */
60
+ declare function find<T>(arr: readonly T[], propertyToCheck: string): T | undefined;
61
+ /**
62
+ * Finds the first item in an object that matches the given predicate function.
63
+ *
64
+ * @template T
65
+ * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
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
+ * @returns {T | undefined} - The first property value that matches the predicate, or `undefined` if no match is found.
68
+ *
69
+ * @example
70
+ * // Using a predicate function
71
+ * const obj = { a: 1, b: 2, c: 3 };
72
+ * const result = find(obj, (item) => item > 2);
73
+ * console.log(result); // 3
74
+ */
75
+ declare function find<T extends Record<string, unknown>>(object: T, doesMatch: (item: T[keyof T], index: number, object: T) => unknown): T | undefined;
76
+ /**
77
+ * Finds the first item in an object that matches the given partial value.
78
+ *
79
+ * @template T
80
+ * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
81
+ * @param {Partial<T[keyof T]>} doesMatch - A partial value to match against the values of the object.
82
+ * @returns {T | undefined} - The first property value that matches the partial value, or `undefined` if no match is found.
83
+ *
84
+ * @example
85
+ * // Using a partial value
86
+ * const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } };
87
+ * const result = find(obj, { name: 'Bob' });
88
+ * console.log(result); // { id: 2, name: 'Bob' }
89
+ */
90
+ declare function find<T extends Record<string, unknown>>(object: T, doesMatch: Partial<T[keyof T]>): T | undefined;
91
+ /**
92
+ * Finds the first item in an object that matches a property with a specific value.
93
+ *
94
+ * @template T
95
+ * @param {readonly T[]} object - The object to search through.
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
+ * @returns {T | undefined} - The first item that has the specified property value, or `undefined` if no match is found.
98
+ *
99
+ * @example
100
+ * // Using a property-value pair
101
+ * const items = { alice: { id: 1, name: 'Alice' }, bob: { id: 2, name: 'Bob' } };
102
+ * const result = find(items, ['name', 'Alice']);
103
+ * console.log(result); // { id: 1, name: 'Alice' }
104
+ */
105
+ declare function find<T extends Record<string, unknown>>(object: T, doesMatchProperty: [keyof T, unknown]): T | undefined;
106
+ /**
107
+ * Finds the first item in an object that has a specific property, where the property name is provided as a string.
108
+ *
109
+ * @template T
110
+ * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
111
+ * @param {string} propertyToCheck - The property name to check.
112
+ * @returns {T | undefined} - The first property value that has the specified property, or `undefined` if no match is found.
113
+ *
114
+ * @example
115
+ * // Using a property name
116
+ * const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } };
117
+ * const result = find(obj, 'name');
118
+ * console.log(result); // { id: 1, name: 'Alice' }
119
+ */
120
+ declare function find<T extends Record<string, unknown>>(object: T, propertyToCheck: string): T | undefined;
121
+
122
+ export { find };
@@ -0,0 +1,42 @@
1
+ import { property } from '../object/property.mjs';
2
+ import { matches } from '../predicate/matches.mjs';
3
+ import { matchesProperty } from '../predicate/matchesProperty.mjs';
4
+
5
+ function find(source, doesMatch) {
6
+ let values = source;
7
+ if (!Array.isArray(source)) {
8
+ values = Object.values(source);
9
+ }
10
+ switch (typeof doesMatch) {
11
+ case 'function': {
12
+ if (!Array.isArray(source)) {
13
+ const entries = Object.entries(source);
14
+ for (let i = 0; i < entries.length; i++) {
15
+ const entry = entries[i];
16
+ const key = entry[0];
17
+ const value = entry[1];
18
+ if (doesMatch(value, key, source)) {
19
+ return value;
20
+ }
21
+ }
22
+ return undefined;
23
+ }
24
+ return values.find(doesMatch);
25
+ }
26
+ case 'object': {
27
+ if (Array.isArray(doesMatch) && doesMatch.length === 2) {
28
+ const key = doesMatch[0];
29
+ const value = doesMatch[1];
30
+ return values.find(matchesProperty(key, value));
31
+ }
32
+ else {
33
+ return values.find(matches(doesMatch));
34
+ }
35
+ }
36
+ case 'string': {
37
+ return values.find(property(doesMatch));
38
+ }
39
+ }
40
+ }
41
+
42
+ export { find };
@@ -96,6 +96,7 @@ export { chunk } from './array/chunk.mjs';
96
96
  export { concat } from './array/concat.mjs';
97
97
  export { difference } from './array/difference.mjs';
98
98
  export { fill } from './array/fill.mjs';
99
+ export { find } from './array/find.mjs';
99
100
  export { flatten } from './array/flatten.mjs';
100
101
  export { flattenDeep } from './array/flattenDeep.mjs';
101
102
  export { flattenDepth } from './array/flattenDepth.mjs';
@@ -96,6 +96,7 @@ export { chunk } from './array/chunk.js';
96
96
  export { concat } from './array/concat.js';
97
97
  export { difference } from './array/difference.js';
98
98
  export { fill } from './array/fill.js';
99
+ export { find } from './array/find.js';
99
100
  export { flatten } from './array/flatten.js';
100
101
  export { flattenDeep } from './array/flattenDeep.js';
101
102
  export { flattenDepth } from './array/flattenDepth.js';