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

shunting-yard.js

v2.0.0

Published

The shunting yard algorithm transforms a mathematical expression from infix notation into a reversed polish notation (postfix). This is a really extensible implementation.

Downloads

6

Readme

Shunting Yard Algorithm Build Status

The shunting yard algorithm transforms a mathematical expression from infix notation into a reversed polish notation (postfix).

An more in depth introduction to this algorithm you can get on Wikipedia.

Implementation details

if used with a string

This implementation of the shunting yard algorithm takes a String in and puts out an array representation in reversed polish notation (RPN). The resulting RPN you can easily processed (using the ShuntingYard.resolveRpn function) to a resulting number.

Note that ShuntingYard.parse will ignore every whitespace of the input string. Strings formatted like '1 100 + 5' will result in the array containing those tokens: [1100, 5, +] – which also allows for nice formatting of bigger number, but could lead to problems if there was meant an operator between 1 and 100.

if used with an input array

Used with an array of tokens, you have some more flexibility, because the operator tokens can be longer then single characters.

In both cases, you have to define which operators will be in your input. That is, because the algorithm has to know the precedence and associativity of the operators. Predefined are those common operators: +, -, *, / and ^.

Beware that your tokens don't contain whitespaces at the beginning or the end, so "+" is not the same as " +".

Example

const shuntingYard = new ShuntingYard();
const rpn = shuntingYard.parse('10+3*3'); // rpn is now: ['10', '3', '3', '*', '+']
const result = shuntingYard.resolveRPN(rpn); // result is now: 19
// or in short:
const result2 = shuntingYard.resolve(rpn); // result is now: 19

If you want, you could also use eval for this string but nobody would like to have eval in their code (considering what eval is doing to your parser and the security implications you have to be aware of).

But the real benefit of this way is when defining your own Operators. Just like that:

const shuntingYard = new ShuntingYard();
shuntingYard.addOperator('o', new Operator('o', 2, 'left', 2, function(a, b) { return a + Math.sqrt(b); }));
var rpn = shuntingYard.parse('10o3*3'); // rpn is now: ['10', '3', '3', '*', 'o']
var result = shuntingYard.resolveRPN(rpn); // result is now: 13

You can also use an array as input:

shuntingYard.addOperator('add', new Operator('add', 2, 'left', 2, function(a, b) { return a + b; }));
var rpn = shuntingYard.parse([5, 'add', 4, '*', 4]); // rpn is now: [5, 4, 4, '*', 'add']
var result = shuntingYard.resolveRPN(rpn); // result is now: 21

And you can define new functions (those do not have to be infix):

shuntingYard.addOperator('sqrt', new Operator('sqrt', 1, function(a) { return Math.sqrt(a); }));
var rpn = shuntingYard.parse([6, '+', 'sqrt', '(', 6, '+', 3 ')']); // rpn is now: ['6', '6', '3', '+', 'sqrt', '+']
var result = shuntingYard.resolveRPN(rpn); // result is now: 9

TODOs

For resolveRPN:

  • change tokenizer from '' to whitespace for example
  • change operators

MIT License

Copyright (C) 2013-2017 Georg Tavonius

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.