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

string2math

v1.0.0

Published

A library for calculating mathematical expressions from a string

Readme

string2math

A library for calculating mathematical expressions from a string

How it works

npm install string2math

Calculate

import string2math from 'string2math'

Numbers

let program_1 = string2math('(1.2 + 42) * -4.4e-2')
console.log(program_1.calculate()) // -1.9008

Variables

let program_2 = string2math('var_1 + var_2 * 3')
console.log(program_2.calculate({ var_1: 1, var_2: 2 })) // 7

// complex variable names should be written in square brackets

program_2 = string2math('[EUR / USD] / [GBP / USD]')
console.log(program_2.calculate({ '[EUR / USD]': 1.04, '[GBP / USD]': 1.26 }))

Functions

let program_3 = string2math('func(1, 2) + x')
console.log(program_3.calculate({ func: Math.max, x: 5 })) // 7

// or

program_3 = string2math('max(1, 2) + x')
console.log(program_3.calculate(Math, { x: 5 })) // 7

Conditions

let program_1 = string2math('x !== -Infinity ? x : NaN')
console.log(program_1.calculate({ x: 42 })) // 42

Custom operators

let program_4 = string2math('vec2(1, 2) + vec2(3, 4) - -vec2(1, 1)')
console.log(
  program_4.calculate({
    vec2: (x, y) => ({ x: x, y: y }),
    '+': (v1 = { x: 0, y: 0 }, v2) => ({ x: v1.x + v2.x, y: v1.y + v2.y }),
    '-': (v1 = { x: 0, y: 0 }, v2) => ({ x: v1.x - v2.x, y: v1.y - v2.y }),
  })
) // { x: 5, y: 7 }

/*
NOTE
The operators "+" and "-" can be placed before a variable, like "!" and "~":
-~2 - -3 * +!5

In this case, the function will be called like this:

addition(undefined, value) // +
subtraction(undefined, value) // -

!!! We need to keep this in mind !!!
*/

All operators can be replaced:

 '!'  : 'Logical NOT',
 '~'  : 'Bitwise NOT',
 '**' : 'Exponentiation',
 '*'  : 'Multiplication',
 '/'  : 'Division',
 '%'  : 'Remainder',
 '+'  : 'Addition',
 '-'  : 'Subtraction',
 '<<' : 'Bitwise left shift',
 '>>' : 'Bitwise right shift',
 '>>>': 'Bitwise unsigned right shift',
 '<'  : 'Less than',
 '<=' : 'Less than or equal',
 '>'  : 'Greater than',
 '>=' : 'Greater than or equal',
 '==' : 'Equality',
 '!=' : 'Inequality',
 '='  : 'Strict equality',
 '===': 'Strict equality',
 '!==': 'Strict inequality',
 '&'  : 'Bitwise AND',
 '^'  : 'Bitwise XOR',
 '|'  : 'Bitwise OR',
 '&&' : 'Logical AND',
 '||' : 'Logical OR',
 '??' : 'Coalescing NaN'

By default, '=' is equivalent to '===', but they can be replaced independently.

Schema and Nodes

// Nodes
import {
  ProgramNode,
  ConditionNode,
  VariableNode,
  ConstantNode,
  FunctionNode,
  OperatorNode,
} from 'string2math'

import string2math from 'string2math'

const program = string2math('(x + 1) * 2 - 3 >= func(4, 5) ? 6 : 7')

console.log(program)
//  ||
//  ||
// \  /
//  \/
_ = /* ProgramNode */ {
  type: 'Program',
  nodeChild: /* ConditionNode */ {
    type: 'Condition',
    nodeIf: /* OperatorNode */ {
      type: 'Operator',
      operator: '>=',
      nodeLeft: /* OperatorNode */ {
        type: 'Operator',
        operator: '-',
        nodeLeft: /* OperatorNode */ {
          type: 'Operator',
          operator: '*',
          nodeLeft: /* OperatorNode */ {
            type: 'Operator',
            operator: '+',
            nodeLeft: /* VariableNode */ {
              type: 'Variable',
              variable: 'x',
            },
            nodeRight: /* ConstantNode */ {
              type: 'Constant',
              constant: 1,
            },
          },
          nodeRight: /* ConstantNode */ {
            type: 'Constant',
            constant: 2,
          },
        },
        nodeRight: /* ConstantNode */ {
          type: 'Constant',
          constant: 3,
        },
      },
      nodeRight: /* FunctionNode */ {
        type: 'Function',
        function: 'func',
        nodeListOfArgs: [
          /* ConstantNode */ {
            type: 'Constant',
            constant: 4,
          },
          /* ConstantNode */ {
            type: 'Constant',
            constant: 5,
          },
        ],
      },
    },
    nodeThen: /* ConstantNode */ {
      type: 'Constant',
      constant: 6,
    },
    nodeElse: /* ConstantNode */ {
      type: 'Constant',
      constant: 7,
    },
  },
}

License

MIT