@monstermann/fn
Version:
A utility library for TypeScript.
46 lines (44 loc) • 715 B
JavaScript
import { dfdlT } from "@monstermann/dfdl";
//#region src/function/when.ts
/**
* `when(predicate, onTrue)`
*
* Conditionally transforms a value when the predicate is true, otherwise returns the original value.
*
* ```ts
* when(
* 5,
* (x) => x > 3,
* (x) => x * 2,
* ); // 10
*
* when(
* 2,
* (x) => x > 3,
* (x) => x * 2,
* ); // 2
* ```
*
* ```ts
* pipe(
* 5,
* when(
* (x) => x > 3,
* (x) => x * 2,
* ),
* ); // 10
*
* pipe(
* 2,
* when(
* (x) => x > 3,
* (x) => x * 2,
* ),
* ); // 2
* ```
*/
const when = dfdlT((value, predicate, onTrue) => {
return predicate(value) ? onTrue(value) : value;
}, 3);
//#endregion
export { when };