Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion benchmarks/bundle-size/find.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ describe('find bundle size', () => {

it('es-toolkit/compat', async () => {
const bundleSize = await getBundleSize('es-toolkit/compat', 'find');
expect(bundleSize).toMatchInlineSnapshot(`8295`);
expect(bundleSize).toMatchInlineSnapshot(`8555`);
});
});
8 changes: 8 additions & 0 deletions src/compat/array/find.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
});

it(`find should provide correct \`predicate\` arguments for arrays`, () => {
let args: any;

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

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const array = ['a'];

find(array, function () {
Expand All @@ -70,7 +70,7 @@
});

it(`find should provide correct \`predicate\` arguments for objects`, () => {
let args: any;

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

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const object = { a: 1 };

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand All @@ -92,6 +92,14 @@
expect(find(objects, { b: 2 }, 4)).toBe(undefined);
});

it('find should coerce `fromIndex` to an integer', () => {
const array = [1, 2, 3, 1, 2, 3];

expect(find(array, x => x === 1, 2.7)).toBe(1);
expect(find(array, x => x === 1, -2.7)).toBe(undefined);
expect(find(array, x => x === 1, -0.5)).toBe(1);
});

it('should return `undefined` when provided `null` or `undefined`', () => {
expect(find(null, 'a')).toBe(undefined);
expect(find(undefined, 'a')).toBe(undefined);
Expand Down
3 changes: 3 additions & 0 deletions src/compat/array/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { ObjectIterateeCustom } from '../_internal/ObjectIteratee.ts';
import { ObjectIteratorTypeGuard } from '../_internal/ObjectIterator.ts';
import { iteratee } from '../util/iteratee.ts';
import { toInteger } from '../util/toInteger.ts';

/**
* Finds the first element in an array-like object that matches a type guard predicate.
Expand Down Expand Up @@ -103,9 +104,9 @@
* console.log(result); // { id: 1, name: 'Alice' }
*/
export function find<T>(
source: ArrayLike<T> | Record<any, any> | null | undefined,

Check warning on line 107 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 107 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 109 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,
Expand All @@ -114,6 +115,8 @@
if (!source) {
return undefined;
}

fromIndex = toInteger(fromIndex);
if (fromIndex < 0) {
fromIndex = Math.max(source.length + fromIndex, 0);
}
Expand Down