functional/composition.test.ts

31 lines
839 B
TypeScript
Raw Normal View History

2020-05-11 22:34:57 +00:00
import { identity, nop, partial, pipe } from "./composition.ts";
2021-08-26 23:01:08 +00:00
import { expect, it } from "./deps.testing.ts";
2020-05-11 22:34:57 +00:00
2020-12-02 20:58:26 +00:00
it("nop", () => {
2020-05-13 09:46:10 +00:00
expect(nop()).toBeUndefined();
2020-05-11 22:34:57 +00:00
});
2020-12-02 20:58:26 +00:00
it("identity", () => {
2020-05-13 09:46:10 +00:00
expect(identity(5)).toBe(5);
expect(identity(null)).toBe(null);
2020-05-11 22:34:57 +00:00
});
2020-12-02 20:58:26 +00:00
it("partial", () => {
2020-05-11 22:34:57 +00:00
const func1 = (conf: { a: number; b: string; c: number }) =>
`${conf.a}${conf.b}${conf.c}`;
const pfunc1 = partial({ b: "test" }, func1);
2020-05-13 09:46:10 +00:00
expect(pfunc1({ a: 5, c: 8 })).toEqual("5test8");
2020-05-11 22:34:57 +00:00
const func2 = (conf: { a: number; b: string }, c: number) =>
`${conf.a}${conf.b}${c}`;
const pfunc2 = partial({ b: "test" }, func2);
2020-05-13 09:46:10 +00:00
expect(pfunc2({ a: 2 }, 3)).toEqual("2test3");
2020-05-11 22:34:57 +00:00
});
2020-12-02 20:58:26 +00:00
it("pipe", () => {
2020-05-11 22:34:57 +00:00
const f = pipe((x: number) => x * 2, (x: number) => x + 1);
2020-05-13 09:46:10 +00:00
expect(f(1)).toBe(3);
expect(f(2)).toBe(5);
expect(f(3)).toBe(7);
2020-05-11 22:34:57 +00:00
});