-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.ts
More file actions
126 lines (105 loc) · 2.97 KB
/
Copy pathexamples.ts
File metadata and controls
126 lines (105 loc) · 2.97 KB
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
// This file contains the code for the section
// An Extensible Expression Evaluator
// in the file
// Writing Full-Fledged Type Programs in Typescript.md
/// <reference path="tests.ts" />
type SAMPLE_OBJ = {
rec: 'rec';
car: 'a car';
tree: 'a tree';
Tim: 'ok';
whiteList: ['Tim', 'John', 'Luke', 'Luca', 'Stefano', 'Doe', 'Smith'];
ifStartsWith: ['name', 'surname'];
name1: 'Tim';
name2: 'John';
name3: 'Luca';
name4: 'Luke';
name5: 'Stefano';
// name_invalid: 'Stephen';
surname1: 'Doe';
surname2: 'Smith';
// surname_invalid: 'Rossi';
};
type SAMPLE_TEST = EvalExpr<SAMPLE_OBJ, `
$'KEYS' filter ('key' => {
if 'ifStartsWith' then (
*'ifStartsWith' map ('start' => {
call('StartsWith', v'start', v'key')
}) reduce 'any'
) else call('StartsWith', 'name', v'key')
}) map ('x' => {
*v'x' in *'whiteList'
}) reduce 'all'
`>;
type SAMPLE_TEST2 = EvalExpr<SAMPLE_OBJ, `
bool 'rec'
`>;
type SAMPLE_TEST3 = EvalExpr<SAMPLE_OBJ, `
('car' && 'Tim' && 'tree') || 'name5'
`>;
type SAMPLE_TEST4 = EvalExpr<SAMPLE_OBJ, `
'truck' || '"Truck" not present'
`>;
type EvalBoolExpr<Obj, Expr> = EvalExpr<Obj, `bool (${Expr & string})`>;
type SAMPLE_TEST5 = EvalBoolExpr<SAMPLE_OBJ, `
('car' && 'Tim' && 'tree') || 'name5'
`>;
interface Funcs3<T1, T2, T3> {
// T1 = {'op', 'mem'}
INFIX2__and__PARSE: ['INFIX2__&&__', _EVAL_Skip<T2>, _EVAL_Skip<T3>];
INFIX2__and__: 0; // does nothing
// T1 = {'op', 'mem'}
INFIX2__or__PARSE: ['INFIX2__||__', _EVAL_Skip<T2>, _EVAL_Skip<T3>];
INFIX2__or__: 0; // does nothing
}
type SAMPLE_TEST6 = EvalExpr<SAMPLE_OBJ, `
('car' and 'Tim' and 'tree') or 'name5'
`>;
type SAMPLE_TEST7 = EvalExpr<SAMPLE_OBJ, `
$'KEYS'
`>;
interface Funcs1<T> {
WrapInUnderscores: `_${T & string}_`;
}
type SAMPLE_TEST8 = EvalExpr<SAMPLE_OBJ, `
$'KEYS' map ('x' => {
call('WrapInUnderscores',
call('WrapInUnderscores', v'x')
)
})
`>;
interface Funcs3<T1, T2, T3> {
INFIX2__startsWith__: T2 extends `${T3 & string}${any}` ? true : false;
}
type SAMPLE_TEST9 = EvalExpr<SAMPLE_OBJ, `
$'KEYS' filter ('x' => {
v'x' startsWith 'name'
})
`>;
// true
type SAMPLE_TEST10 = EvalExpr<SAMPLE_OBJ, `
$'KEYS' map ('x' => {
v'x' startsWith 'name'
}) reduce 'any'
`>;
// false
type SAMPLE_TEST11 = EvalExpr<SAMPLE_OBJ, `
$'KEYS' map ('x' => {
v'x' startsWith 'name'
}) reduce 'all'
`>;
interface Funcs2<T1, T2> {
StartsWithThis: T2 extends `${T1 & string}${any}` ? true : false;
}
type SAMPLE_TEST12 = EvalExpr<SAMPLE_OBJ, `
$'KEYS' map ('StartsWithThis' bind 'name')
reduce 'any'
`>;
// [
// "LHS operand not supported by `bind`. Operands: ",
// "StarXXXXXtsWithThis", "name"
// ]
type SAMPLE_TEST13 = EvalExpr<SAMPLE_OBJ, `
$'KEYS' map ('StarXXXXXtsWithThis' bind 'name')
reduce 'any'
`>;