https://github.com/codeplea/rimath
Math Expression Evaluator in JavaScript
https://github.com/codeplea/rimath
Last synced: 5 months ago
JSON representation
Math Expression Evaluator in JavaScript
- Host: GitHub
- URL: https://github.com/codeplea/rimath
- Owner: codeplea
- License: zlib
- Created: 2019-02-04T13:52:01.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-04T14:36:10.000Z (about 7 years ago)
- Last Synced: 2025-10-09T07:04:27.029Z (5 months ago)
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RiMath
RiMath is a small math expression evaluator in JavaScript. It uses the
[shunting-yard
algorithm](https://en.wikipedia.org/wiki/Shunting-yard_algorithm) to convert
expressions to [Reverse Polish
Notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation).
This code is battle-tested and has been in use since 2013 at [Turnkey
Telemetry](https://turnkeytelemetry.com/), both server-side and client-side.
## Installation
If using Node.js, you can easily install with npm:
```
npm install rimath
```
Otherwise, just copy `rimath.js` into your project. It also works fine
client-side.
## Example Usage
```
var rimath = require('./rimath.js');
//Basic usage
var expr = rimath.compile("100+5*2");
console.log(expr()); //110
//Call external function
var triple = function(s) {s.push(s.pop()*3);};
var funcs = {trip: [1, triple]};
expr = rimath.compile("trip(100)", funcs);
console.log(expr()); //300
//Detect error (unbalanced paran)
expr = rimath.compile("1+(4*2");
console.log(expr); //{text: '1+(4*2', index: 6, token: '', type: 'paren'}
```