Skip to content

Commit

Permalink
Removed bug when solving +2 resulted in -2 and added more test for er…
Browse files Browse the repository at this point in the history
…ror catching)
  • Loading branch information
Ankit committed Aug 25, 2016
1 parent a9b1f99 commit d96c375
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 19 deletions.
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "math-expression-evaluator",
"version": "1.0.2",
"version": "1.2.13",
"description": "A flexible math expression evaluator",
"main": "dist/browser/math-expression-evaluator.js",
"keywords": [
Expand Down
20 changes: 12 additions & 8 deletions dist/browser/math-expression-evaluator.js
@@ -1,5 +1,5 @@
/** math-expression-evaluator version 1.2.13
Dated:2016-08-23 */
/** math-expression-evaluator version 1.2.14
Dated:2016-08-25 */

(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.mexp = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/**
Expand Down Expand Up @@ -505,6 +505,7 @@ var indexOf = require('lodash.indexof');
throw(new Mexp.exception("Can't understand after "+inpStr.slice(i)));
}
var index=indexOf(token,key);
var cToken=key;
var cType=type[index];
var cEv=eva[index];
var cPre=preced[cType];
Expand Down Expand Up @@ -625,12 +626,15 @@ var indexOf = require('lodash.indexof');
inc(ptc,1);
}
}
else if(pre.type!==5&&pre.type!==7&&pre.type!==1&&pre.type!==3&&pre.type!==13){
allowed=type0;
asterick=empty;
inc(ptc,2).push(2);
str.push({value:Mexp.math.changeSign,type:0,pre:21,show:"-"});
str.push({value:"(",type:4,pre:0,show:"("});
else if(pre.type!==5&&pre.type!==7&&pre.type!==1&&pre.type!==3&&pre.type!==13){//changesign only when negative is found
if(cToken==='-'){//do nothing for + token
//don't add with the above if statement as that will run the else statement of parent if on Ctoken +
allowed=type0;
asterick=empty;
inc(ptc,2).push(2);
str.push({value:Mexp.math.changeSign,type:0,pre:21,show:"-"});
str.push({value:"(",type:4,pre:0,show:"("});
}
}
else{
str.push(obj);
Expand Down
6 changes: 3 additions & 3 deletions dist/browser/math-expression-evaluator.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "math-expression-evaluator",
"version": "1.2.13",
"version": "1.2.14",
"description": "A flexible math expression evaluator",
"main": "src/formula_evaluator.js",
"scripts": {
Expand Down
16 changes: 10 additions & 6 deletions src/lexer.js
Expand Up @@ -126,6 +126,7 @@ var indexOf = require('lodash.indexof');
throw(new Mexp.exception("Can't understand after "+inpStr.slice(i)));
}
var index=indexOf(token,key);
var cToken=key;
var cType=type[index];
var cEv=eva[index];
var cPre=preced[cType];
Expand Down Expand Up @@ -246,12 +247,15 @@ var indexOf = require('lodash.indexof');
inc(ptc,1);
}
}
else if(pre.type!==5&&pre.type!==7&&pre.type!==1&&pre.type!==3&&pre.type!==13){
allowed=type0;
asterick=empty;
inc(ptc,2).push(2);
str.push({value:Mexp.math.changeSign,type:0,pre:21,show:"-"});
str.push({value:"(",type:4,pre:0,show:"("});
else if(pre.type!==5&&pre.type!==7&&pre.type!==1&&pre.type!==3&&pre.type!==13){//changesign only when negative is found
if(cToken==='-'){//do nothing for + token
//don't add with the above if statement as that will run the else statement of parent if on Ctoken +
allowed=type0;
asterick=empty;
inc(ptc,2).push(2);
str.push({value:Mexp.math.changeSign,type:0,pre:21,show:"-"});
str.push({value:"(",type:4,pre:0,show:"("});
}
}
else{
str.push(obj);
Expand Down
50 changes: 50 additions & 0 deletions test/index.js
Expand Up @@ -113,4 +113,54 @@ describe('Testing Unit', function () {
it('checks root function', function () {
assert.equal(a.eval("root4"),"2");
});
it('checks + precedence before number insise parenthesis ', function () {
assert.equal(a.eval("(-2)"),"-2");
});
it('checks multiple allowable operator', function () {
assert.equal(a.eval("2+++-++-+-+3"),"-1");
assert.equal(a.eval("2*+3"),"6");
});
});
describe('These expression will raise error', function () {
it('should tell to compllete expression', function () {
try{
a.eval("2*")
}
catch(e){
assert.equal(e.message,"complete the expression")
}
});
it('should warn about multiple operators', function () {
try{
a.eval("2**3")
}
catch(e){
assert.equal(e.message,"* is not allowed after *")
}
});
it('should warn about multiple operators', function () {
try{
a.eval("2*Mod*3")
}
catch(e){
assert.equal(e.message,"Mod is not allowed after *")
}
});
it('should warn about operator inside parenthesis', function () {
try{
a.eval("(+)")
}
catch(e){
assert.equal(e.message,") is not allowed after +")
}
});
it('should warn about operator inside parenthesis', function () {
try{
a.eval("(+)")
}
catch(e){
assert.equal(e.message,") is not allowed after +")
}
});

});

0 comments on commit d96c375

Please sign in to comment.