-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.html
94 lines (88 loc) · 2.37 KB
/
demo.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>CEL Parser Demo</title>
<style>
body,
textarea {
margin: 0;
font-family: "IBM Plex Mono", ui-monospace, monospace;
font-size: 18px;
}
textarea,
div {
width: calc(100% - 20px);
border: 2px solid #ccc;
padding: 8px;
}
textarea:focus {
outline: 0;
}
div {
white-space: pre;
border-color: white;
}
.error {
color: #999;
font-style: oblique;
}
</style>
</head>
<body>
<script src="dist/index.js"></script>
<script src="dist/to-debug-string.js"></script>
<script>
function execute(expression) {
if (expression.trim() === "") {
document.getElementById("result").innerHTML = "";
} else {
try {
document.getElementById("result").innerHTML = print(
CEL.parse(expression),
);
document.getElementById("result").classList.remove("error");
} catch (e) {
document.getElementById("result").classList.add("error");
throw e;
}
}
}
function indent(string) {
return " " + string.replaceAll("\n", "\n ");
}
function print(thing) {
if (typeof toDebugString === "function") {
return toDebugString(thing);
} else if (Array.isArray(thing)) {
return "[\n" + indent(thing.map(print).join("\n")) + "\n]";
} else if (typeof thing === "object") {
return (
thing.constructor.name +
" {\n" +
indent(
Object.keys(thing)
.map((k) => k + ": " + print(thing[k]))
.join("\n"),
) +
"\n}"
);
} else if (typeof thing === "undefined") {
return "undefined";
} else if (typeof thing === "bigint") {
return thing.toString();
} else {
return JSON.stringify(thing);
}
}
</script>
<textarea
placeholder="CEL expression"
rows="3"
oninput="execute(this.value)"
></textarea>
<div id="result"></div>
</body>
</html>