small fix, added tests, readme

This commit is contained in:
Mahdi Dibaiee 2015-04-20 14:25:41 +04:30
parent a8f47cd662
commit f903f35098
9 changed files with 51 additions and 14 deletions

View File

@ -7,7 +7,12 @@ module.exports = function(grunt) {
dist: {
files: [{
expand: true,
src: ['lib/*.js', 'tests/*.js'],
src: ['tests/*.js'],
dest: 'dist/'
}, {
expand: true,
src: ['lib/*.js'],
flatten: true,
dest: 'dist/'
}]
}

3
bin.js Normal file
View File

@ -0,0 +1,3 @@
var Equation = require('./dist/index');
console.log(Equation.solve(process.argv[2]));

18
dist/index.js vendored
View File

@ -26,7 +26,11 @@ var _import = require('./helpers');
var _ = _interopRequireWildcard(_import);
var Mathstring = {
var _polyfills = require('babel/polyfill');
var _polyfills2 = _interopRequireWildcard(_polyfills);
var Equation = {
/**
* Solves the given math expression, following these steps:
* 1. Replace constants in the expression
@ -72,7 +76,7 @@ var Mathstring = {
var variables = [];
stack.forEach(function (a) {
if (!_.isNumber(a) && !_operators2['default'][a] && a === a.toLowerCase()) {
if (typeof a === 'string' && !_.isNumber(a) && !_operators2['default'][a] && a === a.toLowerCase()) {
// grouped variables like (y) need to have their parantheses removed
variables.push(_.removeSymbols(a));
}
@ -91,8 +95,7 @@ var Mathstring = {
return a;
});
console.log(variables, expression);
return Mathstring.solve(expression);
return Equation.solve(expression);
};
}
};
@ -168,7 +171,6 @@ var parseExpression = function parseExpression(expression) {
stack.push(record);
}
// $0(stack);
return parseGroups(stack);
};
@ -187,9 +189,9 @@ var parseGroups = function parseGroups(stack) {
// Parantheses become inner arrays which will then be processed first
var sub = 0;
return stack.reduce(function (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([]);
}
@ -392,5 +394,5 @@ var replaceConstants = function replaceConstants(expression) {
});
};
exports['default'] = Mathstring;
exports['default'] = Equation;
module.exports = exports['default'];

3
dist/readstream.js vendored
View File

@ -13,7 +13,8 @@ exports['default'] = function (string) {
if (i >= string.length) {
return null;
}return string[i++];
}
return string[i++];
},
current: function current() {
return string[i - 1];

4
dist/tests/solve.js vendored
View File

@ -60,6 +60,10 @@ describe('Functions', function () {
it('should work for without parantheses', function () {
_expect.expect(_M2['default'].solve('lg4 * 5')).to.equal(10);
});
it('should work for wrapped functions', function () {
_expect.expect(_M2['default'].solve('(lg4)*2')).to.equal(4);
});
});
describe('Constats', function () {

14
lib/README.md Normal file
View 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
```

View File

@ -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([]);
}

View File

@ -22,5 +22,8 @@
"grunt-eslint": "^11.0.0",
"grunt-mocha-test": "^0.12.7",
"mocha": "^2.2.4"
},
"dependencies": {
"babel": "^5.1.11"
}
}

View File

@ -53,6 +53,10 @@ describe('Functions', () => {
it('should work for without parantheses', () => {
expect(M.solve('lg4 * 5')).to.equal(10);
});
it('should work for wrapped functions', () => {
expect(M.solve('(lg4)*2')).to.equal(4);
});
});
describe('Constats', () => {