Skip to content

Commit

Permalink
fixes bug in object freddies: branches with no input property were no…
Browse files Browse the repository at this point in the history
…t being invoked
  • Loading branch information
quinnnned committed Sep 10, 2016
1 parent e13fbf2 commit 371eed1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/index.js
Expand Up @@ -29,17 +29,15 @@ export const functionize = (freddy) => {
return composeMap(freddy);
};

export const composeMap = (freddyMap) => (input, ...rest) => {
const results = Object.keys(input).map(
(inputKey) => ({
[inputKey]: functionize(freddyMap[inputKey])(input[inputKey], ...rest)
export const composeMap = (freddyMap={}) => (input={}, ...rest) => (
Object.keys(freddyMap).map(
(key) => ({
[key]: functionize(freddyMap[key])(input[key], ...rest)
})
);

// TODO: Check for thenables

return results.reduce( (fused, shard) => Object.assign(fused, shard), {});
};
).reduce( (fused, shard) => Object.assign(fused, shard),
Object.assign({}, input)
)
);

export const composeList = (freddyList) => {
const [first, ...rest] = freddyList;
Expand Down
33 changes: 33 additions & 0 deletions test/index.js
Expand Up @@ -193,6 +193,39 @@ test('object freddies should pass all function parameters to each branch', (asse
assert.end();
});


test('a branch of an object freddy should be executed, even if no input is supplied to that branch', (assert) => {
// Arrange
const input = {};
const freddy = { a: (x='foo') => x, b: (x=2) => x, c: (x=true) => x };

// Act
const compositeFunction = freddies(freddy);
const actual = compositeFunction(input);
const expected = {a:'foo', b:2, c:true};

// Assert
assert.deepEqual(actual, expected);
assert.end();
});

test('properties of freddy input objects that do not have branches in that freddy are passed through unchanged', (assert) => {
// Arrange
const emptyObject = {};
const input = {emptyObject, aBool: true, aString: 'aString'};
const freddy = {};

// Act
const compositeFunction = freddies(freddy);
const actual = compositeFunction(input);

// Assert
assert.equal(actual.emptyObject, input.emptyObject);
assert.equal(actual.aBool, input.aBool);
assert.equal(actual.aString, input.aString);
assert.end();
});

test('freddies interprets non-function-non-array-non-object arguments as constant functions', (assert) => {
// Arrange
const aConstantValue = "a constant value";
Expand Down

0 comments on commit 371eed1

Please sign in to comment.