Skip to content

Commit

Permalink
feat(ch16): enable explicit dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
poulzinho committed Feb 29, 2020
1 parent 677fe0b commit 99bfe18
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/ch-16_functional-mixins/16.1_functional-mixins.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ export const flying = obj => {
}
});
};

export const withLogging = logger => obj => Object.assign({}, obj, {
log(text) {
logger(text)
}
});
36 changes: 29 additions & 7 deletions src/ch-16_functional-mixins/16.1_functional-mixins.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from 'chai';
import {flying} from "./16.1_functional-mixins";
import {flying, withLogging} from "./16.1_functional-mixins";
import {pipe} from "../ch-10_abstraction-and-composition/10.1_abstraction_and_composition";

describe("Functional Mixins", () => {
Expand Down Expand Up @@ -75,12 +75,7 @@ describe("Functional Mixins", () => {
});

it("should enable implicit dependencies", () => {
const withLogging = logger => obj => Object.assign({}, obj, {
log(text) {
logger(text)
}
});

// see here the default value log
const withConfig = config => (obj = {
log: (text = "") => console.log(text)
}) => Object.assign({}, obj, {
Expand All @@ -106,7 +101,34 @@ describe("Functional Mixins", () => {

expect(config.get("host")).equal("localhost");
expect(config.get("notThere")).equal(undefined);
});

it("should enable explicit dependencies", () => {
const addConfig = config => obj => Object.assign({}, obj, {
get(key) {
return config[key] === undefined
? this.log(`Missing config key: ${key}`)
: config[key]
}
});

const withConfig = ({initialConfig, logger}) => obj => pipe(
withLogging(logger),
addConfig(initialConfig)
)(obj);

const createConfig = ({initialConfig, logger}) => withConfig({initialConfig, logger})({});

const initialConfig = {
host: "localhost"
};

const logger = console.log.bind(console);

const config = createConfig({initialConfig, logger});

expect(config.get("host")).equal("localhost");
expect(config.get("unknown")).equal(undefined);
});

});

0 comments on commit 99bfe18

Please sign in to comment.