Jump to content

User:lzupancic/Sandbox/jsExprParser

From Wikipedia, the free encyclopedia
jsExprParser
Developer(s)Leon Zupancic
Stable release
1.6.5 / November 13, 2010 (2010-11-13)
Written inJavaScript
Size14 KB (original) / 6 KB (minified)
TypeJavaScript library
LicenseMIT License
Websitewww.jsexprparser.tk

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

[edit]
  • 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

[edit]

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.

<script type="text/javascript" src="expr-parser.js"></script>

Usage

[edit]

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

var p = new ExprParser();

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.

p.parse("2*(3+5)");         // parses the given expression
var result = p.evaluate();  // evaluates the expression (result = 16)

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

p.parse("x^2+5*y+z");
var result = p.evaluate({x:3,y:4,z:7});  // save variables into repository and evaluates the expression

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

p.getVar("x");    // returns value of variable 'x'
p.clearVar("x");  // removes variable 'x' from the repository
p.getVar("x");    // variable 'x' was removed, function returns 'undefined'
p.getVar();       // returns object with all variables currently in repository
p.clearVar();     // clears all variables from repository

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.

p.parse("x*y*z");
var result = p.evalf({x:2,y:3,z:4});  // expression is immediately evaluated, variables are not saved

p.getVar("x");  // returns 'undefined'
p.getVar();     // returns empty object {}

Plug-ins

[edit]

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.

<script type="text/javascript" src="mathplus.js"></script>
<script type="text/javascript" src="expr-parser.js"></script>

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.

var myFuncLibrary = {
  cube: function(x) {
    return x*x*x;
  }
};

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.

var p = new ExprParser( myFuncLibrary );
p.parse("2*cube(3+x)");
p.evaluate({x:2});  // returns 250

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

p.include( myFuncLibrary );
p.parse("2*cube(3+x)");
p.evaluate({x:2});  // returns 250

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

// returns true if all libraries are included otherwise returns false
p.included( Math, MathPlus, myFuncLibrary );
[edit]

Category:JavaScript libraries Category:Software using the MIT license