User:Lzupancic/Sandbox/jsExprParser

jsExprParser is fast and lightweight open source cross-browser JavaScript library. It provides simple and efficient way to perform evaluations of mathematical expressions given as plain strings. Supported are native JavaScript operators along with all functions and constants defined in build-in JavaScript Math object. Additional functions are supported by using included MathPlus library. On top of that it is possible to include arbitrary number of additional custom libraries. The project is licensed under MIT license.

Features

 * Completely standalone (no 3rd party library dependencies)
 * No extensions to native JavaScript objects
 * Super fast expression evaluations
 * Support for JavaScript operators
 * Support for JavaScript Math object functions and constants
 * Support for custom variables
 * Support for custom libraries
 * Small size
 * Easy to use

Including the library
The jsExprParser library is a single JavaScript file, containing all of its functions. It can be included within a web page by linking to a local copy.

Usage
jsExprParser is used by first creating an instance of the ExprParser object.

Once the ExprParser object has been instantiated, its methods may be called. Before some expression can be evaluated it must be parsed. This is time consuming operation but it needs to be done only once - before the expression is evaluated for the first time. After that evaluations are performed very fast.

Expressions supplied to parse method may of course contain arbitrary number of variables. Values are then specified for variables at evaluation time.

Variables from the above example are automatically saved into internal repository and are accessible to the user.

There is also faster evaluation method available which does not save variables to repository but only uses them for the time of the method call.

Plug-ins
jsExprParser is designed in way that other developers can create custom libraries of mathematical functions and include those libraries in jsExprParser to extend its knowledge. Currently there is one additional library shipped with jsExprParser. This is MathPlus library defined in mathplus.js source file. To use it, library must be included within a web page along with expr-parser.js. It can be included before (recommended) or after expr-parser.js.

Once the MathPlus library is included on page jsExprParser uses it by default and all additional functions are immediately available. User-defined custom functions do not need to be defined in a separate file. They can be specified inside object literal and then included to jsExprParser. It is up to developer to decide if functions should be in a separate file or not. In case of many or even a few functions it is best programming practice to put them into a separate JavaScript file and then include this file within a web page. Below is an example of a simple custom defined library, containing only one function.

There are two ways to include library to jsExprParser. If ExprParser instance is not yet created then library can be specified as a constructor parameter. Constructor accepts arbitrary number of comma separated custom libraries.

If instance of ExprParser already exists then it is possible to use include method.

It is also possible to test if one or more libraries are included.