Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed an issue with parenthesis being stripped with no validation #2

Merged
merged 1 commit into from
Jan 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions expressionEvaluators/simpleEvaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,17 @@ define([],function(){
return pieces;
};

var stripOuterBrackets = function(s){
if(s.length <= 0){
return s;
var stripOuterBrackets = function(expression){
if(expression.length <= 0){
return expression;
}

if(s[0] === "("){
s = s.substr(1);
}

if(s[s.length - 1] === ")"){
s = s.substr(0,s.length -1);
if(expression[0] === "(" && expression[expression.length - 1] === ")"){
expression = expression.substr(1);
expression = expression.substr(0,expression.length -1);
}

return s;
return expression;
}

var stripQuotes = function(s){
Expand Down
18 changes: 18 additions & 0 deletions tests/Evaluator-Tests/SimpleEvaluatorTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,22 @@ define(['expressionEvaluators/simpleEvaluator'],function(SE){

equal(value, 3);
});

test("Given an expression with the first statement out of parenthesis and the second one within the expression should evaluate", function(){
var expression = "=5+(5-1)",
expectedResult = 9;

var evaluator = new SE.SimpleEvaluator();

strictEqual(evaluator.evaluate(expression), expectedResult);
});

test("Close of parenthesis do not terminate expression evaluation", function(){
var expression = "=(5)-1",
expectedResult = 4;

var evaluator = new SE.SimpleEvaluator();

strictEqual(evaluator.evaluate(expression), expectedResult);
});
});