@monstermann/fn
Version:
A utility library for TypeScript.
25 lines (23 loc) • 525 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
//#region src/array/findOr.ts
/**
* `findOr(array, predicate, fallback)`
*
* Returns the first element in `array` that satisfies the provided `predicate` function, or `fallback` if no element is found.
*
* ```ts
* findOr([1, 2, 3, 4], (x) => x > 10, 0); // 0
* ```
*
* ```ts
* pipe(
* [1, 2, 3, 4],
* findOr((x) => x > 10, 0),
* ); // 0
* ```
*/
const findOr = dfdlT((target, predicate, or) => {
return target.find(predicate) ?? or;
}, 3);
//#endregion
export { findOr };