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

evaluator.js

v3.2.4

Published

Evaluates mathematical expressions

Downloads

672

Readme

Evaluator.js

Evaluator.js is a small, zero-dependency module for evaluating mathematical expressions.

All major operations, constants, and methods are supported. Additionally, Evaluator.js intelligently reports invalid syntax, such as a misused operator, missing operand, or mismatched parentheses.

Evaluator.js is used by a desktop calculator application of the same name. See a live demo on the website.

Installation

npm install evaluator.js

API

/**
 * Takes a string and evaluates the result.
 *
 * @param {string} expression The string.
 *
 * @throws {Error} No input.
 * @throws {Error} No valid tokens.
 * @throws {Error} Misused operator: <token>.
 * @throws {Error} Mismatched parentheses.
 * @throws {Error} Invalid token: <token>.
 * @throws {Error} No operations.
 * @throws {Error} Insufficient arguments for method: <token>.
 * @throws {Error} Insufficient operands for operator: <token>.
 * @throws {Error} Division by zero.
 * @throws {Error} Insufficient operators.
 *
 * @returns {number} The result.
 */
export default function (expression: string): number;

Examples

import evaluate from 'evaluator.js';

evaluate('8 / (2 + 2.75)');    // 1.68421053
evaluate('pi / 1.3');          // 2.41660973
evaluate('sum(5, 10, 50, -5)') // 60
evaluate('194 % 5');           // 4
evaluate('mean(12, 25, 1, 7)') // 11.25
evaluate('-8 - -3');           // -5
evaluate('acos(0)');           // 1.57079633
evaluate('((6 - 9) / 5) * 9'); // -5.4
evaluate('-e ^ -2');           // -0.13533528

Reference

Operators

  • + - Add / Unary Plus
  • - - Subtract / Unary Minus
  • * - Multiply
  • / - Divide
  • ^ - Power
  • % - Modulo
  • ( - Begin Group
  • ) - End Group
  • , - Separate Argument

Constants

  • E - Euler's constant and the base of natural logarithms.
  • LN2 - Natural logarithm of 2.
  • LN10 - Natural logarithm of 10.
  • LOG2E - Base 2 logarithm of E.
  • LOG10E - Base 10 logarithm of E.
  • PHI - Golden ratio.
  • PI - Ratio of the circumference of a circle to its diameter.
  • SQRT1_2 - Square root of 1/2.
  • SQRT2 - Square root of 2.
  • TAU - Ratio of the circumference of a circle to its radius.

Methods

  • ABS(x) - Returns the absolute value of a number.
  • ACOS(x) - Returns the arccosine of a number.
  • ACOSH(x) - Returns the hyperbolic arccosine of a number.
  • ADD(x, y) - Returns the total of two numbers.
  • ASIN(x) - Returns the arcsine of a number.
  • ASINH(x) - Returns the hyperbolic arcsine of a number.
  • ATAN(x) - Returns the arctangent of a number.
  • ATANH(x) - Returns the hyperbolic arctangent of a number.
  • ATAN2(y, x) - Returns the arctangent of the quotient of the arguments.
  • CBRT(x) - Returns the cube root of a number.
  • CEIL(x) - Returns the smallest integer greater than or equal to a number.
  • COS(x) - Returns the cosine of a number.
  • COSH(x) - Returns the hyperbolic cosine of a number.
  • DIVIDE(x, y) - Returns the quotient of two numbers.
  • EXP(x) - Returns E to the power of x.
  • EXPM1(x) - Returns subtracting 1 from EXP(x).
  • FACTORIAL(x) - Returns the factorial of x.
  • FLOOR(x) - Returns the largest integer less than or equal to a number.
  • HYPOT(x[, y[, ...]]) - Returns the square root of the sum of squares of the arguments.
  • LOG(x) - Returns the natural logarithm of a number.
  • LOG1P(x) - Returns the natural logarithm of 1 + x.
  • LOG10(x) - Returns the base 10 logarithm of a number.
  • LOG2(x) - Returns the base 2 logarithm of a number.
  • MAX(x[, y[, ...]]) - Returns the largest of one or more numbers.
  • MEAN(x[, y[, ...]]) - Returns the mean of one or more numbers.
  • MIN(x[, y[, ...]]) - Returns the smallest of one or more numbers.
  • MOD(x, y) - Returns the modulus of two numbers.
  • MULTIPLY(x, y) - Returns the product of two numbers.
  • POW(x, y) - Returns base to the exponent power.
  • SIN(x) - Returns the sine of a number.
  • SINH(x) - Returns the hyperbolic sine of a number.
  • SQRT(x) - Returns the positive square root of a number.
  • SUBTRACT(x, y) - Returns the difference of two numbers.
  • SUM(x[, y[, ...]]) - Returns the sum of one or more numbers.
  • TAN(x) - Returns the tangent of a number.
  • TANH(x) - Returns the hyperbolic tangent of a number.