expression-calculator
Advanced tools
+1
-1
| { | ||
| "name": "expression-calculator", | ||
| "version": "1.0.0", | ||
| "version": "1.0.1", | ||
| "description": "Calculate expressions without using `eval()`, using LL(1) syntax analyzer.", | ||
@@ -5,0 +5,0 @@ "main": "dist/exprcalc.js", |
+58
-31
@@ -8,15 +8,42 @@ expression-calculator | ||
| API Documentation | ||
| ----------------- | ||
| Installation | ||
| ------------ | ||
| ### Initialization | ||
| ES6/Webpack | ||
| const calc = new Calc(); | ||
| const calc = new Calc(expression); | ||
| const calc = new Calc(RPN); | ||
| import Calc from 'expression-calculator'; | ||
| Node.js | ||
| const Calc=require('expression-calculator'); | ||
| Script Tag | ||
| <script type='text/javascript' src='path/to/exprcalc.js'></script> | ||
| Initialization | ||
| -------------- | ||
| ### `const calc = new Calc();` | ||
| Initialize a new instance of `Calc()` with empty RPN expression. | ||
| ### `const calc = new Calc(expression);` | ||
| Initialize a new instance of `Calc()`, then compile the given expression into RPN (`compile()` method is called internally). | ||
| ### Methods | ||
| ### `const calc = new Calc(RPN);` | ||
| #### `compile(expr)` | ||
| Initialize a new instance of `Calc()`, then load the given RPN data (`setRPN()` method is called internally). | ||
| ### `const calc = new Calc(calcInstance);` | ||
| Get a copy of the given `Calc()` instance. | ||
| Methods | ||
| ------- | ||
| ### `compile(expr)` | ||
| Compile an expression into an RPN expression. | ||
@@ -26,7 +53,7 @@ | ||
| * `expr` - Expression to compile in string. | ||
| * `expr` - The string to be compiled. | ||
| **Returns** | ||
| A reference to this instance. | ||
| A reference to this instance for chaining. | ||
@@ -38,13 +65,11 @@ **Throws** | ||
| #### `calc(vars)` | ||
| ### `calc(vars)` | ||
| Calculate the previously compiled expression. | ||
| The compiled expression may contain variables, with default value 0. | ||
| The compiled expression may contain variables, use a key-value dict to specify values for variables in the expression. Variable names are case sensitive. | ||
| You may use a key-value dict to specify values for variables in the expression. Variable names are case sensitive. | ||
| **Parameters** | ||
| * `vars` - A dict contains key-value pairs (Optional). | ||
| * `vars` - A dict contains key-value pairs (Required when the RPN contains variables, and all the variables in the RPN expression must have a value here). | ||
@@ -55,2 +80,7 @@ **Returns** | ||
| **Throws** | ||
| * `TypeError` - When the value of a variable is not a number. | ||
| * `ReferenceError` - When the value of a variable is not given in the `vars` parameter. | ||
| **Example** | ||
@@ -63,3 +93,3 @@ | ||
| #### `getRPN()` | ||
| ### `getRPN()` | ||
@@ -70,3 +100,3 @@ Get the compiled RPN expression. | ||
| Returns the compiled RPN which can be converted into JSON, or `undefined` if no RPN is loaded or compiled. | ||
| Returns the compiled RPN which can be converted into JSON, or `null` for empty RPN. | ||
@@ -78,3 +108,3 @@ **Example** | ||
| The code above will get the following JSON data: | ||
| The code above will generate the following JSON data: | ||
@@ -109,3 +139,3 @@ [ | ||
| #### `setRPN(expr)` | ||
| ### `setRPN(expr)` | ||
@@ -120,21 +150,18 @@ Load previously compiled RPN from other source like database or memcache/redis. | ||
| A reference to this instance. | ||
| A reference to this instance for chaining. | ||
| **Throws** | ||
| * `TypeError` - If the given RPN contains tokens with invalid type or value. | ||
| * `SyntaxError` - If the given RPN contains invalid tokens or errors. | ||
| ### Aliases | ||
| ### `compress()` | ||
| #### `Calc.TOKEN_NUM` | ||
| Reduce the size of RPN by precalculating the numbers. | ||
| Mark RPN tokens as number type. | ||
| Aliases | ||
| ------- | ||
| #### `Calc.TOKEN_VAR` | ||
| Mark RPN tokens as variable type. | ||
| #### `Calc.TOKEN_OPER` | ||
| Mark RPN tokens as operand type. | ||
| * `Calc.TOKEN_NUM` - Mark RPN tokens as number type. | ||
| * `Calc.TOKEN_VAR` - Mark RPN tokens as variable type. | ||
| * `Calc.TOKEN_OPER` - Mark RPN tokens as operand type. |
+3
-0
@@ -101,2 +101,5 @@ import { | ||
| getRPN(){ | ||
| if(!this.__data.length){ | ||
| return null; | ||
| } | ||
| return this.__data.map((item)=>({...item})); | ||
@@ -103,0 +106,0 @@ } |
+1
-1
@@ -143,3 +143,3 @@ import assert from 'assert'; | ||
| let emptyRPN=new RPN().setRPN([]); | ||
| assert.deepStrictEqual(emptyRPN.getRPN(),[]); | ||
| assert.deepStrictEqual(emptyRPN.getRPN(),null); | ||
| assert.deepStrictEqual(emptyRPN.calc(),null); | ||
@@ -146,0 +146,0 @@ }); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
64780
1.66%1531
0.2%159
20.45%1
-50%