-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (89 loc) · 2.55 KB
/
index.js
File metadata and controls
112 lines (89 loc) · 2.55 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
document.addEventListener('DOMContentLoaded', onBodyLoad);
const runButton = document.querySelector('#run');
runButton.addEventListener('click', run);
function onBodyLoad() {
const footer = document.querySelector('#user-agent');
footer.textContent = window.navigator.userAgent;
const input = document.querySelector('#input');
input.addEventListener('input', onInput);
input.addEventListener(
'keydown',
(event) => {
if (event.ctrlKey && event.code === 'Enter') {
run();
}
}
);
onInput();
}
function onInput() {
const input = document.querySelector('#input');
const button = document.querySelector('#run');
const xEvaluatedTo = document.querySelector('#x-evaluated-to');
if (input.value.length > 0) {
button.removeAttribute('disabled');
const toEval = `x = ${input.value}`;
let x = undefined;
try {
eval(toEval);
xEvaluatedTo.textContent = `const x = ${format(x)};`;
} catch (error) {
console.error(error);
xEvaluatedTo.textContent = 'Invalid expression';
}
} else {
button.setAttribute('disabled', '');
xEvaluatedTo.textContent = 'undefined';
}
}
function run() {
const input = document.querySelector('#input');
const xEvaluatedTo = document.querySelector('#x-evaluated-to');
const output = document.querySelector('#output');
const button = document.querySelector('#run');
const buttonText = button.querySelector('.button-text');
const spinner = button.querySelector('.spinner-border');
button.disabled = true;
buttonText.classList.add('d-none');
spinner.classList.remove('d-none');
const toEval = `x = ${input.value}`;
console.debug('toEval', toEval);
let x = undefined;
let result;
try {
eval(toEval);
xEvaluatedTo.textContent = `const x = ${format(x)};`;
const { isInfinite, examples } = giveExamples(x);
if (examples.length > 0) {
if (isInfinite) {
result = examples
.map((example, index) => `x == ${format(example, index)}`)
.join('\n')
.concat('\n…');
} else {
result = examples
.map(example => `x == ${format(example)}`)
.join('\n');
}
output.classList.add('success');
} else {
result = `Nothing is loosely equal to ${format(x)}.`;
}
output.classList.remove('error');
} catch (error) {
console.error(error);
xEvaluatedTo.textContent = 'undefined';
result = error.message || error.stack;
output.classList.add('error');
output.classList.remove('success');
}
output.textContent = result;
setTimeout(
() => {
button.disabled = false;
buttonText.classList.remove('d-none');
spinner.classList.add('d-none');
},
100
);
}