@monstermann/fn
Version:
A utility library for TypeScript.
32 lines (30 loc) • 707 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
//#region src/option/orElse.ts
/**
* `orElse(target, orElse)`
*
* Returns the target value if it's not `null` or `undefined`, otherwise calls a fallback function and returns its result. The fallback function is only executed when the target is null or undefined.
*
* ```ts
* orElse(5, () => 10); // 5
* orElse(null, () => 10); // 10
* orElse(undefined, Math.random); // calls Math.random()
* orElse(0, () => 10); // 0
* ```
*
* ```ts
* pipe(
* 5,
* orElse(() => 10),
* ); // 5
* pipe(
* null,
* orElse(() => 10),
* ); // 10
* ```
*/
const orElse = dfdlT((target, orElse$1) => {
return target ?? orElse$1();
}, 2);
//#endregion
export { orElse };