-
Notifications
You must be signed in to change notification settings - Fork 1
/
recursive.test.ts
167 lines (150 loc) · 3.5 KB
/
recursive.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { expect, test, vi } from "vitest";
import {
array,
Codec,
DecoderResult,
fields,
flatMap,
multi,
record,
recursive,
string,
} from "../index.js";
test("recursive data structure", () => {
// Consider this recursive data structure:
type Person = {
name: string;
friends: Array<Person>;
};
// This wouldn’t work to decode it, because we’re trying to use
// `personCodec` in the definition of `personCodec` itself.
/*
const personCodec = fields<Person>({
name: string,
friends: array(personCodec), // ReferenceError: Cannot access 'personCodec' before initialization
});
*/
// `recursive` lets us delay when `personCodec` is referenced, solving the
// issue.
const personCodec: Codec<Person> = fields({
name: string,
friends: array(recursive(() => personCodec)),
});
const data: unknown = {
name: "John",
friends: [
{
name: "Alice",
friends: [],
},
{
name: "Bob",
friends: [
{
name: "Charlie",
friends: [],
},
],
},
],
};
expect(personCodec.decoder(data)).toMatchInlineSnapshot(`
{
"tag": "Valid",
"value": {
"friends": [
{
"friends": [],
"name": "Alice",
},
{
"friends": [
{
"friends": [],
"name": "Charlie",
},
],
"name": "Bob",
},
],
"name": "John",
},
}
`);
});
test("recurse non-record", () => {
type Dict = { [key: string]: Dict | number };
const dictCodec: Codec<Dict> = record(
flatMap(multi(["number", "object"]), {
decoder: (value): DecoderResult<Dict | number> => {
switch (value.type) {
case "number":
return { tag: "Valid", value: value.value };
case "object":
// Thanks to the arrow function we’re in, the reference to
// `dictCodec` is delayed and therefore works.
return dictCodec.decoder(value.value);
}
},
encoder: (value) =>
typeof value === "number"
? { type: "number", value }
: { type: "object", value },
}),
);
const data: unknown = {
t: {
i: {
n: {
y: 1,
t: 2,
},
e: 3,
},
},
};
expect(dictCodec.decoder(data)).toMatchInlineSnapshot(`
{
"tag": "Valid",
"value": {
"t": {
"i": {
"e": 3,
"n": {
"t": 2,
"y": 1,
},
},
},
},
}
`);
});
test("circular objects", () => {
// This data structure is impossible to create without mutation, because you
// can’t create a Person without creating a Person.
type Person = {
name: string;
likes: Person;
};
const personCodec: Codec<Person> = fields({
name: string,
likes: recursive(() => personCodec),
});
const alice: Record<string, unknown> = {
name: "Alice",
likes: undefined,
};
const bob: Record<string, unknown> = {
name: "Bob",
likes: alice,
};
// Make the object circular:
alice.likes = bob;
// Calling the decoder would cause infinite recursion!
// So be careful when working with recursive data!
const wouldCauseInfiniteRecursion1: () => DecoderResult<Person> = vi.fn(() =>
personCodec.decoder(alice),
);
expect(wouldCauseInfiniteRecursion1).not.toHaveBeenCalled();
});