essy-evaluator
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "essy-evaluator", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Parses and evaluates mathematical expressions from Javascript strings.", | ||
@@ -5,0 +5,0 @@ "main": "dist/bundle.js", |
103
README.md
@@ -11,4 +11,4 @@ Javascript Expression Parser and Evaluator | ||
The module was created during development of | ||
[Essy Tree](https://essytree.com), online decision tree | ||
The module was created during development of | ||
[Essy Tree](https://essytree.com), online decision tree | ||
analysis software, where it is used in production. | ||
@@ -27,7 +27,18 @@ | ||
var evaluator = new essy.Evaluator(); | ||
var tokens = parser.parse('2 * 3'); | ||
var tokens; | ||
// Use built-in operators. | ||
tokens = parser.parse('2 * 3'); | ||
console.log(evaluator.evaluate(tokens)); // 6 | ||
Documentation | ||
// Use built-in functions. | ||
tokens = parser.parse('max(1, 3, 2) + 4'); | ||
console.log(evaluator.evaluate(tokens)); // 7 | ||
// Use custom definitions. | ||
tokens = parser.parse('myCustomName / 4'); | ||
evaluator.defineName('myCustomName', 8); | ||
console.log(evaluator.evaluate(tokens)); // 2 | ||
Documentation | ||
------------------------------------- | ||
@@ -41,3 +52,3 @@ | ||
Constructs a new `Parser` instance. Accepts an optional configuration object that can | ||
be used to disable default operators or add new operators. For example, the following | ||
be used to disable default operators or add new operators. For example, the following | ||
disables the default "+" operator and adds two custom operators, "#" and "$". | ||
@@ -47,6 +58,6 @@ | ||
operators: { | ||
// Disable default "+" operator. | ||
'+': false, | ||
// Add custom "#" and "$" operators. | ||
@@ -83,12 +94,12 @@ '#': true, | ||
#### parse (expression {String}) | ||
Parses an expression into tokens. `parse()` returns an array of simple token objects, | ||
each with properties `type {String}` and `value {Any}`. The token type can be one of the | ||
Parses an expression into tokens. `parse()` returns an array of simple token objects, | ||
each with properties `type {String}` and `value {Any}`. The token type can be one of the | ||
following: | ||
- __name__ All strings not enclosed in quotes are converted to name tokens. The value is | ||
- __name__ All strings not enclosed in quotes are converted to name tokens. The value is | ||
equal to the name. | ||
- __number__ Numbers represented in decimal or scientific notiation. | ||
- __operator__ Any operator defined in the operator list. | ||
- __operator__ Any operator defined in the operator list. | ||
- __string__ Any string encapsulated in quotes. | ||
`parse()` will throw a `ParseException` in the following cases: | ||
@@ -98,15 +109,15 @@ | ||
- Invalid trailing operator | ||
- Unrecognized operator | ||
- Unterminated string | ||
- Unrecognized operator | ||
- Unterminated string | ||
#### updateOperators (operators {Object}) | ||
Updates valid operator list. Keys should be the operator (e.g., '+') and values indicate | ||
Updates valid operator list. Keys should be the operator (e.g., '+') and values indicate | ||
whether the operator is valid. Values can be "truthy" or "falsey". | ||
parser.updateOperators({ | ||
// Disable some built-in operators. false or 0 work just as well. | ||
'+': false, | ||
'*': 0, | ||
// Add some custom operators. | ||
@@ -119,4 +130,4 @@ '$': true, | ||
### Evaluator ### | ||
The Evaluator class provides methods to evaluate an array of tokens returned from | ||
`Parser.parse()` as well as define custom operators, functions, and names. See below | ||
The Evaluator class provides methods to evaluate an array of tokens returned from | ||
`Parser.parse()` as well as define custom operators, functions, and names. See below | ||
the methods section for lists of pre-defined constants, operators, and functions. | ||
@@ -127,3 +138,3 @@ | ||
##### evaluate (tokens {Object[]}) | ||
Evaluates array of tokens returned from `Parser.parse()` and returns result, typically | ||
Evaluates array of tokens returned from `Parser.parse()` and returns result, typically | ||
a number. | ||
@@ -138,4 +149,4 @@ | ||
##### defineFunction (name {String}, ev {Function} [, noArgs {Boolean}]) | ||
Defines a custom function. The `name` is the name for the function and `ev` is | ||
the function body. The `noArgs` flag should be set to true if the function does | ||
Defines a custom function. The `name` is the name for the function and `ev` is | ||
the function body. The `noArgs` flag should be set to true if the function does | ||
not accept any arguments; by default this value is false. | ||
@@ -147,11 +158,11 @@ | ||
var tokens = parser.parse('addTwoNumbers(2, 3)'); | ||
evaluator.defineFunction('addTwoNumbers', function () { | ||
return this.argValue(0) + this.argValue(1); | ||
}); | ||
console.log(evaluator.evaluate(tokens)); // 5 | ||
As seen above, the `ev` function has access to provided argument values | ||
via the `argValue()` method, which accepts an argument index. In the above, | ||
As seen above, the `ev` function has access to provided argument values | ||
via the `argValue()` method, which accepts an argument index. In the above, | ||
`argValue(0) === 2` and `argValue(1) === 3`. | ||
@@ -165,22 +176,22 @@ | ||
var tokens = parser.parse('addNumbers(1,2,3,4)'); | ||
evaluator.defineFunction('addNumbers', function () { | ||
var sum = 0, | ||
values = this.argValues(); | ||
for (var i = 0; i < values.length; i++) { | ||
sum += values[i]; | ||
} | ||
return values; | ||
}); | ||
console.log(evaluator.evaluate(tokens)); // 10 | ||
The above makes use of the `argValues()` method, which evaluates and returns all | ||
The above makes use of the `argValues()` method, which evaluates and returns all | ||
argument values. | ||
##### defineName (name {String}, value {Any}) | ||
Defines a custom name. This can be useful if you want to define custom constant | ||
Defines a custom name. This can be useful if you want to define custom constant | ||
values or include variables in your expressions. | ||
@@ -192,5 +203,5 @@ | ||
var tokens = parser.parse('3 + myCustomName'); | ||
evaluator.defineName('myCustomName', 4); | ||
console.log(evaluator.evaluate(tokens)); // 7 | ||
@@ -200,3 +211,3 @@ | ||
#### Pre-defined Constants | ||
#### Pre-defined Constants | ||
`Evaluator` defines the following constants by default: | ||
@@ -208,14 +219,14 @@ | ||
FALSE | False | 0 | ||
LN2 | Natural logarithm of 2 | ~0.693 | ||
LN10 | Natural logartihm of 10 | ~2.302 | ||
PI | Pi | ~3.145 | ||
SQRT1_2 | 1 over the square root of 2 | ~0.707 | ||
SQRT2 | Square root of 2 | ~1.414 | ||
LN2 | Natural logarithm of 2 | ~0.693 | ||
LN10 | Natural logartihm of 10 | ~2.302 | ||
PI | Pi | ~3.145 | ||
SQRT1_2 | 1 over the square root of 2 | ~0.707 | ||
SQRT2 | Square root of 2 | ~1.414 | ||
TRUE | True | 1 | ||
#### Pre-defined Operators | ||
#### Pre-defined Operators | ||
`Evaluator` defines the following operators by default: | ||
Operator | Example | Description | Returns | ||
Operator | Example | Description | Returns | ||
:------- | :---------- | :------------- | :------ | ||
@@ -270,3 +281,1 @@ \+ | x + y | Addition | Sum of x and y. | ||
tan(x) | Returns the tangent of x. | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49010
267