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 🙏

© 2024 – Pkg Stats / Ryan Hefner

evalexp

v1.11.0

Published

Mathematical expression parser and evaluator

Downloads

21

Readme

evalexp

Mathematical expression parser and evaluator It accepts string expression. Then it creates parsed object and and evaluate it.

Library is not using eval()

It is parsing string, based on it, creates new expression and evaluating it.

  • It don't have access to global variables.
  • It is not creating global variables
  • It is only using operators and grammars defined by this library.
  • Everything else will throw error

Because parsing is complex, library is creating parsing object and when you need to use different variables with same expression it should be evaluated faster

Motivation

User writes input like this

"3+k6+2*strength"

and it should be evaluated on backend server and should be secure and safe

How to start

import EvalExp from "evalexp";
// or
const { EvalExp } = require("evalexp");

const evalExp = new EvalExp("3k6 + 2strength");
evalExp.parse();
evalExp.evaluate({
    k6: () => 3,
    strength: 2
}); //return number 13

or if you plan to evaluate expression only once

EvalExp.evaluate("3k6+2strength", {
    k6: () => 3,
    strength: 2
}); //return number 13

or from function

import { evaluate } from "evalexp";
//or
const { evaluate } = require("evalexp");

evaluate("3k6+2strength", {
    k6: () => 3,
    strength: 2
});

methods

  • EvalExp.evaluate(expression string, variable declaration) - evaluate expression and return value
  • new EvalExp() - create instance for expression
  • instance.parse() - parse expression
  • instance.evaluate(variable declaration) - evaluate expression and return value. Can be invoked multiple times with different variable declarations

evaluate one expression multiple times with different values

const evalExp = new EvalExp("x+1");
const someInput = [1, 2, 3, 4, 5, 6, 7];
someInput.map(x => evalExp.evaluate({ x })) // [2, 3, 4, 5, 6, 7, 8])

supported grammar

  • minus numbers:
EvalExp.evaluate("-5");
EvalExp.evaluate("-5*3");
  • plus numbers:
EvalExp.evaluate("5");
EvalExp.evaluate("+5");
  • floating numbers :
EvalExp.evaluate("4.345");
  • addition:
EvalExp.evaluate("5+4");
EvalExp.evaluate("+4+34");
EvalExp.evaluate("56.43+32");
  • subtraction:
EvalExp.evaluate("34-21");
EvalExp.evaluate("33-44");
  • exponentiation:
EvalExp.evaluate("3^2"); //9
EvalExp.evaluate("(-3)^2"); //9
  • root extraction:
EvalExp.evaluate("9^0.5"); //3
EvalExp.evaluate("27^(1/3)"); //3
  • modulo:
EvalExp.evaluate("6%2"); //0
EvalExp.evaluate("7%2"); //1
  • multiplication:
EvalExp.evaluate("5*5");
EvalExp.evaluate("-3*8");
  • division:
EvalExp.evaluate("10/2");
  • brackets:
EvalExp.evaluate("5*(3+(5-3)/4)");

multiplication operator can be ignored

EvalExp.evaluate("3(10+45)");
  • variables
EvalExp.evaluate("3*someVariable");

multiplication operator can be ignored

EvalExp.evaluate("3someVariable");
  • functions without arguments - use like variables (without brackets)
EvalExp.evaluate("3*someFunctions");

multiplication operator can be ignored

EvalExp.evaluate("3someFunctions");
  • functions with arguments
EvalExp.evaluate("3*someFunctions(10)");

complex arguments with inner functions are supported

EvalExp.evaluate("2+funcA(2*funcB(3+4, 4))");

About variables and functions

This library does not give access to your global variables. In execute method all possible variables should be declared. If not declared variable is used in expression it will throw error. If you declare function and use it as variable (without bracked) it will be execeuted as zero argument function

variable varA is used in expression and is declated in evaluate function:

import EvalExp from "evalexp";

const evalExp = new EvalExp("10 + (varA + 5) * 2");
evalExp.parse();
evalExp.evaluate({
    varA: 3
});

varA is declared as function and because of it, it will be executed as function in expression

import EvalExp from "evalexp";

const evalExp = new EvalExp("10 + (varA + 5) * 2");
evalExp.parse();
evalExp.evaluate({
    varA() {
        return 5;
    }
});

Support for predefined functions

This library by design does not give any predefined functions. It can be easily extended with any function you want

If condition

EvalExp.evaluate("IF(1, 2, 3)", {
    IF: (condition, isTrue, isFalse) => (condition ? isTrue : isFalse)
});

power

EvalExp.evaluate("POW(2, 2)", {
    POW: (arg1, arg) => Math.pow(arg1, arg)
});