-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.html
400 lines (348 loc) · 17.2 KB
/
index.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Evaluatex.js</title>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700|Bree+Serif' rel='stylesheet' type='text/css'>
<style>
* {
box-sizing: border-box;
}
body {
background-color: #f8f8f8;
font: 16px "Open Sans", Helvetica, Arial, sans-serif;
padding-bottom: 20px;
}
p {
line-height: 150%;
}
a {
border-bottom: 1px dotted #d35;
color: #d35;
text-decoration: none;
}
code, pre {
background: #e8e8e8;
color: #d35;
display: block;
font-size: 0.95em;
margin: 10px 0;
padding: 3px 7px;
white-space: pre-wrap;
white-space: -moz-pre-wrap;
white-space: -o-pre-wrap;
word-wrap: break-word;
}
p code, li code {
display: inline;
margin: 0 2px;
padding: 1px 2px;
}
.example {
display: none;
}
.example-live {
align-items: center;
background: white;
/*border: 1px solid #d35;*/
border-radius: 5px;
box-shadow: 1px 1px 5px #aaa;
display: flex;
flex-direction: row;
font: 200 15px monospace;
margin: 15px -20px;
padding: 7px 10px;
}
.example-live .editor {
color: #aaa;
cursor: default;
flex: 1 1 auto;
}
.example-live input[type="text"] {
border: 0;
border-bottom: 1px solid #f35;
color: black;
font: 400 15px monospace;
min-width: 20px;
outline: none;
padding: 5px 0 5px 4px;
resize: horizontal;
width: auto;
}
.example-live .result {
flex: 0 0 auto;
font: 16px "Open Sans", sans-serif;
}
h1 {
color: #d35;
font: bold 100px "Bree Serif", Helvetica, Arial, sans-serif;
margin: 15px 0px;
text-align: center;
text-shadow: 3px 3px 5px #353;
}
h2 {
clear: both;
color: #d35;
font: 26px "Open Sans", Helvetica, Arial, sans-serif;
margin-top: 50px;
text-align: center;
}
h3 {
color: #d35;
font: oblique 20px "Open Sans", sans-serif;
margin: 25px 0;
text-align: center;
}
strong, b {
font-weight: 700;
}
.wrapper {
border: 1px dashed #ddd;
margin: auto;
max-width: 800px;
padding: 8px;
width: 90%;
}
@media screen and (max-width: 768px) {
.example-live {
display: block;
}
.example-live .result {
margin-top: 10px;
}
}
</style>
<script src="evaluatex.docs.js"></script>
<script src="stretchy.min.js"></script>
<script type="text/javascript">
window.onload = function () {
// Create example elements
const exampleTemplate = document.querySelector(".example-template");
for (el of document.querySelectorAll(".example")) {
const liveExampleNode = exampleTemplate.cloneNode(true);
liveExampleNode.setAttribute("class", "example-live");
const expressionNode = liveExampleNode.querySelector(".expression");
expressionNode.value = el.innerHTML.trim();
Stretchy.resize(expressionNode);
// Put everything on the first line if no constants or options
if (!el.hasAttribute("data-constants") && !el.hasAttribute("data-options")) {
removeNode(liveExampleNode.querySelector(".first-break"));
}
// Show/hide the second argument
const constantsNode = liveExampleNode.querySelector(".constants");
let constants = {};
if (el.hasAttribute("data-constants") || el.hasAttribute("data-options")) {
const constantsStr = el.getAttribute("data-constants") || "{}";
constantsNode.innerHTML = constantsStr;
constants = eval("(" + constantsStr + ")");
}
else {
removeNode(liveExampleNode.querySelector(".constants-container"));
}
// Show/hide the third argument
const optionsNode = liveExampleNode.querySelector(".options");
let options = {};
if (el.hasAttribute("data-options")) {
const optionsStr = el.getAttribute("data-options") || "{}";
optionsNode.innerHTML = optionsStr;
options = eval("(" + optionsStr + ")");
}
else {
removeNode(liveExampleNode.querySelector(".options-container"));
}
// Show/hide the variables
let vars = {};
if (el.hasAttribute("data-vars")) {
const varsStr = el.getAttribute("data-vars");
liveExampleNode.querySelector(".vars").innerHTML = varsStr;
vars = eval("(" + varsStr + ")"); // Need to have () around an object for eval to work
}
const resultNode = liveExampleNode.querySelector(".result");
el.parentNode.replaceChild(liveExampleNode, el);
// Update handler
let timeout = 0;
const update = function () {
try {
const expressionStr = JSON.parse("\"" + expressionNode.value.trim() + "\"");
const fn = evaluatex(expressionStr, constants, options);
const result = fn(vars);
resultNode.innerHTML = "Result: " + Math.round(result * 1000) / 1000;
}
catch (e) {
console.error(e);
resultNode.innerHTML = "Error!";
}
};
update(expressionNode);
expressionNode.onkeydown = function (ev) {
// Debounce to prevent recompiling the equation too often
clearTimeout(timeout);
timeout = setTimeout(function () {
update();
}, 300);
};
}
// Handle special cases
const expressionSpecial = document.querySelector("#expressionSpecial");
const resultSpecial = document.querySelector("#resultSpecial");
const updateSpecial = function () {
try {
const fn = evaluatex(expressionSpecial.value.trim());
const result1 = Math.round(fn({ magic: 2 }) * 1000) / 1000;
const result2 = Math.round(fn({ magic: 99 }) * 1000) / 1000;
resultSpecial.innerHTML = "Result: [" + result1 + ", " + result2 + "]";
}
catch (e) {
console.error(e);
resultSpecial.innerHTML = "Error!";
}
};
expressionSpecial.onkeydown = function() {
setTimeout(updateSpecial, 100);
};
updateSpecial();
};
function removeNode(node) {
node.parentNode.removeChild(node);
}
</script>
</head>
<body>
<a href="https://github.com/arthanzel/evaluatex"><img style="position: absolute; top: 0; right: 0; border: 0;"
src="https://camo.githubusercontent.com/a6677b08c955af8400f44c6298f40e7d19cc5b2d/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677261795f3664366436642e706e67"
alt="Fork me on GitHub"
data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png"></a>
<div class="wrapper">
<h1>Evaluatex.js</h1>
<h2>Latex and ASCIIMath evaluator for Javascript.</h2>
<p><b>Install with <a href="http://npmjs.com">NPM</a>:</b></p>
<code>$ npm install evaluatex</code>
<p><b>Use a minified version in a webapp:</b></p>
<pre><script src="node_modules/evaluatex/dist/evaluatex.min.js"></script>
<script type="text/javascript">
evaluatex();
<script></pre>
<p><b>Get zipped releases <a href="https://github.com/arthanzel/evaluatex/releases">here</a>.</b></p>
<p>Check it on <a href="https://github.com/arthanzel/evaluatex">Github</a>.</p>
<h2>Try it!</h2>
<p><b>Evaluatex</b> is a parser that reads and evaluates LaTeX and ASCII math. It can safely resolve math
expressions, without relying on Javascript's <code>eval</code>, which is evil. It's especially powerful when
combined with math markup tools such as <a href="http://mathquill.com/">Mathquill</a>.</p>
<h2>API</h2>
<pre>const fn = evaluatex(expression, constants = {}, options = {});
const result = fn(variables = {});</pre>
<ul>
<li><code>evaluatex()</code> compiles a math expression into a function <code>fn</code>.</li>
<li><code>expression</code> is an ASCII or LaTeX expression to be parsed and evaluated.</li>
<li><code>constants</code> is a map of constant values - values that don't change if you invoke <code>fn</code> more than
once.
</li>
<li><code>options</code> is a map of options for the compiler.</li>
<li><code>result</code> is the numerical result of the calculation.</li>
<li><code>variables</code> is a map of variables that <em>can</em> change between invocations of <code>fn</code>.</li>
</ul>
<h2>Demo</h2>
<p>Change the math below and see the result on the right. You may use any functions and objects in Javascript's
<code>Math</code>.</p>
<div class="example">
sqrt(3^2 + 4^2) + log(PI) / log(SQRT2)
</div>
<h2>Manual</h2>
<!-- Introduction -->
<p><b>Evaluatex</b> takes a string containing a math expression and returns a <em>compiled function</em>. The
compiled function evaluates the math and returns the result.</p>
<p>Throughout this manual, you may change the underlined math in any of the examples to explore how <b>Evaluatex</b> works.</p>
<div class="example">1 + 2 * 3 / 4</div>
<p>A one-liner:</p>
<code>const result = evaluatex("1 + 2 * 3 / 4")()</code>
<p><b>Evaluatex</b> returns a function because you can invoke it multiple times with different variables.</p>
<div class="example-live">
<div class="editor">
fn = evaluatex("<input id="expressionSpecial" class="expression" type='text' autocomplete='off'
autocorrect='off'
autocapitalize='off'
spellcheck='false' value='1 + magic'/>");
<br/>result = [ fn({ magic: 2 }), fn({ magic: 99 }) ];
</div>
<div class="result" id="resultSpecial"></div>
</div>
<!-- Basics -->
<h3>Evaluatex is a calculator</h3>
<p>Evaluatex supports all of the features you should expect from a simple calculator, including order of operations. You can use parentheses, square brackets, or curly braces interchangeably.
Exponents use the <code>^</code> operator.</p>
<div class="example">(1 + 2) * [3 ^ 4] - {5 + 6}</div>
<!-- Named values -->
<p>You can use named values in expressions that are visible to the expression only. This avoids the need for <code>eval()</code>.
See the
<a href="#constants-and-variables">Constants and variables</a> section for more.</p>
<div class="example" data-vars="{ a: 3, b: 4 }">1 + a / b</div>
<!-- Implicit multiplication -->
<p>Implicit multiplication with brackets or variables is not a problem.</p>
<div class="example" data-vars="{ a: 3, b: 4 }">4a(1 + b)</div>
<p>Be careful with associativity: <code>1 / 2a</code> is <code>(1 / 2) * a</code>.</p>
<div class="example" data-vars="{ a: 3 }">1 / 2a</div>
<!-- Javascript Math -->
<p>You have full access to all functions and constants in Javascript's <code>Math</code> object.</p>
<div class="example">sin(PI / 2) + LN2 ^ E + hypot(3, 4)</div>
<!-- Implicit brackets -->
<p>You can omit brackets from simple functions, but be careful with implicit multiplication.</p>
<div class="example">sin 2PI</div>
<div class="example">sin PI^2</div>
<p>When in doubt, use parentheses.</p>
<!-- Custom functions -->
<h3>Functions</h3>
<p>You can define custom functions as constants.</p>
<div class="example" data-constants="{ incr: function(x) { return x + 1; } }">incr(4)</div>
<p>Multi-argument functions work just as well, but they require parentheses.</p>
<div class="example">hypot(3, 4)</div>
<div class="example">min(5, 4, 3, -2, 1)</div>
<div class="example" data-constants="{ sum3: function(a, b, c) { return a + b + c; } }">sum3(10, 20, 30)</div>
<p>There are several functions built in to <b>Evaluatex</b> to help with logs, roots, and trigonometry.</p>
<div class="example">logn(81, 3)</div>
<div class="example">rootn(8, 3)</div>
<div class="example">csc(PI/4)</div>
<p>Absolute values work like a charm, as do factorials, which round to the nearest integer before performing the calculation.</p>
<div class="example">|5 - 20|</div>
<div class="example">3.6!</div>
<!-- Constants and variables -->
<h3 id="constants-and-variables">Constants and variables</h3>
<p>You can refer to symbols, such as <code>x</code>, in a math expression. These symbols can be <strong>constants</strong> or <strong>variables</strong>.</p>
<p><strong>Constants </strong> are specified in the call to <code>evaluatex()</code> as the second parameter. Their values are compiled by <strong>Evaluatex</strong> into the resultant equation. Use constants if you know that they won't change between invocations. Constants may be numeric values or functions, but not expressions like <code>PI/2</code>.</p>
<div class="example" data-constants="{ HALF_PI: 1.57 }">100 + HALF_PI</div>
<p><strong>Variables</strong> are specified when calling the compiled function. Their values can be changed between invocations. If you compile an expression with a variable, you <em>must</em> give a value for that variable, otherwise <b>Evaluatex</b> will complain. Variables may only be numeric values, and not functions.</p>
<div class="example" data-vars="{ x: 50 }">100 + x</div>
<p>You can combine constants and variables. Constants have priority over variables - if you define a constant, you can't change it without re-compiling the expression.</p>
<div class="example" data-constants="{ HALF_PI: 1.57 }" data-vars="{ x: 50, HALF_PI: 1000 }">100 + x + HALF_PI</div>
<h3>LaTeX</h3>
<p>LaTeX parses math a little differently. Turn on LaTeX behaviour with a flag:</p>
<div class="example" data-constants="{ x: 2 }" data-options="{ latex: true }">x^24</div>
<p>In the previous example, the power takes only the first symbol after it, so it reads like <code>x^2 * 4</code>. Use curly braces to fix that.</p>
<div class="example" data-constants="{ x: 2 }" data-options="{ latex: true }">x^{24}</div>
<!--<p>If you try to use parens or square brackets instead, you'll get an error.</p>
<div class="example" data-constants="{ x: 2 }" data-options="{ latex: true }">x^(24)</div>-->
<p>LaTeX commands are also supported, like <code>\frac</code>. The following example is interpreted as <code>1 / 20 * 3</code>.</p>
<div class="example" data-options="{ latex: true }">\\frac 1{20}3</div>
<h2>Issues</h2>
<p>Report issues on <a href="https://github.com/arthanzel/evaluatex" target="_blank">Github</a>. <b>Evaluatex</b> does its best to be correct, but unanticipated <s>bugs</s> features might exist.</p>
<h2>License</h2>
<p><b>Evaluatex</b> is licensed under the MIT license. This means that you can probably use it in your project, as long as you don't pass it off as your own or blame the author for the inevitable heat death of the universe.</p>
</div>
<div style="display: none">
<div class="example-template">
<div class="editor">
fn = evaluatex(<span class="first-break">
<br/> </span>"<input class="expression" type='text' autocomplete='off' autocorrect='off'
autocapitalize='off'
spellcheck='false' value=''/>"<span class="constants-container">,
<br/>
<span class="constants"></span></span><span class="options-container">,
<br/>
<span class="options"></span></span>);
<br/>
result = fn(<span class="vars"></span>);
</div>
<div class='result'>Result:</div>
</div>
</div>
</body>
</html>