-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (39 loc) · 1.44 KB
/
index.js
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
const back = {
eME(op1, op, op2) {
const n1 = parseFloat(op1),
n2 = parseFloat(op2);
if (isNaN(n1) || isNaN(n2)) return null;
switch (op) {
case '+': return n1 + n2;
case '-': return n1 - n2;
case '*': return n1 * n2;
case '/': return n1 / n2;
default: return null;
}
},
}
/** Pandoa is an Google Closure alternative written in pure JS to upgrade JS input. */
const pandoa = {
closure(inp) {
let out = inp;
while (true) {
const pOp = out;
out = out
.replace(/([\d.]+)\s*([+\-*/])\s*([\d.]+)/g, (_, op1, op, op2) => {
const r = back.eME(op1, op, op2);
return r !== null ? r.toString() : _;
});
if (out === pOp) {
break;
}
}
out = out.replace(/[\r\n]+/g, ';')
.replace(/;+$/, '').replace(/(\))\s*(\{)/g, '$1$2').replace(" => ", "=>").replace("[\n", "[")
.replace("\n]", "]").replace("{\n", "{").replace("\n}", "}").replace(",\n", ",")
.replace(", ", ",").replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, '').replace(" ;", ";")
.replace(/ = /g, '=').replace(/ - /g, '-').replace(/ \+ /g, '+').replace(/ \/ /g, '/')
.replace(";}", "}").replace(") {", "){");
return out;
},
};
export default pandoa;