From 9274694d98b8f90c0bd05079ac3f4503a9066284 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Tue, 7 Jul 2015 15:50:05 +0430 Subject: [PATCH] Fix #10, add test --- dist/index.js | 6 ++++-- dist/tests/equation.js | 3 +++ lib/index.js | 6 ++++-- package.json | 2 +- tests/equation.js | 4 ++++ 5 files changed, 16 insertions(+), 5 deletions(-) diff --git a/dist/index.js b/dist/index.js index 9d06fca..b5d0aba 100644 --- a/dist/index.js +++ b/dist/index.js @@ -219,7 +219,7 @@ var parseExpression = function parseExpression(expression) { stack.push('-' + cur); // 2 - 5 is also OK, pass - } else if (_.isNumber(beforeSign)) { + } else if (_.isNumber(beforeSign) || isVariable(beforeSign)) { stack.push(cur); } else { stack[past] += cur; @@ -478,8 +478,10 @@ var fixFloat = function fixFloat(number) { * @return {Boolean} * true if variable, else false */ + +var SPECIALS = '()[]{}'.split(''); var isVariable = function isVariable(a) { - return typeof a === 'string' && !_.isNumber(a) && !_operators2['default'][a] && a === a.toLowerCase(); + return typeof a === 'string' && !_.isNumber(a) && !_operators2['default'][a] && a === a.toLowerCase() && SPECIALS.indexOf(a) === -1; }; exports.isVariable = isVariable; diff --git a/dist/tests/equation.js b/dist/tests/equation.js index c018fbb..a086517 100644 --- a/dist/tests/equation.js +++ b/dist/tests/equation.js @@ -13,6 +13,9 @@ describe('Equations', function () { var equation = _M2['default'].equation('x+2'); _expect.expect(equation(2)).to.equal(4); + + var subtraction = _M2['default'].equation('x - 3'); + _expect.expect(subtraction(10)).to.equal(7); }); it('should work with multiple variables', function () { diff --git a/lib/index.js b/lib/index.js index fb8f07f..65fbce5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -182,7 +182,7 @@ const parseExpression = expression => { stack.push(`-${cur}`); // 2 - 5 is also OK, pass - } else if (_.isNumber(beforeSign)) { + } else if (_.isNumber(beforeSign) || isVariable(beforeSign)) { stack.push(cur); } else { stack[past] += cur; @@ -379,9 +379,11 @@ const fixFloat = number => { * @return {Boolean} * true if variable, else false */ + +const SPECIALS = '()[]{}'.split(''); export const isVariable = a => { return typeof a === 'string' && !_.isNumber(a) && - !operators[a] && a === a.toLowerCase(); + !operators[a] && a === a.toLowerCase() && SPECIALS.indexOf(a) === -1; }; export default Equation; diff --git a/package.json b/package.json index ecaf0c6..127004f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "equations", - "version": "1.3.2", + "version": "1.3.3", "description": "", "main": "dist/index.js", "directories": { diff --git a/tests/equation.js b/tests/equation.js index 81a36ec..f05a472 100644 --- a/tests/equation.js +++ b/tests/equation.js @@ -6,6 +6,10 @@ describe('Equations', () => { let equation = M.equation('x+2'); expect(equation(2)).to.equal(4); + + // Issue #10 + let subtraction = M.equation('x - 3'); + expect(subtraction(10)).to.equal(7); }); it('should work with multiple variables', () => {