Fix #10, add test

This commit is contained in:
Mahdi Dibaiee 2015-07-07 15:50:05 +04:30
parent a4b45ded0a
commit 9274694d98
5 changed files with 16 additions and 5 deletions

6
dist/index.js vendored
View File

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

View File

@ -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 () {

View File

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

View File

@ -1,6 +1,6 @@
{
"name": "equations",
"version": "1.3.2",
"version": "1.3.3",
"description": "",
"main": "dist/index.js",
"directories": {

View File

@ -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', () => {