🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

expression-calculator

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

expression-calculator - npm Package Compare versions

Comparing version
1.0.0
to
1.0.1
+1
-1
package.json
{
"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.

@@ -101,2 +101,5 @@ import {

getRPN(){
if(!this.__data.length){
return null;
}
return this.__data.map((item)=>({...item}));

@@ -103,0 +106,0 @@ }

@@ -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 @@ });