Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/harrydehix/infix-postfix
converts infix expressions to postfix expressions
https://github.com/harrydehix/infix-postfix
infix infix-postfix-converter infix-to-postfix postfix rpn
Last synced: 23 days ago
JSON representation
converts infix expressions to postfix expressions
- Host: GitHub
- URL: https://github.com/harrydehix/infix-postfix
- Owner: harrydehix
- Created: 2021-08-02T17:54:28.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-08-13T09:14:58.000Z (over 3 years ago)
- Last Synced: 2024-12-12T08:20:19.247Z (about 2 months ago)
- Topics: infix, infix-postfix-converter, infix-to-postfix, postfix, rpn
- Language: TypeScript
- Homepage:
- Size: 56.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# infix-postfix
This package allows you to convert infix expressions to postfix expressions.
## Installation
```bash
npm install infix-postfix
```
## Usage#### Importing
```typescript
import infixToPostfix from "infix-postfix";
```
or
```javascript
const infixToPostfix = require("infix-postfix");
```
#### Converting
##### to a string
```javascript
console.log(infixToPostfix("(a+2b)^-2").toString());/* Output:
a 2 b * + 2 − ^
*/
```
##### to an array
```javascript
console.log(infixToPostfix("(a+2b)^-2").toArray());/* Output:
[
'a', 2, 'b', '*',
'+', 2, '−', '^'
]
*/
```## Examples
`2+3*4` ▶ `2 3 4 * +`
`a b(3+4)2` ▶ `a b * 3 4 + * 2 *`
`-12-3` ▶ `12 − 3 -`
`12^-2` ▶ `12 2 − ^`
`((2cm+112)*12.2/3)^2` ▶ `2 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`.#### Number parsing support
Numbers get parsed. Obviously only works with the `toArray()` method.#### Variable support
Any variable not containing operator characters (`+`, `-`, ...) is supported. For example `a-6` gets parsed to `a 6 -`.#### 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 *`. Furthermore `a(3)2` is translated to `a 3 * 2 *`.