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 🙏

© 2025 – Pkg Stats / Ryan Hefner

polish-engine

v1.0.2

Published

Polish notation math engine for evaluating complex mathematical expressions.

Readme

Polish Engine

A lightweight, TypeScript-based math expression evaluator that uses Reverse Polish Notation (RPN) for accurate and extensible computation.

Features

  • Converts infix expressions to Reverse Polish Notation (RPN).
  • Evaluates real and complex numbers using complex.js.
  • Supports custom functions, variables, and operator precedence.
  • Written entirely in TypeScript for strong typing and easy integration.
  • Unit tested with vitest.

Installation

From npm

npm install polish-engine

Or from GitHub repository

git clone https://github.com/JVenturaDev/JVPolishEngine.git
cd polish-engine
npm install

Usage

Example 1: Basic Arithmetic

import { polishEngine } from "polish-engine";

const engine = new polishEngine();

console.log(engine.evaluate("2 + 3 * 4")); // 14
console.log(engine.evaluate("(2 + 3) * 4")); // 20

Example 2: Using Variables

console.log(engine.evaluate("a^2 + b^2", { a: 3, b: 4 })); // 25

Example 3: Complex Numbers

console.log(engine.evaluate("sqrt(-9)")); //3i

Architecture Overview

The project is modularized into independent components:

| Module | Description | |--------|--------------| | tokenizer.ts | Splits expressions into tokens. | | polish-parser.ts | Converts tokens from infix to postfix (RPN). | | PreprocessModule.ts | Normalizes and transforms mathematical expressions — replaces symbols, standardizes syntax, and prepares input for tokenization..| | polish-evaluator.ts | Evaluates the postfix expressions. | | functionsModule.ts | Handles built-in and custom functions. | | polish-engine.ts | High-level API for developers. |

Class Diagram

polish-engine/
├── src/
│   ├── polish-engine.ts     # Main class that ties everything together
│   ├── tokenizer.ts         # Expression tokenizer
│   ├── polish-parser.ts     # Converts infix expressions to RPN
│   ├── polish-evaluator.ts  # Evaluates RPN and functions
│   ├── PreprocessModule.ts  # Normalizes and transforms expressions (symbols → functions)
│   └── functionsModule.ts   # Custom mathematical functions
│
├── tests/
│   ├── tokenizer.test.ts
│   ├── parser.test.ts
│   ├── evaluator.test.ts
│   └── polish-engine.test.ts
│
├── package.json
├── tsconfig.json
├── vitest.config.ts
├── LICENCE
└── README.md

Example Integration

You can easily integrate polishEngine into your calculator, educational, or simulation projects.

const result = engine.evaluate("sin(pi / 2) + sqrt(16)");
console.log(result); // 5

Running Tests

Run all unit tests using vitest:

npm test

Example output:

 PASS  tests/polish-engine.test.ts
  polishEngine
    ✓ should evaluate a simple expression
    ✓ should handle parentheses correctly
    ✓ should calculate factorial

Tech Stack

  • Language: TypeScript
  • Testing: vitest
  • Math Library: complex.js
  • Module Format: ES Modules

Example File: polish-engine.ts

import { Tokenizer } from "./tokenizer";
import { parser } from "./polish-parser";
import { evaluator } from "./polish-evaluator";
import { preprocessExpression } from "./PreprocessModule";
import Complex from "complex.js";

export class polishEngine {
    private tokenizer = new Tokenizer();
    private Parser = new parser(this.tokenizer);
    private Evaluator = new evaluator();

    evaluate(expression: string, variables?: Record<string, number>) {
        const preprocessed = preprocessExpression(expression);
        const tokens = this.tokenizer.tokenize(preprocessed);
        const rpn = this.Parser.toPostFix(tokens);
        const result = this.Evaluator.evaluatePostFix(rpn, variables);

        if (result instanceof Complex) {
            if (result.im === 0) return result.re;
            return result;
        }
        return result;
    }
}

Author

Jonathan Ventura GitHub: JVenturaDev


License

This project is licensed under the GNU General Public License v3.0 (GPL-3.0) — see the LICENSE file for full details.

© 2025 Jonathan Ventura. You are free to use, modify, and distribute this software under the same license terms, provided that proper attribution and a copy of the GPL v3 are included.


Contributing

  1. Fork this repository.
  2. Create a new branch: git checkout -b feature/new-feature.
  3. Commit your changes: git commit -m "Add new feature".
  4. Push to the branch: git push origin feature/new-feature.
  5. Create a Pull Request.

Contributions are welcome!