A simple expression parsing library that can customize various operators
Basic usage
install
npm install simple-expression-parsing --save-devyarn add simple-expression-parsing
use
;const instance = expression: "a+b";instance;
Browser Usage
install
<script src="./dist/index.js"></script>
use
const ExpressionParse = window;const instance = expression: "a+b";instance;
example
const instance = expression: "a+b";console;// {// "type": "BinaryExpression",// "left": {// "type": "Identifier",// "start": 0,// "end": 1,// "name": "a"// },// "operator": {// "type": "BinaryOperator",// "value": "+",// "start": 1,// "end": 2// },// "right": {// "type": "Identifier",// "start": 2,// "end": 3,// "name": "b"// }// }
const instance = expression: "a@@b";instance;console;// {// "type": "BinaryExpression",// "left": {// "type": "Identifier",// "start": 0,// "end": 1,// "name": "a"// },// "operator": {// "type": "BinaryOperator",// "value": "@@",// "start": 1,// "end": 3// },// "right": {// "type": "Identifier",// "start": 3,// "end": 4,// "name": "b"// }// }
docs
const instance = //The string you need to parse expression: "a+b" //Remove all default unary operators, replacing them with custom unary operators unaryOps: "@@@@@" //Remove all default binary operators, replacing them with custom binary operators //The value means the precedence of the operator. You can compare the priority of the default binary operator to determine your priority. binaryOps: "#####": 5 //Remove all default literals, replacing them with custom literals literals: this: "that" ;instance //Add an extra binary operator, not the same as the initialization time, will not delete the default binary operator //Add an extra unary operator //Add an extra unary literals //Remove the specified binary operators //Remove the specified unary operators //Remove the specified literals ;
default data
//default unary operatorsconst unaryOps = "!": true "-": true "+": true;//default binary operatorsconst binaryOps = "||": 1 "&&": 2 "|": 3 "^": 4 "&": 5 "==": 6 "!=": 6 "===": 6 "!==": 6 "<": 7 ">": 7 "<=": 7 ">=": 7 "<<": 8 ">>": 8 "+": 9 "-": 9 "*": 10 "/": 10 "%": 10;//default literalsconst literals = undefined: undefined null: null false: false true: true;
License
The simple-expression-parsing
is MIT licensed.