UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

53 lines (51 loc) 1.14 kB
import { NonNil } from "../internals/types.js"; //#region src/option/mapOrElse.d.ts /** * `mapOrElse(target, map, orElse)` * * Applies a mapping function to a value if it's not `null` or `undefined`, otherwise calls a fallback function. If the target value is `null` or `undefined`, it calls the `orElse` function and returns its result. Otherwise, it applies the mapping function and returns the result. * * ```ts * mapOrElse( * 5, * (x) => x * 2, * () => 0, * ); // 10 * * mapOrElse( * null, * (x) => x * 2, * () => 0, * ); // 0 * * mapOrElse( * undefined, * (x) => x * 2, * () => -1, * ); // -1 * ``` * * ```ts * pipe( * 5, * mapOrElse( * (x) => x * 2, * () => 0, * ), * ); // 10 * * pipe( * null, * mapOrElse( * (x) => x * 2, * () => 0, * ), * ); // 0 * ``` */ declare const mapOrElse: { <T, U, V>(map: (value: NoInfer<NonNil<T>>, orElse: () => V) => U): (target: T) => U | V; <T, U, V>(target: T, map: (value: NoInfer<NonNil<T>>) => U, orElse: () => V): U | V; }; //#endregion export { mapOrElse };