@klodianimeri/pipejs
Version:
Pipe functions that provide convenient and efficient ways to work with iterators.
22 lines (18 loc) • 701 B
text/typescript
import { Pipe } from "../pipe.js";
import { Yield } from "../util/index.js";
export function findLastIndex(predicate: (element: any, index?: number) => boolean, fromIndex?: number): Pipe {
fromIndex = typeof fromIndex === 'number' ? fromIndex : 0;
return () => {
let i: number = -1;
let lastIndex: number = -1;
return (result: IteratorResult<any>): IteratorResult<any> | Array<IteratorResult<any>> => {
if (result?.done) {
return [Yield(lastIndex), result];
}
++i;
if (i >= fromIndex && predicate(result.value, i)) {
lastIndex = i;
}
};
}
}