-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe.test.ts
103 lines (85 loc) · 3.05 KB
/
pipe.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 { flow, flowAsync, pipe, pipeAsync } from "./pipe.ts";
import { assertEquals } from "@std/assert";
Deno.test("pipe()", async (t) => {
await t.step("pipes with no functions returns input value", () => {
const result = pipe(5);
assertEquals(result, 5);
});
await t.step("pipes with single function", () => {
const double = (n: number) => n * 2;
const result = pipe(5, double);
assertEquals(result, 10);
});
await t.step("pipes with multiple functions", () => {
const double = (n: number) => n * 2;
const addOne = (n: number) => n + 1;
const toString = (n: number) => n.toString();
const result = pipe(5, double, addOne, toString);
assertEquals(result, "11");
});
await t.step("preserves types through the chain", () => {
const numToString = (n: number) => n.toString();
const stringToArray = (s: string) => s.split("");
const arrayToLength = (arr: string[]) => arr.length;
const result = pipe(123, numToString, stringToArray, arrayToLength);
assertEquals(result, 3);
});
});
Deno.test("pipeAsync()", async (t) => {
await t.step("pipes with no functions returns input value", async () => {
const result = await pipeAsync(5);
assertEquals(result, 5);
});
await t.step("pipes with single function", async () => {
const double = (n: number) => n * 2;
const result = await pipeAsync(5, double);
assertEquals(result, 10);
});
await t.step("pipes multiple functions", async () => {
const double = (n: number) => n * 2;
const addOne = (n: number) => n + 1;
const toString = (n: number) => n.toString();
const result = await pipeAsync(5, double, addOne, toString);
assertEquals(result, "11");
});
await t.step("preserves types through the chain", async () => {
const numToString = (n: number) => n.toString();
const stringToArray = (s: string) => s.split("");
const arrayToLength = (arr: string[]) => arr.length;
const result = await pipeAsync(
123,
numToString,
stringToArray,
arrayToLength,
);
assertEquals(result, 3);
});
});
Deno.test("flow()", async (t) => {
await t.step("composes a single function", () => {
const double = (n: number) => n * 2;
const result = flow(double);
assertEquals(result(5), 10);
});
await t.step("composes multiple functions", () => {
const double = (n: number) => n * 2;
const addOne = (n: number) => n + 1;
const toString = (n: number) => n.toString();
const result = flow(double, addOne, toString);
assertEquals(result(5), "11");
});
});
Deno.test("flowAsync()", async (t) => {
await t.step("composes a single function", async () => {
const double = (n: number) => n * 2;
const result = flowAsync(double);
assertEquals(await result(5), 10);
});
await t.step("composes multiple functions", async () => {
const double = (n: number) => n * 2;
const addOne = (n: number) => n + 1;
const toString = (n: number) => n.toString();
const result = flowAsync(double, addOne, toString);
assertEquals(await result(5), "11");
});
});