npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@estilles/expression-parser

v1.0.0

Published

Customizable Infix to Postfix Converter/Resolver

Downloads

7

Readme

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