1
0
Fork 0
functional/composition.test.ts

31 lines
839 B
TypeScript

import { identity, nop, partial, pipe } from "./composition.ts";
import { expect, it } from "./deps.testing.ts";
it("nop", () => {
expect(nop()).toBeUndefined();
});
it("identity", () => {
expect(identity(5)).toBe(5);
expect(identity(null)).toBe(null);
});
it("partial", () => {
const func1 = (conf: { a: number; b: string; c: number }) =>
`${conf.a}${conf.b}${conf.c}`;
const pfunc1 = partial({ b: "test" }, func1);
expect(pfunc1({ a: 5, c: 8 })).toEqual("5test8");
const func2 = (conf: { a: number; b: string }, c: number) =>
`${conf.a}${conf.b}${c}`;
const pfunc2 = partial({ b: "test" }, func2);
expect(pfunc2({ a: 2 }, 3)).toEqual("2test3");
});
it("pipe", () => {
const f = pipe((x: number) => x * 2, (x: number) => x + 1);
expect(f(1)).toBe(3);
expect(f(2)).toBe(5);
expect(f(3)).toBe(7);
});