UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

31 lines (29 loc) 740 B
import { dfdlT } from "@monstermann/dfdl"; //#region src/array/findIndexOr.ts /** * `findIndexOr(target, predicate, or)` * * Returns the index of the first element in `target` that satisfies the provided `predicate` function. If no element satisfies the predicate, returns `or`. * * ```ts * findIndexOr([1, 2, 3, 4], (x) => x > 2, -1); // 2 * findIndexOr([1, 2, 3, 4], (x) => x > 5, -1); // -1 * ``` * * ```ts * pipe( * [1, 2, 3, 4], * findIndexOr((x) => x > 2, -1), * ); // 2 * pipe( * [1, 2, 3, 4], * findIndexOr((x) => x > 5, -1), * ); // -1 * ``` */ const findIndexOr = dfdlT((target, predicate, or) => { const idx = target.findIndex(predicate); return idx < 0 ? or : idx; }, 3); //#endregion export { findIndexOr };