simple-expression-parsing
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

NPM version License Build Status Coverage Status

simple-expression-parsing

A simple expression parsing library that can customize various operators

Basic usage

install

npm install simple-expression-parsing --save-dev
yarn add simple-expression-parsing

use

import { ExpressionParse } from "simple-expression-parsing";
const instance = new ExpressionParse({
  expression: "a+b"
});
instance.getAst();

Browser Usage

install

<script src="./dist/index.js"></script>

use

const { ExpressionParse } = window;
const instance = new ExpressionParse({
  expression: "a+b"
});
instance.getAst();

example

const instance = new ExpressionParse({
  expression: "a+b"
});
console.log(instance.getAst());
// {
//     "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 = new ExpressionParse({
  expression: "a@@b"
});
instance.addBinaryOps({
  "@@": 6
});
console.log(instance.getAst());
// {
//     "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 = new ExpressionParse({
  //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
  .addBinaryOps({
    "##": 5
  })
  //Add an extra unary operator
  .addUnaryOps(["@@"])
  //Add an extra unary literals
  .addLiterals({
    hard: "hard"
  })
  //Remove the specified binary operators
  .removeBinaryOps(["##", "+"])
  //Remove the specified unary operators
  .removeUnaryOps(["@@"])
  //Remove the specified literals
  .removeLiterals(["hard"])
  .getAst();

default data

//default unary operators
const unaryOps = {
  "!": true,
  "-": true,
  "+": true
};
//default binary operators
const binaryOps = {
  "||": 1,
  "&&": 2,
  "|": 3,
  "^": 4,
  "&": 5,
  "==": 6,
  "!=": 6,
  "===": 6,
  "!==": 6,
  "<": 7,
  ">": 7,
  "<=": 7,
  ">=": 7,
  "<<": 8,
  ">>": 8,
  "+": 9,
  "-": 9,
  "*": 10,
  "/": 10,
  "%": 10
};
//default literals
const literals = {
  undefined: undefined,
  null: null,
  false: false,
  true: true
};

License

The simple-expression-parsing is MIT licensed.

Readme

Keywords

none

Package Sidebar

Install

npm i simple-expression-parsing

Weekly Downloads

3

Version

1.0.6

License

MIT

Unpacked Size

54.6 kB

Total Files

18

Last publish

Collaborators

  • xingzhichen