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
8 changes: 8 additions & 0 deletions src/compat/array/lastIndexOf.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@

it(`should coerce \`fromIndex\` to an integer`, () => {
expect(lastIndexOf(array, 2, 4.2)).toBe(4);
expect(lastIndexOf(array, 1, '-1' as any)).toBe(3);

Check warning on line 51 in src/compat/array/lastIndexOf.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
});

it(`should coerce null and boolean \`fromIndex\` values to integers`, () => {
expect(lastIndexOf(array, 1, null as any)).toBe(0);

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

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
expect(lastIndexOf(array, 2, null as any)).toBe(-1);

Check warning on line 56 in src/compat/array/lastIndexOf.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
expect(lastIndexOf(array, 1, false as any)).toBe(0);

Check warning on line 57 in src/compat/array/lastIndexOf.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
expect(lastIndexOf(array, 1, true)).toBe(0);
});

it(`should return -1 for empty array or nullish values`, () => {
Expand Down
5 changes: 3 additions & 2 deletions src/compat/array/lastIndexOf.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isArrayLike } from '../predicate/isArrayLike.ts';
import { toInteger } from '../util/toInteger.ts';

/**
* Gets the index at which the last occurrence of value is found in array.
Expand Down Expand Up @@ -36,8 +37,8 @@ export function lastIndexOf<T>(

const length = array.length;

let index = (fromIndex as number) ?? length - 1;
if (fromIndex != null) {
let index = fromIndex === undefined ? length - 1 : toInteger(fromIndex);
if (fromIndex !== undefined) {
index = index < 0 ? Math.max(length + index, 0) : Math.min(index, length - 1);
}

Expand Down