From 743220aa1d050f523effd97a2330d8f1ee3a25d3 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Tue, 21 Apr 2015 16:57:18 +0430 Subject: [PATCH] simplify init --- 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, 130 insertions(+), 80 deletions(-) delete mode 100644 dist/tests/basic.js create mode 100644 dist/tests/simplify.js create mode 100644 tests/simplify.js diff --git a/dist/helpers.js b/dist/helpers.js index 5f92884..11ad662 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.replace(/\W/g, ''); + return string.toString().replace(/\W/g, ''); }; exports.removeSymbols = removeSymbols; \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 5a7981b..d84b5f8 100644 --- a/dist/index.js +++ b/dist/index.js @@ -53,6 +53,7 @@ var Equation = { return stack; }, + /** * Creates an equation function which replaces variables * in the given expression with the values specified in order, @@ -72,8 +73,7 @@ var Equation = { var variables = []; stack.forEach(function (a) { - if (typeof a === 'string' && !_.isNumber(a) && !_operators2['default'][a] && a === a.toLowerCase()) { - // grouped variables like (y) need to have their parantheses removed + if (isVariable(a)) { variables.push(_.removeSymbols(a)); } }); @@ -95,6 +95,31 @@ 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; }, @@ -177,6 +202,11 @@ 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 deleted file mode 100644 index 6495dc3..0000000 --- a/dist/tests/basic.js +++ /dev/null @@ -1,73 +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('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 new file mode 100644 index 0000000..282460e --- /dev/null +++ b/dist/tests/simplify.js @@ -0,0 +1,35 @@ +'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 9be7820..b254c31 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -74,5 +74,5 @@ export const flatten = (arr) => { }; export const removeSymbols = string => { - return string.replace(/\W/g, ''); + return string.toString().replace(/\W/g, ''); }; diff --git a/lib/index.js b/lib/index.js index e306df2..72235fb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -30,6 +30,7 @@ let Equation = { return stack; }, + /** * Creates an equation function which replaces variables * in the given expression with the values specified in order, @@ -49,9 +50,7 @@ let Equation = { let variables = []; stack.forEach(a => { - if (typeof a === 'string' && !_.isNumber(a) && - !operators[a] && a === a.toLowerCase()) { - // grouped variables like (y) need to have their parantheses removed + if (isVariable(a)) { variables.push(_.removeSymbols(a)); } }); @@ -69,6 +68,31 @@ 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; }, @@ -142,6 +166,12 @@ 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 new file mode 100644 index 0000000..b7cc657 --- /dev/null +++ b/tests/simplify.js @@ -0,0 +1,28 @@ +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); + // }); +});