From 433c2fb3391d4a7090bb99c948f55885ea171089 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Tue, 21 Apr 2015 16:58:38 +0430 Subject: [PATCH] Revert "simplify init" This reverts commit 743220aa1d050f523effd97a2330d8f1ee3a25d3. --- dist/helpers.js | 2 +- dist/index.js | 34 ++------------------ dist/tests/basic.js | 73 ++++++++++++++++++++++++++++++++++++++++++ dist/tests/simplify.js | 35 -------------------- lib/helpers.js | 2 +- lib/index.js | 36 ++------------------- tests/simplify.js | 28 ---------------- 7 files changed, 80 insertions(+), 130 deletions(-) create mode 100644 dist/tests/basic.js delete mode 100644 dist/tests/simplify.js delete mode 100644 tests/simplify.js diff --git a/dist/helpers.js b/dist/helpers.js index 11ad662..5f92884 100644 --- a/dist/helpers.js +++ b/dist/helpers.js @@ -152,6 +152,6 @@ var flatten = (function (_flatten) { exports.flatten = flatten; var removeSymbols = function removeSymbols(string) { - return string.toString().replace(/\W/g, ''); + return string.replace(/\W/g, ''); }; exports.removeSymbols = removeSymbols; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index d84b5f8..5a7981b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -53,7 +53,6 @@ var Equation = { return stack; }, - /** * Creates an equation function which replaces variables * in the given expression with the values specified in order, @@ -73,7 +72,8 @@ var Equation = { var variables = []; stack.forEach(function (a) { - if (isVariable(a)) { + 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)); } }); @@ -95,31 +95,6 @@ var Equation = { }; }, - /** - * Simplifies a math expression - * - * Example: - * 2*x^2 + x - 2*x - x^2 - * becomes - * x^2 - x - * - * @param {String} expression - * The expression to create an equation for (containing variables) - * @return {String} - * The simplified expression - */ - simplify: function simplify(expression) { - var stack = parseExpression(expression); - stack = sortStack(stack); - stack = _.parseNumbers(stack); - - console.dir(stack, { - depth: null - }); - - return expression; - }, - registerOperator: function registerOperator(key, options) { _operators2['default'][key] = options; }, @@ -202,11 +177,6 @@ var parseExpression = function parseExpression(expression) { return parseGroups(stack); }; -var isVariable = function isVariable(char) { - var ch = _.removeSymbols(char); - return typeof ch === 'string' && !_.isNumber(ch) && !_operators2['default'][ch] && char === ch.toLowerCase(); -}; - /** * Takes the parsed array from parseExpression and * groups up expressions in parantheses in deep arrays diff --git a/dist/tests/basic.js b/dist/tests/basic.js new file mode 100644 index 0000000..6495dc3 --- /dev/null +++ b/dist/tests/basic.js @@ -0,0 +1,73 @@ +'use strict'; + +var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }; + +var _expect = require('chai'); + +var _M = require('../index.js'); + +var _M2 = _interopRequireWildcard(_M); + +describe('Basic math operators', function () { + it('should work for add +', function () { + _expect.expect(_M2['default'].solve('2+2')).to.equal(4); + }); + + it('should work for minus -', function () { + _expect.expect(_M2['default'].solve('15-3')).to.equal(12); + }); + + it('should work for divison /', function () { + _expect.expect(_M2['default'].solve('20/2')).to.equal(10); + }); + + it('should work for multiplication *', function () { + _expect.expect(_M2['default'].solve('6*3')).to.equal(18); + }); + + it('should work for power ^', function () { + _expect.expect(_M2['default'].solve('5^2')).to.equal(25); + }); + + it('should work for multi-digit numbers', function () { + _expect.expect(_M2['default'].solve('12+15')).to.equal(27); + }); +}); + +describe('Precedence', function () { + it('Test case 1', function () { + _expect.expect(_M2['default'].solve('2+(2+1)*(1+1)^2')).to.equal(14); + }); + + it('Test case 2', function () { + _expect.expect(_M2['default'].solve('2+5*4/2-2')).to.equal(10); + }); + + it('Test case 3', function () { + _expect.expect(_M2['default'].solve('2+(5*4/2)-2')).to.equal(10); + }); + + it('Test case 4', function () { + _expect.expect(_M2['default'].solve('(2+2)^2+(5+1)*4+(2+(4/2)/2)')).to.equal(16 + 24 + 3); + }); +}); + +describe('Functions', function () { + it('should work for with parantheses', function () { + _expect.expect(_M2['default'].solve('lg(4) * 5')).to.equal(10); + }); + + it('should work for without parantheses', function () { + _expect.expect(_M2['default'].solve('lg4 * 5')).to.equal(10); + }); +}); + +describe('Constats', function () { + it('should work for constant values', function () { + _expect.expect(_M2['default'].solve('sin(PI/2)')).to.equal(1); + }); + + it('should work for functions as constants', function () { + _expect.expect(_M2['default'].solve('RAND')).to.not.equal(_M2['default'].solve('RAND')); + }); +}); \ No newline at end of file diff --git a/dist/tests/simplify.js b/dist/tests/simplify.js deleted file mode 100644 index 282460e..0000000 --- a/dist/tests/simplify.js +++ /dev/null @@ -1,35 +0,0 @@ -'use strict'; - -var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }; - -var _expect = require('chai'); - -var _M = require('../index.js'); - -var _M2 = _interopRequireWildcard(_M); - -describe('Simplify simple math expressions', function () { - it('should work for add +', function () { - _expect.expect(_M2['default'].simplify('2+2-1')).to.equal('3'); - }); - // - // it('should work for minus -', () => { - // expect(M.solve('15-3')).to.equal(12); - // }); - // - // it('should work for divison /', () => { - // expect(M.solve('20/2')).to.equal(10); - // }); - // - // it('should work for multiplication *', () => { - // expect(M.solve('6*3')).to.equal(18); - // }); - // - // it('should work for power ^', () => { - // expect(M.solve('5^2')).to.equal(25); - // }); - // - // it('should work for multi-digit numbers', () => { - // expect(M.solve('12+15')).to.equal(27); - // }); -}); \ No newline at end of file diff --git a/lib/helpers.js b/lib/helpers.js index b254c31..9be7820 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -74,5 +74,5 @@ export const flatten = (arr) => { }; export const removeSymbols = string => { - return string.toString().replace(/\W/g, ''); + return string.replace(/\W/g, ''); }; diff --git a/lib/index.js b/lib/index.js index 72235fb..e306df2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -30,7 +30,6 @@ let Equation = { return stack; }, - /** * Creates an equation function which replaces variables * in the given expression with the values specified in order, @@ -50,7 +49,9 @@ let Equation = { let variables = []; stack.forEach(a => { - if (isVariable(a)) { + 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)); } }); @@ -68,31 +69,6 @@ let Equation = { }; }, - /** - * Simplifies a math expression - * - * Example: - * 2*x^2 + x - 2*x - x^2 - * becomes - * x^2 - x - * - * @param {String} expression - * The expression to create an equation for (containing variables) - * @return {String} - * The simplified expression - */ - simplify(expression) { - let stack = parseExpression(expression); - stack = sortStack(stack); - stack = _.parseNumbers(stack); - - console.dir(stack, { - depth: null - }); - - return expression; - }, - registerOperator(key, options) { operators[key] = options; }, @@ -166,12 +142,6 @@ const parseExpression = expression => { return parseGroups(stack); }; -const isVariable = char => { - let ch = _.removeSymbols(char); - return typeof ch === 'string' && !_.isNumber(ch) && - !operators[ch] && char === ch.toLowerCase(); -}; - /** * Takes the parsed array from parseExpression and * groups up expressions in parantheses in deep arrays diff --git a/tests/simplify.js b/tests/simplify.js deleted file mode 100644 index b7cc657..0000000 --- a/tests/simplify.js +++ /dev/null @@ -1,28 +0,0 @@ -import {expect} from 'chai'; -import M from '../index.js'; - -describe('Simplify simple math expressions', () => { - it('should work for add +', () => { - expect(M.simplify('2+2-1')).to.equal('3'); - }); - // - // it('should work for minus -', () => { - // expect(M.solve('15-3')).to.equal(12); - // }); - // - // it('should work for divison /', () => { - // expect(M.solve('20/2')).to.equal(10); - // }); - // - // it('should work for multiplication *', () => { - // expect(M.solve('6*3')).to.equal(18); - // }); - // - // it('should work for power ^', () => { - // expect(M.solve('5^2')).to.equal(25); - // }); - // - // it('should work for multi-digit numbers', () => { - // expect(M.solve('12+15')).to.equal(27); - // }); -});