-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
104 lines (82 loc) · 3.07 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { delay } from "bluebird";
import * as chai from "chai";
import {
reduce,
reduceAsync,
reducer,
reducerAsync
} from "./index";
const expect = chai.expect;
// sync tests
const abc = ["a", "b", "c"];
function* alphaConcatGen(alpha: string[]) {
for (const letter of alpha) {
const previousYield = (yield) || "";
yield previousYield + letter;
}
}
const alphaConcater = reducer(alphaConcatGen);
const alphaConcaterResult = alphaConcater(abc);
console.log("alphaConcaterResult:");
console.log(alphaConcaterResult);
expect(alphaConcaterResult).to.equal("abc");
const alphaConcatResult = reduce(alphaConcatGen(abc));
console.log("alphaConcatResult:");
console.log(alphaConcatResult);
expect(alphaConcatResult).to.equal("abc");
function* alphaConcatPlusGen(alpha: string[], doodad: string) {
for (const letter of alpha) {
const previousYield = (yield) || "";
yield previousYield + letter + doodad;
}
};
const alphaConcaterPlus = reducer(alphaConcatPlusGen);
const alphaConcaterPlusResult = alphaConcaterPlus(abc, "|");
console.log("alphaConcaterPlusResult:");
console.log(alphaConcaterPlusResult);
expect(alphaConcaterPlusResult).to.equal("a|b|c|");
const alphaConcatPlusResult = reduce(alphaConcatPlusGen(abc, "|"));
console.log("alphaConcatPlusResult:");
console.log(alphaConcatPlusResult);
expect(alphaConcatPlusResult).to.equal("a|b|c|");
// async tests
const delays = [500, 1500, 1000];
async function* delayAsyncGen(delayMilliseconds: number[]) {
for (const delayMs of delayMilliseconds) {
const totalDelayedMilliseconds: number = (yield) || 0;
await delay(delayMs);
yield totalDelayedMilliseconds + delayMs;
}
}
const delayerAsync = reducerAsync(delayAsyncGen);
(async function () {
const delayerAsyncResult = await delayerAsync(delays);
console.log("delayerAsyncResult:");
console.log(delayerAsyncResult);
expect(delayerAsyncResult).to.equal(3000);
const delayAsyncResult = await reduceAsync(delayAsyncGen(delays));
console.log("delayAsyncResult:");
console.log(delayAsyncResult);
expect(delayAsyncResult).to.equal(3000);
})();
async function addDelay(totalDelayedMilliseconds: number, delayMs: number, plus: number): Promise<number> {
await delay(delayMs);
return totalDelayedMilliseconds + delayMs + plus;
}
async function* delayPlusAsyncGen(delayMilliseconds: number[], plus: number) {
for (const delayMs of delayMilliseconds) {
const totalDelayedMilliseconds: number = (yield) || 0;
yield addDelay(totalDelayedMilliseconds, delayMs, plus);
}
}
const delayerPlusAsync = reducerAsync(delayPlusAsyncGen);
(async function () {
const delayerPlusAsyncResult = await delayerPlusAsync(delays, 1);
console.log("delayerPlusAsyncResult:");
console.log(delayerPlusAsyncResult);
expect(delayerPlusAsyncResult).to.equal(3003);
const delayPlusAsyncResult = await reduceAsync(delayPlusAsyncGen(delays, 1));
console.log("delayPlusAsyncResult:");
console.log(delayPlusAsyncResult);
expect(delayPlusAsyncResult).to.equal(3003);
})();