Skip to content

Commit

Permalink
feat: Update "when" to pipe when passing array of functions. Add uncu…
Browse files Browse the repository at this point in the history
…rried 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)
  • Loading branch information
andreidmt committed Oct 1, 2020
1 parent 3e58452 commit 3010b42
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
28 changes: 21 additions & 7 deletions src/when/when.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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)
}
42 changes: 30 additions & 12 deletions src/when/when.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 3010b42

Please sign in to comment.