matheval is a node.js library that evaluates simple math expressions. Supports variables, parentheses, basic functions.
var evaluate = require('matheval').evaluate;
evaluate('x = 1', console.log); // prints 1
evaluate('x + 1', console.log); // prints 2
$ npm install matheval
You can override Variables.js to supply your own variables (for example, you can load them from a database or something)
var evaluate = require('./matheval.js').evaluate;
var Variables = require('./matheval.js').Variables;
var v = new Variables();
evaluate('x = 1', v, function(result) {
console.log(result);
evaluate('x + 1', v, console.log);
});
unary minus is higher precedence than exponentiation, so -1**-2 === (-1)(-2), not -(1(-2))
exponentiation is right-associative, so 333 is 327, not 93
The tests rely on mocha and should
To run them:
$ npm test