Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@estilles/expression-parser

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@estilles/expression-parser

Customizable Infix to Postfix Converter/Resolver

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
23
Maintainers
1
Weekly downloads
 
Created
Source

expression-parser

Customizable Infix to Postfix Converter/Resolver

expression-parser is yet another infix to postfix/reverse polish notation converter. I created it because I needed the ability to parse expressions with symbols/identifiers/variables of different formats.

Highlights

Supports the following expression tokens:

  • basic binary operators (e.g. +,-,*,/,%,^)
  • unary negation operator (e.g. -10)
  • parenthesis for grouping

Supports the following operands:

  • Integers (e.g. -10,1,0,1,2,3,...)
  • Floating point numbers (e.g. 3.14159)
  • Symbols in the form if singe upper/lower case letters (e.g. a+b-c)
  • Custom symbom/variable patters (specify your own regular expression)

Detects the following expression errors:

  • Too many operands
  • Missing operands
  • Missing opening parenthesis
  • Missing closing parenthesis

Is thoughrouly unit tested.

Installation

npm install @estilles/expression-parser

Usage

Importing

import { parse, resolve, toString, toArray } from '@estilles/expression-parser'

or

const { parse, resolve, toString, toArray } = require('@estilles/expression-parser')

API

parse()

The parse() function takes a mathematica expression, tokenizes and parses it, then returns a parsed expression object that represents the expression in postfix or reversed polish notation (RPN).

The resulting postfix/RPN expression can be visualized by converting it to a string using toString(parsedExpression) or an array of tokens using toArray(parsedExpression) (see below).

The optional symbolPattern is a regular expression used to parse symbols/variables within the expression. The default symbolPattern is /^([a-zA-Z])/. It will accept single upper or lower case letters. If you want to create your symbolPattern, the regular expression must: (a) start with a begin anchor ^, and (b) capture the name of the symbol/variable in the first capture group.

parse(expression : String[, symbolPattern : Regex]) => parsedExpression

e.g.
parse('1+2*3/4')
parse('(a+b)^2*c')
parse('$(instance:var1) + $(instance:var2)', /^\$\(((?:[^:$)]+):(?:[^)$]+))\)/)
toString()

Converts a parsed expression to a string

toString(parsedExpression) => string

e.g.
toString(parse('1+2*3/4')) => '1 2 3 * 4 / 1 +'
toString(parse('(a+b)^2*c')) => 'a b + 2 ^ c *'
toArray()

Converts a parsed expression to an array of tokens

toArray(parsedExpression) => array

e.g.
toString(parse('1+2*3/4')) => ['1', '2', '3', '*', '4', '/', '1', '+']
toString(parse('(a+b)^2*c')) => ['a', 'b', '+', '2', '^', 'c', '*']
resolve(parsedExpression[, values]) -> result

Takes a parsed expression and an optional dictionary of symbol -> values, evaluates the expression and returns the result.

resolve(parsedExpression[, values]) => result

e.g.
resolve(parse('1+2*3/4')) => 2.5
resolve(parse('(a+b)^2*c), { a: 1, b: 2, c: 3}) => 27

FAQs

Package last updated on 28 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc