Skip to content

Commit

Permalink
feat(ch01): add object concatenation aka mixin composition
Browse files Browse the repository at this point in the history
  • Loading branch information
poulzinho committed Dec 26, 2019
1 parent 892a5a5 commit 16b56dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/ch-01_intro/1.1_function-composition.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ export const doStuffAsync = (x, time) => new Promise((resolve) => {
});

export const doStuffBetter = x => multiplyBy2(add1(x));

export const mixin = (...objects) => {
return objects.reduce((mixedObj, object) => ({...mixedObj, ...object}), {});
};
16 changes: 15 additions & 1 deletion src/ch-01_intro/1.1_function-composition.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {assert, expect} from "chai";
import {spy} from 'sinon';
import {add1, doStuff, doStuffAsync, doStuffBetter, multiplyBy2, wait} from './1.1_function-composition';
import {add1, doStuff, doStuffAsync, doStuffBetter, mixin, multiplyBy2, wait} from './1.1_function-composition';

describe("Function Composition, apply a function to the output of another function", () => {
before(() => {
Expand Down Expand Up @@ -55,3 +55,17 @@ describe("Function Composition, apply a function to the output of another functi
assert(console.log.calledWith('afterMultiplyBy2: 42'), 'It is not afterMultiplyBy2: 42');
})
});

describe("Object Composition", () => {
it("should allow object concatenation aka mixin composition", () => {
const a = {
a: "a"
};

const b = {
b: "b"
};

expect(mixin(a, b)).to.deep.equal({a: "a", b: "b"});
});
});

0 comments on commit 16b56dc

Please sign in to comment.