generated from shortercode/ts-library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playground.html
190 lines (181 loc) · 4.96 KB
/
playground.html
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
<!DOCTYPE html>
<html>
<head>
<style>
html {
height: 100%;
}
body {
height: 100%;
margin: 0;
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 3rem 1fr 2fr;
}
.header {
grid-column: 1 / 3;
grid-row: 1;
background-color: rgb(0 142 171);
}
.json-pane {
grid-column: 1;
grid-row: 2 / 4;
background-color: rgb(255, 255, 245);
border-right: 2px solid rgb(0 142 171);
overflow: auto;
padding: 1rem;
box-sizing: border-box;
outline: none;
font-family: monospace;
white-space: pre;
}
.expression-pane {
grid-column: 2;
grid-row: 2;
background-color: rgb(255, 255, 245);
border-bottom: 2px solid rgb(0 142 171);
overflow: auto;
padding: 1rem;
box-sizing: border-box;
outline: none;
font-family: monospace;
white-space: pre;
}
.output-pane {
grid-column: 2;
grid-row: 3;
background-color: lightgrey;
overflow: auto;
padding: 1rem;
box-sizing: border-box;
font-family: monospace;
white-space: pre;
}
</style>
</head>
<body>
<div class="header"></div>
<div class="json-pane" contenteditable>
{
"Account": {
"Account Name": "Firefly",
"Order": [
{
"OrderID": "order103",
"Product": [
{
"Product Name": "Bowler Hat",
"ProductID": 858383,
"SKU": "0406654608",
"Description": {
"Colour": "Purple",
"Width": 300,
"Height": 200,
"Depth": 210,
"Weight": 0.75
},
"Price": 34.45,
"Quantity": 2
},
{
"Product Name": "Trilby hat",
"ProductID": 858236,
"SKU": "0406634348",
"Description": {
"Colour": "Orange",
"Width": 300,
"Height": 200,
"Depth": 210,
"Weight": 0.6
},
"Price": 21.67,
"Quantity": 1
}
]
},
{
"OrderID": "order104",
"Product": [
{
"Product Name": "Bowler Hat",
"ProductID": 858383,
"SKU": "040657863",
"Description": {
"Colour": "Purple",
"Width": 300,
"Height": 200,
"Depth": 210,
"Weight": 0.75
},
"Price": 34.45,
"Quantity": 4
},
{
"ProductID": 345664,
"SKU": "0406654603",
"Product Name": "Cloak",
"Description": {
"Colour": "Black",
"Width": 30,
"Height": 20,
"Depth": 210,
"Weight": 2
},
"Price": 107.99,
"Quantity": 1
}
]
}
]
}
}
</div>
<div class="expression-pane" contenteditable>
Account.Order.Product.(Price * Quantity)
</div>
<div class="output-pane"></div>
<script type="module">
import { prepare_expression, scan, Runtime } from './mjs/index.mjs';
const json_pane = document.querySelector('.json-pane');
const expression_pane = document.querySelector('.expression-pane');
const output_pane = document.querySelector('.output-pane');
const runtime = new Runtime;
function update () {
const expr_source = expression_pane.textContent;
let expr;
try {
expr = prepare_expression(runtime, expr_source);
} catch (e) {
output_pane.textContent = e.message;
return;
}
let data;
try {
data = JSON.parse(json_pane.textContent);
} catch {
output_pane.textContent = 'Invalid JSON';
return;
}
let result;
try {
result = expr(data);
} catch (e) {
output_pane.textContent = e.message;
return;
}
if (result === undefined) {
output_pane.textContent = 'No match';
} else {
output_pane.textContent = JSON.stringify(result, null, 2);
}
}
json_pane.addEventListener('input', () => {
update();
});
expression_pane.addEventListener('input', () => {
update();
});
update();
</script>
</body>
</html>