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 🙏

© 2026 – Pkg Stats / Ryan Hefner

math_engine

v1.0.2

Published

A math engine to parse and evaluate strings expressions.

Readme

Math engine guide

This package can be installed in every node project as well as react-native-cli and expo-cli. You can use this simple script to parse and get the result, not only the math result but also the steps trace,of string expressions.

Waring

Note that it is not going to work with the usual math writing e.g. 2+2-1, but with a special formatting (that is described below) where spaces are not allowed.

Installation

It's very easy to download, just use the following command.

npm install math_engine

Syntax

As said before, it needs a special syntax, but luckily it is very simple to get used to it. An example of expression can be: m[2,2]. Where the first letter m stays for multiplication, and the two numbers inside the brackets are the arguments of the operation. Just follow the rule (without spaces): operation id + [ + arg1 + , +arg2 +] Of course you can create multiple nested operations like: m[2,a[4,2]] is the equivalent of: 2 × (4 + 2) = 12.

Warning

Not all the operations takes two arguments. For example sine, cosine, square-root and others must contain only one number inside the brackets. For example below is reported a small table where you can find the square-root operation.

|Do| Don't| |---|---| |q[4]|q[4,4]|

Supported operations

|Name|Usage|Number of arguments| |---|---|---| |sum|a[arg1,arg2]|2| |subtraction|s[arg1,arg2]|2| |division|d[arg1,arg2]|2| |multiplication|m[arg1,arg2]|2| |power|p[arg1,arg2]|2| |square-root|q[arg]|1| |cosine|c[arg]|1| |sine|i[arg]|1| |tangent|t[arg]|1| |exponential|e[arg]|1| |logarithm (log10)|l[arg]|1| |binary AND|A[arg]|1| |binary OR|O[arg]|1| |binary NOT|N[arg]|1|

Using variables

This simple script allows you to use variables inside your expression. You must specify your variables inside the expression by adding 'x' + index.

const expression = 'm[x1,x2]';
const variables = [20,30]; // result => 20 × 30 => 600

Warning

The count for the index is starting from 1 and not from 0 (as usual in arrays)

Usage (javascript)

The javascript code is very simple, first you need to import the package by using:

import MathEngine from "math_engine";

Then define the expression to evaluate, and the variables (if they are necessary):

const expression = 'm[q[100],x1]';
const variables = [20]; // must be an array (can also be empty [] if you are not using variables);

Now, create the engine object by passing the expression, the variables array, and the language that is needed if you want to get all the math steps ( can only be 'it', 'en'). If it is empty, the script will use 'en' as default:

let engine = new MathEngine(expression, variables, 'en');

Now, (if you are using variables) call replace() on the engine object to replace the 'x1' (x2 , x3 ,x4...) with the corresponding variable (20):

engine.replace(); // this is not needed if you are not using variables. expression = 'm[q[100],20]'

Last step, call evaluate() (this is an async function that needs .then()) on the engine object to get an object that contains the result and the steps.

engine.evaluate().then((res) => {console.log(res.result); console.log(res.stepsTrace)}).catch((error) => {console.error(error)});

Full code

import MathEngine from "math_engine";
const expression = 'm[q[100],x1]';
const variables = [20]; // must be an array (can also be empty [] if you are not using variables);
let engine = new MathEngine(expression, variables, 'en');
engine.replace(); // this is not needed if you are not using variables. expression = 'm[q[100],20]'
engine.evaluate().then((res) => {console.log(res.result); console.log(res.stepsTrace)}).catch((error) => {console.error(error)});

Contact me

If you have any questions or suggestions please contact me at: [email protected]