From f903f35098abf60101f2047f31634653d9c291ef Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Mon, 20 Apr 2015 14:25:41 +0430 Subject: [PATCH] small fix, added tests, readme --- Gruntfile.js | 7 ++++++- bin.js | 3 +++ dist/index.js | 18 ++++++++++-------- dist/readstream.js | 3 ++- dist/tests/solve.js | 4 ++++ lib/README.md | 14 ++++++++++++++ lib/index.js | 9 +++++---- package.json | 3 +++ tests/solve.js | 4 ++++ 9 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 bin.js create mode 100644 lib/README.md diff --git a/Gruntfile.js b/Gruntfile.js index 438d3cf..4c99f4d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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/' }] } diff --git a/bin.js b/bin.js new file mode 100644 index 0000000..ba4754a --- /dev/null +++ b/bin.js @@ -0,0 +1,3 @@ +var Equation = require('./dist/index'); + +console.log(Equation.solve(process.argv[2])); diff --git a/dist/index.js b/dist/index.js index 367c441..b8ad384 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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']; \ No newline at end of file diff --git a/dist/readstream.js b/dist/readstream.js index dc33d95..7aef97a 100644 --- a/dist/readstream.js +++ b/dist/readstream.js @@ -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]; diff --git a/dist/tests/solve.js b/dist/tests/solve.js index 6495dc3..2e1f5a7 100644 --- a/dist/tests/solve.js +++ b/dist/tests/solve.js @@ -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 () { diff --git a/lib/README.md b/lib/README.md new file mode 100644 index 0000000..62d10cf --- /dev/null +++ b/lib/README.md @@ -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 +``` diff --git a/lib/index.js b/lib/index.js index 82afcdf..a47413e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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([]); } diff --git a/package.json b/package.json index a35f5a5..55a719c 100644 --- a/package.json +++ b/package.json @@ -22,5 +22,8 @@ "grunt-eslint": "^11.0.0", "grunt-mocha-test": "^0.12.7", "mocha": "^2.2.4" + }, + "dependencies": { + "babel": "^5.1.11" } } diff --git a/tests/solve.js b/tests/solve.js index 5c2dcff..75599aa 100644 --- a/tests/solve.js +++ b/tests/solve.js @@ -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', () => {