Equation.js/README.md

103 lines
3.1 KiB
Markdown
Raw Normal View History

2015-06-21 19:22:12 +00:00
Equation [![Build Status](https://travis-ci.org/mdibaiee/Equation.svg?branch=master)](https://travis-ci.org/mdibaiee/Equation)
2015-04-20 10:49:53 +00:00
========
Solve math expressions or create equations for repeated and complex Math tasks.
2015-04-20 11:03:55 +00:00
To use in browser, download [`equation.min.js`](https://raw.githubusercontent.com/mdibaiee/Equation/master/equation.min.js).
2015-04-20 10:49:53 +00:00
2016-11-02 22:15:30 +00:00
_A similar library I wrote in Haskell is also available [mathexpr](https://github.com/mdibaiee/mathexpr)_
2015-04-21 10:16:05 +00:00
Install using npm:
```
npm install equations
```
2017-11-28 18:22:52 +00:00
#### Examples:
2015-04-21 10:16:05 +00:00
2015-04-20 10:49:53 +00:00
```javascript
// solve
console.log(Equation.solve('4 * lg(4) ^ 3')); // 32
// equation
let sphereArea = Equation.equation('4 * PI * r^2');
console.log(sphereArea(5)); // 314.1592653589793
let test = Equation.equation('2x + 6y');
console.log(test(4, 3)).to.equal(8 + 18);
2015-04-20 10:49:53 +00:00
```
2015-04-20 11:03:55 +00:00
You can also register your own operators and constants.
```javascript
// these options are explained in [`operators.js`](https://github.com/mdibaiee/Equation/blob/master/lib/operators.js).
2015-04-20 11:03:55 +00:00
Equation.registerOperator('$', {
fn: a => 1/a,
format: '10',
precedence: 2
});
Equation.solve('$2'); // 0.5
Equation.solve('$5'); // 0.2
Equation.registerConstant('N', () => {
return Math.random() * 10 + 10
});
Equation.solve('N'); // a number between 10 and 20
// Complex operators
Equation.solve('sigma(0, 5, 2@ + 5)'); // 60
2015-04-20 11:03:55 +00:00
```
For a list of operators and constants see [`operators.js`](https://github.com/mdibaiee/Equation/blob/master/lib/operators.js) and [`constants.js`](https://github.com/mdibaiee/Equation/blob/master/lib/constants.js).
2015-04-21 10:20:54 +00:00
2015-04-20 10:49:53 +00:00
API
===
2017-11-28 18:22:52 +00:00
#### solve(expression: String)
2015-04-20 10:49:53 +00:00
Takes a math expression and evaluates it, returning the result.
2017-11-28 18:22:52 +00:00
#### equation(expression: String)
2015-04-20 10:49:53 +00:00
Takes a math expression containing variables, returning a function which
replaces the variables with given arguments, and solves the expression.
2017-11-28 18:22:52 +00:00
#### registerOperator(key: String, options: Object)
2015-04-20 11:03:55 +00:00
Registers a new operator.
Options:
2017-11-28 18:22:52 +00:00
##### fn
2015-04-21 10:16:05 +00:00
The function which is run on arguments and returns the result
2017-11-28 18:22:52 +00:00
##### format
2015-04-20 14:23:42 +00:00
The format which specifies how arguments are placed relative to operator, this is a string in which 1 represents the operator and zeroes represent arguments.
2015-04-20 11:03:55 +00:00
For example `+` has the format `010` and factorial has `01`.
2017-11-28 18:22:52 +00:00
##### precedence
2015-04-21 10:16:05 +00:00
Specifies the precedence of operator. The less the value, the higher the precedence, resulting in sooner execution.
2015-04-20 11:03:55 +00:00
2017-11-28 18:22:52 +00:00
#### registerConstant(key: String, value: Function/Number)
2015-04-20 11:03:55 +00:00
Registers a new constant. value can be a function (takes no arguments), or a constant number.
That's right, we have named these *constants* but they actually can change, that's why we can use functions to define them. An example is the `RAND` constant which calls `Math.random`.
2015-04-20 11:13:29 +00:00
Contributing
============
The code is commented and I hope you don't have a hard time contributing,
anyway, some tips about contributing:
The source code is in `lib` directory and is then transpiled into `dist`.
#####Grunt Tasks
######default
Transpiles `lib` to `dist`
######build
Transpiles, browserifies, uglifies
######eslint
Runs ESLint on `lib` and `tests`
######test
Runs the tests
2015-04-21 10:16:05 +00:00
---
2015-04-20 11:13:29 +00:00
Please make sure to run `grunt eslint` before commiting.
If you're adding something new, please add a test, too.