Skip to content

Commit 6f66fb4

Browse files
committed
feat(lastIndexOf): add lastIndexOf function
(cherry picked from commit 6013891)
1 parent d3e010b commit 6f66fb4

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

index.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
isArray,
2929
isArrayLike,
3030
last,
31+
lastIndexOf,
3132
map,
3233
maximum,
3334
minimum,
@@ -268,6 +269,10 @@ test("contains", t => {
268269
t.false(contains([1, 2, 3], 0));
269270
});
270271

272+
test("lastIndexOf", t => {
273+
t.is(lastIndexOf([1, 2, 3, 4, 3, 2, 1], 3), 4);
274+
});
275+
271276
test("findIndex", t => {
272277
t.is(
273278
findIndex([1, 2, 3, 4, 3, 2, 1], n => n >= 3),

index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,19 @@ export function indexOfFn<T>(value: T): (array: ArrayLike<T>) => number | null {
519519
return array => indexOf(array, value);
520520
}
521521

522+
export function lastIndexOf<T>(array: ArrayLike<T>, value: T): number | null {
523+
for (let i = array.length - 1; i >= 0; --i) {
524+
if (array[i] === value) {
525+
return i;
526+
}
527+
}
528+
return null;
529+
}
530+
531+
export function lastIndexOfFn<T>(value: T): (array: ArrayLike<T>) => number | null {
532+
return array => lastIndexOf(array, value);
533+
}
534+
522535
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
523536
// @ts-ignore duplicate identifier: This is the exported declaration, the implementation is below.
524537
export function findIndex<T>(

0 commit comments

Comments
 (0)