simplify init

This commit is contained in:
Mahdi Dibaiee 2015-04-21 16:57:18 +04:30
parent 6e79cb1e83
commit 743220aa1d
7 changed files with 130 additions and 80 deletions

2
dist/helpers.js vendored
View File

@ -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;

34
dist/index.js vendored
View File

@ -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

73
dist/tests/basic.js vendored
View File

@ -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'));
});
});

35
dist/tests/simplify.js vendored Normal file
View File

@ -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);
// });
});

View File

@ -74,5 +74,5 @@ export const flatten = (arr) => {
};
export const removeSymbols = string => {
return string.replace(/\W/g, '');
return string.toString().replace(/\W/g, '');
};

View File

@ -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

28
tests/simplify.js Normal file
View File

@ -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);
// });
});