Skip to main content

:heavy_division_sign: - MEP is a dependency free mathematical expression parser written in TypeScript.

Why ?

I needed an extensible mathematical expression parser for 1Calc

This package does the following:

  • Lex mathematical expressions with custom operations, constants and functions into tokens
  • Parse tokens and return an RPN array of numbers / Operation.
  • Compute an RPN array and return the result

Installation

Using ES modules:

import * mep from "https://deno.land/x/http_ece@v0.1.0/mod.ts";

Using npm:

$ npm install @negrel/mep

You can now import mep as an ES modules or a CommonJS module.

Exemple

import { compute, registerConstant } from "@negrel/mep";
// or
// const { compute, registerConstant } = require('@negrel/mep')

console.log("Result:", compute("log2(5 * 65 + cos(PI ^ 2))"));
// Result: 8.340283256791498

registerConstant("PI2", Math.PI ** 2);
console.log("Result:", compute("log2(5 * 65 + cos(PI2))"));
// Result: 8.340283256791498

Contributing

If you want to contribute to MEP to add a feature or improve the code contact me at negrel.dev@protonmail.com, open an issue or make a pull request.

:stars: Show your support

Please give a ⭐ if this project helped you!

buy me a coffee

📜 License

MIT © Alexandre Negrel