Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/compat/array/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
* Finds the first item in an object that has a specific property, where the property name is provided as a PropertyKey.
*
* @template T
* @param source - The source array or object to search through.
* @param collection - The source array or object to search through.
* @param [doesMatch=identity] - The criteria to match. It can be a function, a partial object, a key-value pair, or a property name.
* @param [fromIndex=0] - The index to start the search from, defaults to 0.
* @returns The first property value that has the specified property, or `undefined` if no match is found.
Expand All @@ -103,36 +103,36 @@
* console.log(result); // { id: 1, name: 'Alice' }
*/
export function find<T>(
source: ArrayLike<T> | Record<any, any> | null | undefined,
collection: ArrayLike<T> | Record<any, any> | null | undefined,

Check warning on line 106 in src/compat/array/find.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 106 in src/compat/array/find.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
_doesMatch:
| ((item: T, index: number, arr: any) => unknown)

Check warning on line 108 in src/compat/array/find.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
| Partial<T>
| [keyof T, unknown]
| PropertyKey = identity,
fromIndex = 0
): T | undefined {
if (!source) {
if (!collection) {
return undefined;
}
if (fromIndex < 0) {
fromIndex = Math.max(source.length + fromIndex, 0);
fromIndex = Math.max(collection.length + fromIndex, 0);
}

const doesMatch = iteratee(_doesMatch);
if (!Array.isArray(source)) {
const keys = Object.keys(source) as Array<keyof T>;
if (!Array.isArray(collection)) {
const keys = Object.keys(collection) as Array<keyof T>;

for (let i = fromIndex; i < keys.length; i++) {
const key = keys[i];
const value = source[key] as T;
const value = collection[key] as T;

if (doesMatch(value, key as number, source)) {
if (doesMatch(value, key as number, collection)) {
return value;
}
}

return undefined;
}

return source.slice(fromIndex).find(doesMatch);
return collection.slice(fromIndex).find(doesMatch);
}