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

infix-postfix

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

infix-postfix

infix to postfix converter

  • 2.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

infix-postfix

This package allows you to convert infix expressions to postfix expressions.

Installation

npm install infix-postfix

Usage

Importing
import infixToPostfix from "infix-postfix";

or

const infixToPostfix = require("infix-postfix");
Converting
console.log(infixToPostfix("(a+2b)^-2"));

Examples

2+3*42 3 4 * +
-12-312 − 3 -
12^-212 2 − ^
((2cm+112)*12.2/3)^22 cm * 112 + 12.2 * 3 / 2 ^

Features

Operator parsing

+, -, *, /, (), ^ get parsed to + (addition), - (subtraction), (negation), * (multiplication), / (division) and ^ (exponentiation). The difference between - and is, that (negation) is an unary operator (e.g. -4 gets parsed to 4 −) and - (subtraction) is a binary operator (e.g. 2-6 gets parsed to 2 6 -). There is no unary + operator, because one can simply ignore such for mathematical reasons.

Operator reduction

Unnecessary operators get removed automatically. For example ---+4 gets parsed to 4 −, +(12) gets parsed to 12.

Variable support

Any variable not containing operator characters (+, -, ...) is supported. For example a-6 gets parsed to a 6 -.

Variable multiplication shortcut support

A common parser would parse b/10b to b 10b / - which is unfortunately wrong. This parser recognizes that 10 and b are different operands. It parses the string to b 10 b * /. Added to that a b gets parsed to a b *.

Behind the scenes

To understand how this converter works set the function's second argument to true: infixToPostfix("2*2", true);. This will print every step in detail to the console.

Keywords

FAQs

Package last updated on 05 Aug 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