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
7 changes: 7 additions & 0 deletions src/compat/array/findLastIndex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
});

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

Check warning on line 43 in src/compat/array/findLastIndex.spec.ts

View workflow job for this annotation

GitHub Actions / lint

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

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down Expand Up @@ -107,6 +107,13 @@
expect(findLastIndex(array, x => x === 2, 4.2)).toBe(4);
});

it(`\`findLastIndex\` with \`NaN\` \`fromIndex\` should start from 0`, () => {
// Lodash coerces `NaN` via `toInteger`, so `fromIndex` becomes `0` and the
// search still covers the first element. The existing falsey test uses a
// predicate that never matches index 0, so it does not catch this.
expect(findLastIndex(array, x => x === 1, NaN)).toBe(0);
});

it('should return `-1` when provided `null` or `undefined`', () => {
expect(findLastIndex(null, 'a')).toBe(-1);
expect(findLastIndex(undefined, 'a')).toBe(-1);
Expand Down
3 changes: 3 additions & 0 deletions src/compat/array/findLastIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@
*/
export function findLastIndex<T>(
arr: ArrayLike<T> | null | undefined,
doesMatch: ((item: T, index: number, arr: any) => unknown) | Partial<T> | [keyof T, unknown] | PropertyKey = identity,

Check warning on line 58 in src/compat/array/findLastIndex.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
fromIndex: number = arr ? arr.length - 1 : 0
): number {
if (!arr) {
return -1;
}
if (Number.isNaN(fromIndex)) {
fromIndex = 0;
}
if (fromIndex < 0) {
fromIndex = Math.max(arr.length + fromIndex, 0);
} else {
Expand Down