Skip to content

Commit a556e27

Browse files
authored
Merge pull request #238 from Akumzy/patch-1
Added syntax highlighting support for JavaScript
2 parents b5e3d61 + 3539a95 commit a556e27

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

README.md

+34-34
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ Installation
2323

2424
Basic Usage
2525
-------------------------------------
26+
```js
27+
const Parser = require('expr-eval').Parser;
2628

27-
var Parser = require('expr-eval').Parser;
28-
29-
var parser = new Parser();
30-
var expr = parser.parse('2 * x + 1');
29+
const parser = new Parser();
30+
let expr = parser.parse('2 * x + 1');
3131
console.log(expr.evaluate({ x: 3 })); // 7
3232

3333
// or
3434
Parser.evaluate('6 * x', { x: 7 }) // 42
35-
35+
```
3636
Documentation
3737
-------------------------------------
3838

@@ -69,8 +69,8 @@ Constructs a new `Parser` instance.
6969
The constructor takes an optional `options` parameter that allows you to enable or disable operators.
7070

7171
For example, the following will create a `Parser` that does not allow comparison or logical operators, but does allow `in`:
72-
73-
var parser = new Parser({
72+
```js
73+
const parser = new Parser({
7474
operators: {
7575
// These default to true, but are included to be explicit
7676
add: true,
@@ -92,7 +92,7 @@ For example, the following will create a `Parser` that does not allow comparison
9292
assignment: false
9393
}
9494
});
95-
95+
```
9696
#### parse(expression: string)
9797

9898
Convert a mathematical expression into an `Expression` object.
@@ -122,25 +122,25 @@ Evaluate the expression, with variables bound to the values in {variables}. Each
122122
variable in the expression is bound to the corresponding member of the
123123
`variables` object. If there are unbound variables, `evaluate` will throw an
124124
exception.
125-
125+
```js
126126
js> expr = Parser.parse("2 ^ x");
127127
(2^x)
128128
js> expr.evaluate({ x: 3 });
129129
8
130-
130+
```
131131
#### substitute(variable: string, expression: Expression | string | number)
132132

133133
Create a new `Expression` with the specified variable replaced with another
134134
expression. This is similar to function composition. If `expression` is a string
135135
or number, it will be parsed into an `Expression`.
136-
136+
```js
137137
js> expr = Parser.parse("2 * x + 1");
138138
((2*x)+1)
139139
js> expr.substitute("x", "4 * x");
140140
((2*(4*x))+1)
141141
js> expr2.evaluate({ x: 3 });
142142
25
143-
143+
```
144144
#### simplify(variables: object)
145145

146146
Simplify constant sub-expressions and replace variable references with literal
@@ -154,37 +154,37 @@ multiplication are associative, so `((2*(4*x))+1)` from the previous example
154154
cannot be simplified unless you provide a value for x. `2*4*x+1` can however,
155155
because it’s parsed as `(((2*4)*x)+1)`, so the `(2*4)` sub-expression will be
156156
replaced with "8", resulting in `((8*x)+1)`.
157-
157+
```js
158158
js> expr = Parser.parse("x * (y * atan(1))").simplify({ y: 4 });
159159
(x*3.141592653589793)
160160
js> expr.evaluate({ x: 2 });
161161
6.283185307179586
162-
162+
```
163163
#### variables(options?: object)
164164

165165
Get an array of the unbound variables in the expression.
166-
166+
```js
167167
js> expr = Parser.parse("x * (y * atan(1))");
168168
(x*(y*atan(1)))
169169
js> expr.variables();
170170
x,y
171171
js> expr.simplify({ y: 4 }).variables();
172172
x
173-
173+
```
174174
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']`.
175175

176176
#### symbols(options?: object)
177177

178178
Get an array of variables, including any built-in functions used in the
179179
expression.
180-
180+
```js
181181
js> expr = Parser.parse("min(x, y, z)");
182182
(min(x, y, z))
183183
js> expr.symbols();
184184
min,x,y,z
185185
js> expr.simplify({ y: 4, z: 5 }).symbols();
186186
min,x
187-
187+
```
188188
Like `variables`, `symbols` accepts an option argument `{ withMembers: true }` to include object members.
189189

190190
#### toString()
@@ -200,7 +200,7 @@ is an array of parameter names, or a string, with the names separated by commas.
200200

201201
If the optional `variables` argument is provided, the expression will be
202202
simplified with variables bound to the supplied values.
203-
203+
```js
204204
js> expr = Parser.parse("x + y + z");
205205
((x + y) + z)
206206
js> f = expr.toJSFunction("x,y,z");
@@ -211,7 +211,7 @@ simplified with variables bound to the supplied values.
211211
[Function] // function (y, z) { return 100 + y + z; };
212212
js> f(2, 3)
213213
105
214-
214+
```
215215
### Expression Syntax ###
216216

217217
The parser accepts a pretty basic grammar. It's similar to normal JavaScript
@@ -235,15 +235,15 @@ or | Left | Logical OR
235235
x ? y : z | Right | Ternary conditional (if x then y else z)
236236
= | Right | Variable assignment
237237
; | Left | Expression separator
238-
239-
var parser = new Parser({
238+
```js
239+
const parser = new Parser({
240240
operators: {
241241
'in': true,
242242
'assignment': true
243243
}
244244
});
245245
// Now parser supports 'x in array' and 'y = 2*x' expressions
246-
246+
```
247247
#### Unary operators
248248

249249
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
326326
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.
327327

328328
Examples:
329-
329+
```js
330330
square(x) = x*x
331331
add(a, b) = a + b
332332
factorial(x) = x < 2 ? 1 : x * factorial(x - 1)
333-
333+
```
334334
#### Custom JavaScript functions
335335

336336
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:
337-
338-
var parser = new Parser();
337+
```js
338+
const parser = new Parser();
339339

340340
// Add a new function
341341
parser.functions.customAddFunction = function (arg1, arg2) {
@@ -347,7 +347,7 @@ If you need additional functions that aren't supported out of the box, you can e
347347

348348
parser.evaluate('customAddFunction(2, 4) == 6'); // true
349349
//parser.evaluate('fac(3)'); // This will fail
350-
350+
```
351351
#### Constants
352352

353353
The parser also includes a number of pre-defined constants that can be used in expressions. These are shown
@@ -362,17 +362,17 @@ false | Logical `false` value
362362

363363
Pre-defined constants are stored in `parser.consts`. You can make changes to this property to customise the
364364
constants available to your expressions. For example:
365-
366-
var parser = new Parser();
365+
```js
366+
const parser = new Parser();
367367
parser.consts.R = 1.234;
368368

369369
console.log(parser.parse('A+B/R').toString()); // ((A + B) / 1.234)
370-
370+
```
371371
To disable the pre-defined constants, you can replace or delete `parser.consts`:
372-
373-
var parser = new Parser();
372+
```js
373+
const parser = new Parser();
374374
parser.consts = {};
375-
375+
```
376376

377377
### Tests ###
378378

0 commit comments

Comments
 (0)