2
									
								
								dist/helpers.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								dist/helpers.js
									
									
									
									
										vendored
									
									
								
							@@ -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;
 | 
			
		||||
							
								
								
									
										34
									
								
								dist/index.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										34
									
								
								dist/index.js
									
									
									
									
										vendored
									
									
								
							@@ -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
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										73
									
								
								dist/tests/basic.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								dist/tests/basic.js
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -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'));
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
							
								
								
									
										35
									
								
								dist/tests/simplify.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										35
									
								
								dist/tests/simplify.js
									
									
									
									
										vendored
									
									
								
							@@ -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);
 | 
			
		||||
  // });
 | 
			
		||||
});
 | 
			
		||||
@@ -74,5 +74,5 @@ export const flatten = (arr) => {
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export const removeSymbols = string => {
 | 
			
		||||
  return string.toString().replace(/\W/g, '');
 | 
			
		||||
  return string.replace(/\W/g, '');
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										36
									
								
								lib/index.js
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								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
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
  // });
 | 
			
		||||
});
 | 
			
		||||
		Reference in New Issue
	
	Block a user