Files
30-seconds-of-code/snippets/when.md
2018-04-18 20:45:32 -04:00

407 B

when

Tests a value, x, against a predicate function. If true, return fn(x). Else, return x. when(pred, whenTrue) returns another function expecting x for better composability.

const when = (pred, whenTrue) => (x) => pred(x) ? whenTrue(x) : x;
const doubleEvenNumbers = when(
  (x) => x % 2 === 0,
  (x) => x * 2
);

doubleEvenNumbers(2) // 4
doubleEvenNumbers(1) // 1