You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Get an array of the unbound variables in the expression.
166
-
166
+
```js
167
167
js> expr =Parser.parse("x * (y * atan(1))");
168
168
(x*(y*atan(1)))
169
169
js>expr.variables();
170
170
x,y
171
171
js>expr.simplify({ y:4 }).variables();
172
172
x
173
-
173
+
```
174
174
By default, `variables` will return "top-level" objects, so for example, `Parser.parse(x.y.z).variables()` returns `['x']`. If you want to get the whole chain of object members, you can call it with `{ withMembers: true }`. So `Parser.parse(x.y.z).variables({ withMembers: true })` would return `['x.y.z']`.
175
175
176
176
#### symbols(options?: object)
177
177
178
178
Get an array of variables, including any built-in functions used in the
179
179
expression.
180
-
180
+
```js
181
181
js> expr =Parser.parse("min(x, y, z)");
182
182
(min(x, y, z))
183
183
js>expr.symbols();
184
184
min,x,y,z
185
185
js>expr.simplify({ y:4, z:5 }).symbols();
186
186
min,x
187
-
187
+
```
188
188
Like `variables`, `symbols` accepts an option argument `{ withMembers: true }` to include object members.
189
189
190
190
#### toString()
@@ -200,7 +200,7 @@ is an array of parameter names, or a string, with the names separated by commas.
200
200
201
201
If the optional `variables` argument is provided, the expression will be
202
202
simplified with variables bound to the supplied values.
203
-
203
+
```js
204
204
js> expr =Parser.parse("x + y + z");
205
205
((x + y) + z)
206
206
js> f =expr.toJSFunction("x,y,z");
@@ -211,7 +211,7 @@ simplified with variables bound to the supplied values.
211
211
[Function] // function (y, z) { return 100 + y + z; };
212
212
js>f(2, 3)
213
213
105
214
-
214
+
```
215
215
### Expression Syntax ###
216
216
217
217
The parser accepts a pretty basic grammar. It's similar to normal JavaScript
@@ -235,15 +235,15 @@ or | Left | Logical OR
235
235
x ? y : z | Right | Ternary conditional (if x then y else z)
236
236
= | Right | Variable assignment
237
237
; | Left | Expression separator
238
-
239
-
var parser = new Parser({
238
+
```js
239
+
constparser=newParser({
240
240
operators: {
241
241
'in':true,
242
242
'assignment':true
243
243
}
244
244
});
245
245
// Now parser supports 'x in array' and 'y = 2*x' expressions
246
-
246
+
```
247
247
#### Unary operators
248
248
249
249
The parser has several built-in "functions" that are actually unary operators.
@@ -326,16 +326,16 @@ Arrays can be created by including the elements inside square `[]` brackets, sep
326
326
You can define functions using the syntax `name(params) = expression`. When it's evaluated, the name will be added to the passed in scope as a function. You can call it later in the expression, or make it available to other expressions by re-using the same scope object. Functions can support multiple parameters, separated by commas.
327
327
328
328
Examples:
329
-
329
+
```js
330
330
square(x) = x*x
331
331
add(a, b) = a + b
332
332
factorial(x) = x <2?1: x *factorial(x -1)
333
-
333
+
```
334
334
#### Custom JavaScript functions
335
335
336
336
If you need additional functions that aren't supported out of the box, you can easily add them in your own code. Instances of the `Parser` class have a property called `functions` that's simply an object with all the functions that are in scope. You can add, replace, or delete any of the properties to customize what's available in the expressions. For example:
0 commit comments