Skip to content

Commit

Permalink
fix(compose): add varargs override for jux(), add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 12, 2019
1 parent 05bf213 commit e9d57fc
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 8 deletions.
5 changes: 3 additions & 2 deletions packages/compose/src/juxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export function juxt<T, A, B, C, D, E>(a: Fn<T, A>, b: Fn<T, B>, c: Fn<T, C>, d:
export function juxt<T, A, B, C, D, E, F>(a: Fn<T, A>, b: Fn<T, B>, c: Fn<T, C>, d: Fn<T, D>, e: Fn<T, E>, f: Fn<T, F>): Fn<T, [A, B, C, D, E, F]>;
export function juxt<T, A, B, C, D, E, F, G>(a: Fn<T, A>, b: Fn<T, B>, c: Fn<T, C>, d: Fn<T, D>, e: Fn<T, E>, f: Fn<T, F>, g: Fn<T, G>): Fn<T, [A, B, C, D, E, F, G]>;
export function juxt<T, A, B, C, D, E, F, G, H>(a: Fn<T, A>, b: Fn<T, B>, c: Fn<T, C>, d: Fn<T, D>, e: Fn<T, E>, f: Fn<T, F>, g: Fn<T, G>, h: Fn<T, H>): Fn<T, [A, B, C, D, E, F, G, H]>;
export function juxt(...fns: Fn<any, any>[]) {
export function juxt<T, A, B, C, D, E, F, G, H>(a: Fn<T, A>, b: Fn<T, B>, c: Fn<T, C>, d: Fn<T, D>, e: Fn<T, E>, f: Fn<T, F>, g: Fn<T, G>, h: Fn<T, H>, ...xs: Fn<T, any>[]): Fn<T, any[]>;
export function juxt<T>(...fns: Fn<any, any>[]) {
const [a, b, c, d, e, f, g, h] = fns;
switch (fns.length) {
case 1:
Expand All @@ -28,7 +29,7 @@ export function juxt(...fns: Fn<any, any>[]) {
case 8:
return (x) => [a(x), b(x), c(x), d(x), e(x), f(x), g(x), h(x)];
default:
return (x: any) => {
return (x: T) => {
let res = new Array(fns.length);
for (let i = fns.length; --i >= 0;) {
res[i] = fns[i](x);
Expand Down
15 changes: 15 additions & 0 deletions packages/compose/test/delay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { delay } from "../src";

import * as assert from "assert";

describe("delay", () => {

it("only executes once", () => {
let num = 0;
const a = delay(() => ++num);
assert(!a.isRealized());
assert.equal(a.deref(), 1);
assert.equal(a.deref(), 1);
assert(a.isRealized());
});
});
6 changes: 0 additions & 6 deletions packages/compose/test/index.ts

This file was deleted.

98 changes: 98 additions & 0 deletions packages/compose/test/juxt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { juxt } from "../src";

import * as assert from "assert";

describe("juxt", () => {

it("2-args", () => {
const a = juxt(
(x: number) => x + 1,
(x: number) => x * 10
);
assert.deepEqual(a(1), [2, 10]);
});

it("3-args", () => {
const a = juxt(
(x: number) => x + 1,
(x: number) => x * 10,
(x: number) => "id-" + x
);
assert.deepEqual(a(1), [2, 10, "id-1"]);
});

it("4-args", () => {
const a = juxt(
(x: number) => x + 1,
(x: number) => x * 10,
(x: number) => "id-" + x,
(x: number) => [x, x],
);
assert.deepEqual(a(1), [2, 10, "id-1", [1, 1]]);
});

it("5-args", () => {
const a = juxt(
(x: number) => x + 1,
(x: number) => x * 10,
(x: number) => "id-" + x,
(x: number) => [x, x],
(x: number) => ({ a: x }),
);
assert.deepEqual(a(1), [2, 10, "id-1", [1, 1], { a: 1 }]);
});

it("6-args", () => {
const a = juxt(
(x: number) => x + 1,
(x: number) => x - 1,
(x: number) => x * 10,
(x: number) => "id-" + x,
(x: number) => [x, x],
(x: number) => ({ a: x }),
);
assert.deepEqual(a(1), [2, 0, 10, "id-1", [1, 1], { a: 1 }]);
});

it("7-args", () => {
const a = juxt(
(x: number) => x + 1,
(x: number) => x - 1,
(x: number) => x * 10,
(x: number) => x * 100,
(x: number) => "id-" + x,
(x: number) => [x, x],
(x: number) => ({ a: x }),
);
assert.deepEqual(a(1), [2, 0, 10, 100, "id-1", [1, 1], { a: 1 }]);
});

it("8-args", () => {
const a = juxt(
(x: number) => x + 1,
(x: number) => x - 1,
(x: number) => x * 10,
(x: number) => x * 100,
(x: number) => x * 1000,
(x: number) => "id-" + x,
(x: number) => [x, x],
(x: number) => ({ a: x }),
);
assert.deepEqual(a(1), [2, 0, 10, 100, 1000, "id-1", [1, 1], { a: 1 }]);
});

it("9-args", () => {
const a = juxt(
(x: number) => x + 1,
(x: number) => x - 1,
(x: number) => x * 10,
(x: number) => x * 100,
(x: number) => x * 1000,
(x: number) => x * 10000,
(x: number) => "id-" + x,
(x: number) => [x, x],
(x: number) => ({ a: x }),
);
assert.deepEqual(a(1), [2, 0, 10, 100, 1000, 10000, "id-1", [1, 1], { a: 1 }]);
});
});
41 changes: 41 additions & 0 deletions packages/compose/test/partial.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { partial } from "../src";

import * as assert from "assert";

describe("partial", () => {

const fn = (a, b, c, d, e, f, g, h, i) => [a, b, c, d, e, f, g, h, i];
const res = [0, 1, 2, 3, 4, 5, 6, 7, 8];

it("1-arg", () => {
assert.deepEqual(partial(fn, 0)(1, 2, 3, 4, 5, 6, 7, 8), res);
});

it("2-arg", () => {
assert.deepEqual(partial(fn, 0, 1)(2, 3, 4, 5, 6, 7, 8), res);
});

it("3-arg", () => {
assert.deepEqual(partial(fn, 0, 1, 2)(3, 4, 5, 6, 7, 8), res);
});

it("4-arg", () => {
assert.deepEqual(partial(fn, 0, 1, 2, 3)(4, 5, 6, 7, 8), res);
});

it("5-arg", () => {
assert.deepEqual(partial(fn, 0, 1, 2, 3, 4)(5, 6, 7, 8), res);
});

it("6-arg", () => {
assert.deepEqual(partial(fn, 0, 1, 2, 3, 4, 5)(6, 7, 8), res);
});

it("7-arg", () => {
assert.deepEqual(partial(fn, 0, 1, 2, 3, 4, 5, 6)(7, 8), res);
});

it("8-arg", () => {
assert.deepEqual(partial(fn, 0, 1, 2, 3, 4, 5, 6, 7)(8), res);
});
});

0 comments on commit e9d57fc

Please sign in to comment.