@monstermann/fn
Version:
A utility library for TypeScript.
31 lines (29 loc) • 775 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
//#region src/array/findLastIndexOr.ts
/**
* `findLastIndexOr(target, predicate, or)`
*
* Returns the index of the last element in `target` that satisfies the provided `predicate` function. If no element satisfies the predicate, returns `or`.
*
* ```ts
* findLastIndexOr([1, 3, 2, 4], (x) => x > 2, -1); // 3
* findLastIndexOr([1, 2, 3, 4], (x) => x > 5, -1); // -1
* ```
*
* ```ts
* pipe(
* [1, 3, 2, 4],
* findLastIndexOr((x) => x > 2, -1),
* ); // 3
* pipe(
* [1, 2, 3, 4],
* findLastIndexOr((x) => x > 5, -1),
* ); // -1
* ```
*/
const findLastIndexOr = dfdlT((target, predicate, or) => {
const idx = target.findLastIndex(predicate);
return idx < 0 ? or : idx;
}, 3);
//#endregion
export { findLastIndexOr };