UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

43 lines (41 loc) 897 B
import { dfdlT } from "@monstermann/dfdl"; //#region src/number/orElse.ts /** * `orElse(target, orElse)` * * Returns the numeric value of `target` if it's a finite number, otherwise calls the `orElse` function with the original value and returns its result. * * ```ts * orElse(42, () => 0); // 42 * orElse(NaN, () => 0); // 0 * orElse(Infinity, (val) => `Not finite: ${val}`); // "Not finite: Infinity" * orElse("hello", (val) => val.length); // 5 * ``` * * ```ts * pipe( * 42, * orElse(() => 0), * ); // 42 * * pipe( * NaN, * orElse(() => 0), * ); // 0 * * pipe( * Infinity, * orElse((val) => `Not finite: ${val}`), * ); // "Not finite: Infinity" * * pipe( * "hello", * orElse((val) => val.length), * ); // 5 * ``` */ const orElse = dfdlT((target, orElse$1) => { return Number.isFinite(target) ? target : orElse$1(target); }, 2); //#endregion export { orElse };