Skip to content

Commit 187555d

Browse files
committed
Simplify braced detection
1 parent 3124de6 commit 187555d

File tree

1 file changed

+2
-21
lines changed

1 file changed

+2
-21
lines changed

lib/operators.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,6 @@ var opComplexity = {
4444

4545
var opsRE = /\*|\+|\-|\/|\%|\<|\=|\>|\&|\||\!|\^|\~/;
4646

47-
/**
48-
* Check if expression is protected by parentheses
49-
*/
50-
function isBraced (exp) {
51-
if (/\s/.test(exp)) {
52-
// a + b
53-
if (exp[0] !== '(') return false;
54-
55-
var level = 1;
56-
var len = exp.length;
57-
for (let i = 1; i < len - 1; i++) {
58-
if (exp[i] === '(') level++;
59-
else if (exp[i] === ')') level--;
60-
// (a + b) * c + d
61-
if (level === 0) return false;
62-
}
63-
}
64-
return true;
65-
}
6647

6748
/**
6849
* Return rendered operation
@@ -306,15 +287,15 @@ function processOperation (left, right, operator) {
306287

307288
// embrace complex operators
308289
if (operator != '+' && operator != '-') {
309-
if (!isBraced(left)) opResult += '(' + left + ')'
290+
if (/\s/.test(left) && !(left[0] == '(' && left[left.length - 1] == ')')) opResult += '(' + left + ')'
310291
else opResult += left
311292
}
312293
else opResult += left
313294

314295
opResult += ' ' + operator + ' '
315296

316297
if (operator != '+' && operator != '-') {
317-
if (!isBraced(right)) opResult += '(' + right + ')'
298+
if (/\s/.test(right) && !(right[0] == '(' && right[right.length - 1] == ')')) opResult += '(' + right + ')'
318299
else opResult += right
319300
}
320301
else opResult += right

0 commit comments

Comments
 (0)