small fix, added tests, readme
This commit is contained in:
parent
a8f47cd662
commit
f903f35098
@ -7,7 +7,12 @@ module.exports = function(grunt) {
|
|||||||
dist: {
|
dist: {
|
||||||
files: [{
|
files: [{
|
||||||
expand: true,
|
expand: true,
|
||||||
src: ['lib/*.js', 'tests/*.js'],
|
src: ['tests/*.js'],
|
||||||
|
dest: 'dist/'
|
||||||
|
}, {
|
||||||
|
expand: true,
|
||||||
|
src: ['lib/*.js'],
|
||||||
|
flatten: true,
|
||||||
dest: 'dist/'
|
dest: 'dist/'
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
|
3
bin.js
Normal file
3
bin.js
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
var Equation = require('./dist/index');
|
||||||
|
|
||||||
|
console.log(Equation.solve(process.argv[2]));
|
18
dist/index.js
vendored
18
dist/index.js
vendored
@ -26,7 +26,11 @@ var _import = require('./helpers');
|
|||||||
|
|
||||||
var _ = _interopRequireWildcard(_import);
|
var _ = _interopRequireWildcard(_import);
|
||||||
|
|
||||||
var Mathstring = {
|
var _polyfills = require('babel/polyfill');
|
||||||
|
|
||||||
|
var _polyfills2 = _interopRequireWildcard(_polyfills);
|
||||||
|
|
||||||
|
var Equation = {
|
||||||
/**
|
/**
|
||||||
* Solves the given math expression, following these steps:
|
* Solves the given math expression, following these steps:
|
||||||
* 1. Replace constants in the expression
|
* 1. Replace constants in the expression
|
||||||
@ -72,7 +76,7 @@ var Mathstring = {
|
|||||||
var variables = [];
|
var variables = [];
|
||||||
|
|
||||||
stack.forEach(function (a) {
|
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
|
// grouped variables like (y) need to have their parantheses removed
|
||||||
variables.push(_.removeSymbols(a));
|
variables.push(_.removeSymbols(a));
|
||||||
}
|
}
|
||||||
@ -91,8 +95,7 @@ var Mathstring = {
|
|||||||
return a;
|
return a;
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(variables, expression);
|
return Equation.solve(expression);
|
||||||
return Mathstring.solve(expression);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -168,7 +171,6 @@ var parseExpression = function parseExpression(expression) {
|
|||||||
stack.push(record);
|
stack.push(record);
|
||||||
}
|
}
|
||||||
|
|
||||||
// $0(stack);
|
|
||||||
return parseGroups(stack);
|
return parseGroups(stack);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -187,9 +189,9 @@ var parseGroups = function parseGroups(stack) {
|
|||||||
// Parantheses become inner arrays which will then be processed first
|
// Parantheses become inner arrays which will then be processed first
|
||||||
var sub = 0;
|
var sub = 0;
|
||||||
return stack.reduce(function (a, b) {
|
return stack.reduce(function (a, b) {
|
||||||
if (b[b.length - 1] === '(') {
|
if (b.includes('(')) {
|
||||||
if (b.length > 1) {
|
if (b.length > 1) {
|
||||||
_.dive(a, sub).push(b.slice(0, -1), []);
|
_.dive(a, sub).push(b.replace('(', ''), []);
|
||||||
} else {
|
} else {
|
||||||
_.dive(a, sub).push([]);
|
_.dive(a, sub).push([]);
|
||||||
}
|
}
|
||||||
@ -392,5 +394,5 @@ var replaceConstants = function replaceConstants(expression) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports['default'] = Mathstring;
|
exports['default'] = Equation;
|
||||||
module.exports = exports['default'];
|
module.exports = exports['default'];
|
3
dist/readstream.js
vendored
3
dist/readstream.js
vendored
@ -13,7 +13,8 @@ exports['default'] = function (string) {
|
|||||||
|
|
||||||
if (i >= string.length) {
|
if (i >= string.length) {
|
||||||
return null;
|
return null;
|
||||||
}return string[i++];
|
}
|
||||||
|
return string[i++];
|
||||||
},
|
},
|
||||||
current: function current() {
|
current: function current() {
|
||||||
return string[i - 1];
|
return string[i - 1];
|
||||||
|
4
dist/tests/solve.js
vendored
4
dist/tests/solve.js
vendored
@ -60,6 +60,10 @@ describe('Functions', function () {
|
|||||||
it('should work for without parantheses', function () {
|
it('should work for without parantheses', function () {
|
||||||
_expect.expect(_M2['default'].solve('lg4 * 5')).to.equal(10);
|
_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 () {
|
describe('Constats', function () {
|
||||||
|
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 operators from './operators';
|
||||||
import constants from './constats';
|
import constants from './constats';
|
||||||
import * as _ from './helpers';
|
import * as _ from './helpers';
|
||||||
|
import polyfills from 'babel/polyfill';
|
||||||
|
|
||||||
let Equation = {
|
let Equation = {
|
||||||
/**
|
/**
|
||||||
@ -49,7 +50,8 @@ let Equation = {
|
|||||||
let variables = [];
|
let variables = [];
|
||||||
|
|
||||||
stack.forEach(a => {
|
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
|
// grouped variables like (y) need to have their parantheses removed
|
||||||
variables.push(_.removeSymbols(a));
|
variables.push(_.removeSymbols(a));
|
||||||
}
|
}
|
||||||
@ -131,7 +133,6 @@ const parseExpression = expression => {
|
|||||||
stack.push(record);
|
stack.push(record);
|
||||||
}
|
}
|
||||||
|
|
||||||
// $0(stack);
|
|
||||||
return parseGroups(stack);
|
return parseGroups(stack);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -150,9 +151,9 @@ const parseGroups = stack => {
|
|||||||
// Parantheses become inner arrays which will then be processed first
|
// Parantheses become inner arrays which will then be processed first
|
||||||
let sub = 0;
|
let sub = 0;
|
||||||
return stack.reduce((a, b) => {
|
return stack.reduce((a, b) => {
|
||||||
if (b[b.length - 1] === '(') {
|
if (b.includes('(')) {
|
||||||
if (b.length > 1) {
|
if (b.length > 1) {
|
||||||
_.dive(a, sub).push(b.slice(0, -1), []);
|
_.dive(a, sub).push(b.replace('(', ''), []);
|
||||||
} else {
|
} else {
|
||||||
_.dive(a, sub).push([]);
|
_.dive(a, sub).push([]);
|
||||||
}
|
}
|
||||||
|
@ -22,5 +22,8 @@
|
|||||||
"grunt-eslint": "^11.0.0",
|
"grunt-eslint": "^11.0.0",
|
||||||
"grunt-mocha-test": "^0.12.7",
|
"grunt-mocha-test": "^0.12.7",
|
||||||
"mocha": "^2.2.4"
|
"mocha": "^2.2.4"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"babel": "^5.1.11"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,6 +53,10 @@ describe('Functions', () => {
|
|||||||
it('should work for without parantheses', () => {
|
it('should work for without parantheses', () => {
|
||||||
expect(M.solve('lg4 * 5')).to.equal(10);
|
expect(M.solve('lg4 * 5')).to.equal(10);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should work for wrapped functions', () => {
|
||||||
|
expect(M.solve('(lg4)*2')).to.equal(4);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Constats', () => {
|
describe('Constats', () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user