forked from gcanti/tcomb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcomb-tests.ts
405 lines (313 loc) · 6.42 KB
/
tcomb-tests.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import * as t from './index'
//
// basic types
//
// * Any *
t.Any('s');
t.Any(1);
t.Any(true);
// static members
t.Any.displayName;
// meta object
t.Any.is(1);
t.Any.meta.kind;
t.Any.meta.name;
t.Any.meta.identity;
t.Any.meta.predicate;
// * Nil *
t.Nil(null);
t.Nil(undefined);
// * String *
t.String('s');
// * Number *
t.Number(1);
// * Boolean *
t.Boolean(true);
t.Boolean(false);
// * Array *
t.Array([])
t.Array([1, 2, 's'])
// * Object *
t.Object({})
t.Object({a: 1, b: 's'})
// * Function *
t.Function(() => {})
// * Error *
t.Error(new Error())
// * RegExp *
t.RegExp(/a/)
// * Date *
t.Date(new Date())
// test type guard
function testTypeGuardBuiltIn(x: string | number): string {
if (t.String.is(x)) {
return x;
}
return String(x);
}
//
// irreducible combinator
//
// * based on string *
const Url = t.irreducible<string>('Url', (s) => t.String.is(s) && s.indexOf('http') === 0);
Url('s')
// static members
Url.displayName;
// meta object
Url.meta.kind;
Url.meta.name;
Url.meta.identity;
Url.meta.predicate;
// using a tcomb type as type annotation
function f(url: typeof Url.t) {}
// * based on Date *
const PastDate = t.irreducible<Date>('PastDate', (date) => t.Date.is(date) && date.getTime() < new Date().getTime());
PastDate(new Date(1973, 10, 30))
//
// refinement combinator
//
// * a refinement based on a basic type *
const Email = t.refinement(t.String, (s) => s.indexOf('@') !== -1);
Email('s')
// static members
Email.displayName;
// meta object
Email.meta.kind;
Email.meta.name;
Email.meta.identity;
Email.meta.type;
Email.meta.predicate;
// * a refinement based on a Class *
class A {}
const ARefinement = t.refinement<A>(A, () => true);
ARefinement(new A())
//
// interface combinator
//
const MyCoolType = t.interface(
{ isCool: t.Boolean },
{ name: 'My cool type', strict: false }
);
const MySuperCoolType = MyCoolType.extend(
{ isEvenCooler: t.Boolean },
{ name: 'My super cool type', strict: false }
);
//
// struct combinator
//
interface Person {
name: string;
age: number;
}
const Person = t.struct<Person>({
name: t.String,
age: t.Number
}, 'Person');
const person1 = new Person({ name: 'Giulio', age: 42 })
const person2 = Person({ name: 'Giulio', age: 42 })
// static members
Person.displayName;
// extend function
interface Person2 extends Person {
surname: string;
}
const Person2 = Person.extend<Person2>({ surname: t.String })
const Person2b = Person.extend<Person2>(
{ surname: t.String },
{ name: 'Person 2b', strict: false }
)
// update function
const person3 = Person.update(person1, {
name: {$set: 'Guido'}
});
// meta object
Person.meta.kind;
Person.meta.name;
Person.meta.identity;
Person.meta.props;
//
// list combinator
//
const Tags = t.list(t.String, 'Tags');
const list1 = Tags(['a', 'b']);
// static members
Tags.displayName;
// update function
const list2 = Tags.update(list1, {
0: {$set: 's'}
});
// meta object
Tags.meta.kind;
Tags.meta.name;
Tags.meta.identity;
Tags.meta.type;
//
// dict combinator
//
const Phones = t.dict(t.String, t.Number, 'Phones');
const dict1 = Phones({a: 1, b: 2});
// static members
Phones.displayName;
// update function
const dict2 = Phones.update(dict1, {
$remove: ['a']
});
// meta object
Phones.meta.kind;
Phones.meta.name;
Phones.meta.identity;
Phones.meta.domain;
Phones.meta.codomain;
//
// enums combinator
//
const Country = t.enums({
IT: 'Italy',
US: 'United States'
}, 'Country');
const country = Country('IT');
// static members
Country.displayName;
// meta object
Country.meta.kind;
Country.meta.name;
Country.meta.identity;
Country.meta.map;
// of
const Country2 = t.enums.of(['IT', 'US'], 'Country2');
const Country3 = t.enums.of('IT US', 'Country3');
//
// maybe combinator
//
const MaybeString = t.maybe(t.String, 'MaybeString');
MaybeString('s');
MaybeString(null);
MaybeString(undefined);
// static members
MaybeString.displayName;
// meta object
MaybeString.meta.kind;
MaybeString.meta.name;
MaybeString.meta.identity;
MaybeString.meta.type;
//
// tuple combinator
//
const Size = t.tuple<[number, number]>([t.Number, t.Number], 'Size');
type Size = typeof Size.t;
const size1 = Size([100, 200]);
// static members
Size.displayName;
// meta object
Size.meta.kind;
Size.meta.name;
Size.meta.identity;
Size.meta.types;
// update function
const size2 = Size.update(size1, {
0: { $set: 150 }
});
//
// union combinator
//
const Union = t.union<Person | Size>([Person, Size], 'Union');
Union.dispatch = function (x) {
return t.Array.is(x) ? Size : Person;
};
const union1 = Union({ name: 'Giulio', age: 42 });
// static members
Union.displayName;
Union.dispatch({ name: 'Giulio', age: 42 });
// meta object
Union.meta.kind;
Union.meta.name;
Union.meta.identity;
Union.meta.types;
// update function
const union2 = Union.update(union1, {
name: { $set: 'Guido' }
});
//
// union combinator
//
const Min = t.refinement(t.String, (s) => s.length > 2);
const Max = t.refinement(t.String, (s) => s.length < 5);
const MinMax = t.intersection<string>([Min, Max], 'MinMax');
const minmax1 = MinMax('s');
// static members
MinMax.displayName;
// meta object
MinMax.meta.kind;
MinMax.meta.name;
MinMax.meta.identity;
MinMax.meta.types;
// update function
const minmax2 = MinMax.update(minmax1, { $set: 'ss' });
//
// declare combinator
//
type Tree = {
value: number;
left?: Tree;
right?: Tree;
};
const Tree = t.declare<Tree>('Tree');
Tree.define(t.struct({
value: t.Number,
left: t.maybe(Tree),
right: t.maybe(Tree)
}));
const bst = Tree({
value: 5,
left: {
value: 2
},
right: {
left: {
value: 6
},
value: 7
}
});
//
// is
//
t.is(1, t.Number);
//
// assert
//
t.assert(true, 'a message');
t.assert(true, () => 'a lazy message');
//
// fail
//
t.fail('a message');
//
// isType
//
t.isType(t.String);
t.isType(A);
//
// getTypeName
//
t.getTypeName(t.String);
t.getTypeName(A);
//
// mixin
//
t.mixin({a: 1}, {b: 2}).a;
t.mixin({a: 1}, {b: 2}).b;
//
// match
//
t.match(1,
t.String, (s) => 'a string',
t.Number, (n) => n > 2, (n) => 'a number gt 2', // case with a guard (optional)
t.Number, (n) => 'a number lte 2',
A, (a) => 'an instance of A',
t.Any, (x) => 'other...' // catch all
);
//
// update
//
t.update({}, { a: { $set: 1 } });