From 3010b42fbc1f5b6be60c7d44657145d6fb0592b3 Mon Sep 17 00:00:00 2001 From: Andrei Dumitrescu <5057797+andreidcm@users.noreply.github.com> Date: Thu, 1 Oct 2020 14:19:21 +0200 Subject: [PATCH] feat: Update "when" to pipe when passing array of functions. Add uncurried version. BREAKING CHANGE: No longer accepts primitive values for thenFn or elseFn parameters. // old when(isTrue, "val is true", "value is not true", true) // new when(isTrue, () => "val is true", same("value is not true"), true) --- src/when/when.js | 28 +++++++++++++++++++++------- src/when/when.test.js | 42 ++++++++++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/when/when.js b/src/when/when.js index b9e22df..4bf93df 100644 --- a/src/when/when.js +++ b/src/when/when.js @@ -1,4 +1,13 @@ -import { is } from "../is/is" +import { pipe } from "../pipe/pipe" +import { i } from "../i/i" + +const _when = (_ifFn, _thenFn, _elseFn = i, source) => { + const ifFn = Array.isArray(_ifFn) ? pipe(..._ifFn) : _ifFn + const thenFn = Array.isArray(_thenFn) ? pipe(..._thenFn) : _thenFn + const elseFn = Array.isArray(_elseFn) ? pipe(..._elseFn) : _elseFn + + return ifFn(source) ? thenFn(source) : elseFn(source) +} /** * Functional if-then-else @@ -11,7 +20,9 @@ import { is } from "../is/is" * @return {mixed} * * @tag Core + * @signature (ifFn: Function, thenFn: Function, elseFn: Function, source: mixed): mixed * @signature (ifFn: Function, thenFn: Function, elseFn: Function) => (source: mixed): mixed + * @signature (ifFn: Function, thenFn: Function) => (source: mixed): mixed * * @example * when(isEven, increment, decrement)(5) @@ -20,14 +31,17 @@ import { is } from "../is/is" * when(isOdd, increment)(6) * // => 6 */ -export const when = (ifFn, thenFn, elseFn) => source => { - if (ifFn(source)) { - return typeof thenFn === "function" ? thenFn(source) : thenFn +export const when = (...params) => { + // @signature (ifFn, thenFn) => (source) + if (params.length <= 2) { + return source => _when(params[0], params[1], undefined, source) } - if (typeof elseFn === "function") { - return elseFn(source) + // @signature (ifFn, thenFn, elseFn) => (source) + if (params.length <= 3) { + return source => _when(params[0], params[1], params[2], source) } - return is(elseFn) ? elseFn : source + // @signature (ifFn, thenFn, elseFn, source) + return _when(...params) } diff --git a/src/when/when.test.js b/src/when/when.test.js index c92a58a..23a223c 100644 --- a/src/when/when.test.js +++ b/src/when/when.test.js @@ -5,21 +5,15 @@ const isEven = source => source % 2 !== 0 test("when", t => { t.equals( - when(isEven, "even", "odd")(5), - "even", - "Primitive instead of then function" - ) - - t.equals( - when(isEven, "even", "odd")(6), - "odd", - "Primitive instead of else function" + when(isEven, inc, dec)(5), + 6, + 'Increment even input with "then" & "else" defined (curried)' ) t.equals( - when(isEven, inc, dec)(5), + when(isEven, inc, dec, 5), 6, - 'Increment even input with "then" & "else" defined' + 'Increment even input with "then" & "else" defined (uncurried)' ) t.equals( @@ -28,10 +22,34 @@ test("when", t => { 'Decrement odd input with "then" & "else" defined' ) + t.equals( + when(isEven, [inc, inc], dec)(5), + 7, + 'Run "then" as a pipe array of functions' + ) + + t.equals( + when(isEven, inc, [dec, dec])(6), + 4, + 'Run "else" as a pipe array of functions' + ) + + t.equals( + when([inc, isEven], inc, dec)(4), + 5, + 'Run "if" as a pipe array of functions' + ) + t.equals( when(isEven, inc)(5), 6, - 'Increment even input with only "then" defined' + 'Increment even input with only "then" defined (curried)' + ) + + t.equals( + when(isEven, inc)(6), + 6, + 'Return same input when "if" is false with only "then" defined' ) t.equals(