Skip to content

Commit

Permalink
feat: when: allow one or more x to be given to f and g
Browse files Browse the repository at this point in the history
BREAKING CHANGE: when(pred)(...x): predicate can return logical true. see #29
  • Loading branch information
customcommander committed Jan 1, 2022
1 parent d3bd940 commit a571489
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/when.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

const curry = require('./curry');
const T = require('./T');

/**
* @summary
Expand All @@ -22,4 +23,7 @@ const curry = require('./curry');
* @param {?} x
* @returns {?}
*/
module.exports = curry((f, g, x) => f(x) === true ? g(x) : undefined);
module.exports = curry(function (f, g, x /* <- x is needed to maintain the function arity */) {
const [/*f*/,/*g*/,...xs] = Array.from(arguments);
return T(f(...xs)) ? g(...xs) : undefined;
});
47 changes: 36 additions & 11 deletions test/when.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,49 @@ const td = require('testdouble');
const test = require('tape');
const {when: sut} = require('..');

test('when returns `g(x)` if `f(x)` returns `true`', t => {
test('when(f)(g)(...x)', t => {
// parameters symbols
const a = Symbol();
const b = Symbol();
const c = Symbol();
const d = Symbol();

// return symbol
const z = Symbol();

const f = td.func();
const g = td.func();

td.when(f(1)).thenReturn(true);
td.when(f(2)).thenReturn(false);
td.when(f(3)).thenReturn('truthy');
// logical true
td.when(f(a, a)).thenReturn(true);
td.when(f(a, b)).thenReturn(NaN);
td.when(f(a, c)).thenReturn(0);
td.when(f(a, d)).thenReturn('');

// logical false
td.when(f(b, b)).thenReturn(false);
td.when(f(b, c)).thenReturn(null);
td.when(f(b, d)).thenReturn(undefined);

td.when(g(a, a)).thenReturn(z);
td.when(g(a, b)).thenReturn(z);
td.when(g(a, c)).thenReturn(z);
td.when(g(a, d)).thenReturn(z);

td.when(g(1)).thenReturn('OK');
t.same(sut(f)(g)(a, a), z, 'Returns `g(a, a)` as `f(a, a)` is logical true (`true`).');
t.same(sut(f)(g)(a, b), z, 'Returns `g(a, b)` as `f(a, b)` is logical true (`NaN`).');
t.same(sut(f)(g)(a, c), z, 'Returns `g(a, c)` as `f(a, c)` is logical true (`0`).');
t.same(sut(f)(g)(a, d), z, 'Returns `g(a, d)` as `f(a, d)` is logical true (``).');

t.same(sut(f)(g)(1), 'OK',
'expected `g(x)` because f(x) returned `true`.');
t.same(sut(f)(g)(b, b), undefined, 'Returns `undefined` as `f(b, b)` is logical false (`false`).');
t.same(sut(f)(g)(b, c), undefined, 'Returns `undefined` as `f(b, c)` is logical false (`null`).');
t.same(sut(f)(g)(b, d), undefined, 'Returns `undefined` as `f(b, d)` is logical false (`undefined`).');

t.same(sut(f)(g)(2), undefined,
'expected `undefined` because `f(x)` returned `false`.');
td.verify(g(b, b), {times: 0, ignoreExtraArgs: true});
td.verify(g(b, c), {times: 0, ignoreExtraArgs: true});
td.verify(g(b, d), {times: 0, ignoreExtraArgs: true});

t.same(sut(f)(g)(3), undefined,
'expected `undefined` because `f(x)` returned truthy not `true`.');
t.same(sut(f, g, a, a), z, 'Can supply one or more `x` immediately.');

t.end();
});

0 comments on commit a571489

Please sign in to comment.