functional/src/index.test.ts

38 lines
1.2 KiB
TypeScript

import { partial, cmp, nop, identity } from "./index";
describe("nop", () => {
it("does nothing", () => {
expect(nop()).toBeUndefined();
});
});
describe("identity", () => {
it("returns the argument untouched", () => {
expect(identity(5)).toBe(5);
expect(identity(null)).toBe(null);
});
});
describe("partial", () => {
it("applies systematically fixed configuration", () => {
const func = (conf: { a: number, b: string, c: number }) => `${conf.a}${conf.b}${conf.c}`;
const pfunc = partial({ b: "test" }, func);
expect(pfunc({ a: 5, c: 8 })).toEqual("5test8");
});
it("pass through remaining arguments", () => {
const func = (conf: { a: number, b: string }, c: number) => `${conf.a}${conf.b}${c}`;
const pfunc = partial({ b: "test" }, func);
expect(pfunc({ a: 2 }, 3)).toEqual("2test3");
});
});
describe("cmp", () => {
it("simplifies array sorting", () => {
expect([8, 3, 5].sort(cmp())).toEqual([3, 5, 8]);
expect([8, 3, 5, 8].sort(cmp({ reverse: true }))).toEqual([8, 8, 5, 3]);
expect([-2, 8, -7].sort(cmp({ key: Math.abs }))).toEqual([-2, -7, 8]);
expect(["c", "a", "b"].sort(cmp())).toEqual(["a", "b", "c"]);
});
});