small fix, added tests, readme
This commit is contained in:
14
lib/README.md
Normal file
14
lib/README.md
Normal file
@ -0,0 +1,14 @@
|
||||
Equation
|
||||
========
|
||||
|
||||
Solve math expressions or create equations for repeated and complex Math tasks.
|
||||
|
||||
```javascript
|
||||
// solve
|
||||
console.log(Equation.solve('4 * lg4 ^ 3')); // 32
|
||||
|
||||
// equations
|
||||
let sphereArea = Equation.equation('4 * PI * r^2');
|
||||
|
||||
console.log(sphereArea(5)); // 314.1592653589793
|
||||
```
|
@ -2,6 +2,7 @@ import ReadStream from './readstream';
|
||||
import operators from './operators';
|
||||
import constants from './constats';
|
||||
import * as _ from './helpers';
|
||||
import polyfills from 'babel/polyfill';
|
||||
|
||||
let Equation = {
|
||||
/**
|
||||
@ -49,7 +50,8 @@ let Equation = {
|
||||
let variables = [];
|
||||
|
||||
stack.forEach(a => {
|
||||
if (!_.isNumber(a) && !operators[a] && a === a.toLowerCase()) {
|
||||
if (typeof a === 'string' && !_.isNumber(a) &&
|
||||
!operators[a] && a === a.toLowerCase()) {
|
||||
// grouped variables like (y) need to have their parantheses removed
|
||||
variables.push(_.removeSymbols(a));
|
||||
}
|
||||
@ -131,7 +133,6 @@ const parseExpression = expression => {
|
||||
stack.push(record);
|
||||
}
|
||||
|
||||
// $0(stack);
|
||||
return parseGroups(stack);
|
||||
};
|
||||
|
||||
@ -150,9 +151,9 @@ const parseGroups = stack => {
|
||||
// Parantheses become inner arrays which will then be processed first
|
||||
let sub = 0;
|
||||
return stack.reduce((a, b) => {
|
||||
if (b[b.length - 1] === '(') {
|
||||
if (b.includes('(')) {
|
||||
if (b.length > 1) {
|
||||
_.dive(a, sub).push(b.slice(0, -1), []);
|
||||
_.dive(a, sub).push(b.replace('(', ''), []);
|
||||
} else {
|
||||
_.dive(a, sub).push([]);
|
||||
}
|
||||
|
Reference in New Issue
Block a user