Giter VIP home page Giter VIP logo

ngx-expression-builder's Introduction

ng-expression-builder

A minimalistic Angular package for evaluating mathematical expression and creating expression tree from infix notation

NPM JavaScript Style Guide

Install

npm install --save ngx-expression-builder

Usage

Include the service NgExpressionBuilderService into the providers array of corresponding module or component

import { NgExpressionBuilderService } from 'ng-expression-builder';

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
  ],
  providers: [NgExpressionBuilderService], // here
  exports: [
    ...
  ]
})
export class MyModule { }
@Component({
  selector: 'my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.scss'],
  providers: [NgExpressionBuilderService] // here
})
export class MyComponent { ... }

Now inject the service into the corresponding Angular component through constructor

constructor(private readonly ngExpressionBuilderService: NgExpressionBuilderService) { ... }

There are only two public methods:

Name Input Output Description
generateExpressionTree infix expression (string) expression tree structure Converts the infix expression into a binary tree structure
evaluateExpression infix expression (string) value (number) Evaluates the value of the expression

Example

const infixExpression: string = '((2*(6-1))/2+5)*4';
const answer = this.ngExpressionBuilderService.evaluateExpression(infixExpression);
console.log(answer);

// Output:
40
const infixExpression: string = '(a + b) > 10 AND (c * (-5))/100';
const tree = this.ngExpressionBuilderService.generateExpressionTree(this.rawQueryString);
console.log(tree);

// Output:

{
    "operatorSymbol": "AND",
    "operatorName": "And",
    "children": [
        {
            "value": {
                "operatorSymbol": ">",
                "operatorName": "Greater than",
                "children": [
                    {
                        "value": {
                            "operatorSymbol": "+",
                            "operatorName": "Add",
                            "children": [
                                {
                                    "value": "a",
                                    "nodeType": "Left node",
                                    "type": "string"
                                },
                                {
                                    "value": "b",
                                    "nodeType": "Right node",
                                    "type": "string"
                                }
                            ]
                        },
                        "nodeType": "Left node",
                        "type": "Node"
                    },
                    {
                        "value": 10,
                        "nodeType": "Right node",
                        "type": "number"
                    }
                ]
            },
            "nodeType": "Left node",
            "type": "Node"
        },
        {
            "value": {
                "operatorSymbol": "/",
                "operatorName": "Divide",
                "children": [
                    {
                        "value": {
                            "operatorSymbol": "*",
                            "operatorName": "Multiply",
                            "children": [
                                {
                                    "value": "c",
                                    "nodeType": "Left node",
                                    "type": "string"
                                },
                                {
                                    "value": -5,
                                    "nodeType": "Right node",
                                    "type": "number"
                                }
                            ]
                        },
                        "nodeType": "Left node",
                        "type": "Node"
                    },
                    {
                        "value": 100,
                        "nodeType": "Right node",
                        "type": "number"
                    }
                ]
            },
            "nodeType": "Right node",
            "type": "Node"
        }
    ]
}

ngx-expression-builder's People

Contributors

trijit957 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.