-
Notifications
You must be signed in to change notification settings - Fork 28
/
nullishChain.test.ts
44 lines (37 loc) · 1.12 KB
/
nullishChain.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
import { asyncNullishChain, nullishChain } from "./nullishChain.ts";
import { assertEquals } from "testing/asserts.ts";
Deno.test("nullish chain", () => {
const numChain = nullishChain(
(n: number) => (n === 1 ? n : null),
(n) => (n === 2 ? n : undefined),
(n) => (n === 3 ? n : null),
);
assertEquals(numChain(1), 1);
assertEquals(numChain(2), 2);
assertEquals(numChain(3), 3);
assertEquals(numChain(4), undefined);
// @ts-expect-error
// prettier-ignore
nullishChain((_n: number) => 0, (_n: string) => 0);
// @ts-expect-error
const _str: string = nullishChain(() => 2)();
const _empty = nullishChain();
});
Deno.test("async nullish chain", async () => {
const chain = asyncNullishChain(
(n: number) => {
if (n === 1) return "foo";
},
async (n) => {
if (n === 2) return "bar";
},
(n) =>
new Promise<string | null>((res) =>
setTimeout(() => res(n === 3 ? "baz" : null), 100)
),
);
assertEquals(await chain(1), "foo");
assertEquals(await chain(2), "bar");
assertEquals(await chain(3), "baz");
assertEquals(await chain(4), undefined);
});