ClementNerma / Expression.js

An expression parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Expression.js

Expression.js is an expression parser which permit to make and evaluate mathematical expressions using variables and functions.

How to use

First, download the main expression.js file. Then, include it in your HTML page :

<script type="text/javascript" src="expression.js"></script>

Or in your Node.js script :

var Expression = require('./expression.js');

Features

Simple Operations

Here is a code sample :

var parsed = Expression.parse('2 + 8 / 5'); // Parse the expression
// Do some stuff here...
var result = Expression.eval(result);       // And evaluate it

// ==== OR ====
var result = Expression.exec('2 + 8 / 5');

The point of parsing an expression before evaluating it is performances. If you have to evaluate multiple expressions at a time, you can parse it at the beginning of your task, and then evaluate it. NOTE : The longest part is the parsing.

Variables

You can also use variables, like :

Expression.exec('2 + 8 / (6 + 2)', {a: 5}); // Output: 3

If you want to prepare the expression before running it, do :

var parsed = Expression.parse('2 + 8 / (6 + 2)');
// Do some stuff...
Expression.eval(parsed, {a: 5}); // Output: 3

Strings

Expressions can contain only numbers or only strings. If you want to use strings, your code should be :

Expression.exec('"Hello" + "World"');

You MUST use the " symbol to define strings. The ' symbol will not work. NOTE : Only the '+' operator works in this context. The '-' '*' '/' operators does not work.

Functions

Now, let's see how the functions works. If you want, for example, to use the cube function, do :

Expression.exec('3 * cube(5) / 10', { cube: function(n) { return n * n * n; } }); // Output: 37.5

You can also use cosine functions, round...

About

An expression parser

License:GNU General Public License v3.0


Languages

Language:JavaScript 100.0%