@monstermann/fn
Version:
A utility library for TypeScript.
32 lines (30 loc) • 760 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
//#region src/option/mapOr.ts
/**
* `mapOr(target, map, or)`
*
* Applies a mapping function to a value if it's not `null` or `undefined`, otherwise returns a default value. If the target value is `null` or `undefined`, it returns the `or` value. Otherwise, it applies the mapping function and returns the result.
*
* ```ts
* mapOr(5, (x) => x * 2, 0); // 10
* mapOr(null, (x) => x * 2, 0); // 0
* mapOr(undefined, (x) => x * 2, -1); // -1
* ```
*
* ```ts
* pipe(
* 5,
* mapOr((x) => x * 2, 0),
* ); // 10
* pipe(
* null,
* mapOr((x) => x * 2, 0),
* ); // 0
* ```
*/
const mapOr = dfdlT((target, map, or) => {
if (target == null) return or;
return map(target);
}, 3);
//#endregion
export { mapOr };