Skip to content

Commit

Permalink
feat(ch18): add a pipeline for composable custom data types
Browse files Browse the repository at this point in the history
  • Loading branch information
poulzinho committed Mar 7, 2020
1 parent 096d9be commit 7edf1a9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/ch-18_composable_data_types/18.1_composable_data_types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const t = value => {
const add = n => t(value + n);

return Object.assign(add, {
toString: () => `t(${value})`,
valueOf: () => value,
})
};
23 changes: 14 additions & 9 deletions src/ch-18_composable_data_types/18.1_composable_data_types.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import {expect} from 'chai';
import {pipe} from "../ch-10_abstraction-and-composition/10.1_abstraction_and_composition";
import {t} from './18.1_composable_data_types';

describe("Composable custom Data Types", () => {
it("should describe a factory that returns instances (functions) of numerical data types", () => {
const t = value => {
const add = n => t(value + n);

return Object.assign(add, {
toString: () => `t(${value})`,
valueOf: () => value,
})
};

const [x, y, z] = [1, 2, 3];

// Identity
Expand All @@ -22,4 +15,16 @@ describe("Composable custom Data Types", () => {
expect(t(x)(t(y)(t(z))).valueOf()).equal(6);
expect(t(x)(t(y))(t(z)).valueOf()).equal(t(x)(t(y)(t(z))).valueOf());
});

it("should use a pipeline with an initial value", () => {
const add = (...fns) => pipe(...fns)(t(0));

const sum = add(
t(4),
t(4),
t(-1),
);

expect(sum.valueOf()).equal(7);
});
});

0 comments on commit 7edf1a9

Please sign in to comment.