Skip to content

Commit

Permalink
fix: Fixed using paranthesis in 3 argument operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Marek Sierociński committed Jun 26, 2019
1 parent decd238 commit bbdc431
Show file tree
Hide file tree
Showing 11 changed files with 757 additions and 48 deletions.
79 changes: 70 additions & 9 deletions dist/meval.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* meval v0.6.0 | Copyright 2019 (c) Marek Sierociński| https://github.com/marverix/meval/blob/master/LICENSE */
/* meval v0.6.1 | Copyright 2019 (c) Marek Sierociński| https://github.com/marverix/meval/blob/master/LICENSE */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
Expand All @@ -23,8 +23,8 @@
REGEXP.NAME = '[A-Za-z$_][A-Za-z0-9$_.]*';
REGEXP.METHOD = "(".concat(REGEXP.NAME, ")\\((.*)\\)");
REGEXP.PART = "".concat(REGEXP.NAME, "(?:\\(.*\\))?");
REGEXP.PARANTHESIS = '^\\(([^)]+)\\)';
REGEXP.THREE_ARG = '^(.+?)\\?(.+):(.+)$';
REGEXP.PARANTHESIS = '(?:^| )\\(([^)]+)\\)';
REGEXP.THREE_ARG = '^.+\\?.+:.+$';
REGEXP.ACCESSOR = "(".concat(REGEXP.PART, ")\\.(").concat(REGEXP.PART, ")");
REGEXP.STRING = '(["\\\'])([^"]*)\\1';
REGEXP.FLOAT = '[0-9]+\\.[0-9]+';
Expand Down Expand Up @@ -62,6 +62,7 @@
var Constants_4 = Constants.get2ArgOpRegex;

var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
var searchFor3ArgOp = ['?', ':', null];
/**
* Evaluation of expression in given context
*
Expand Down Expand Up @@ -96,9 +97,9 @@
var parts;
expression = expression.trim(); // Check 3 argument operator

parts = expression.match(new RegExp(Constants_1.THREE_ARG));
parts = this._match3ArgOp(expression);

if (parts != null && !this._hasLostParanthesis(parts[1]) && !this._hasLostParanthesis(parts[2]) && !this._hasLostParanthesis(parts[3])) {
if (parts != null) {
return this._digest(parts[1]) ? this._digest(parts[2]) : this._digest(parts[3]);
} // Check 2 argument operators
// ||
Expand Down Expand Up @@ -327,10 +328,6 @@
var parts = expression.match(new RegExp(Constants_3(op)));

if (parts != null) {
if (this._hasLostParanthesis(parts[2])) {
return null;
}

parts[2] = this._digest(parts[2]);
}

Expand Down Expand Up @@ -359,6 +356,60 @@

return parts;
};
/**
* Match 3 argument operator
* @param {String} expression
* @returns {Array|null}
*/


this._match3ArgOp = function _match3ArgOp(expression) {
if (typeof expression !== 'string' || !new RegExp(Constants_1.THREE_ARG).test(expression)) {
return null;
}

var _char;

var state = 0;
var parts = [{
match: '',
paranthesis: 0
}, {
match: '',
paranthesis: 0
}, {
match: '',
paranthesis: 0
}];

for (var i = 0; i < expression.length; i++) {
_char = expression[i];

if (searchFor3ArgOp[state] != null && _char === searchFor3ArgOp[state] && parts[state].paranthesis === 0) {
state++;
} else {
if (_char === '(') {
parts[state].paranthesis++;
} else if (_char === ')') {
parts[state].paranthesis--;
}

parts[state].match += _char;
}
}

if (!parts[0].paranthesis && !parts[1].paranthesis && !parts[2].paranthesis) {
var ret = [expression];

for (var _i = 0; _i < 3; _i++) {
ret.push(this._stripParanthesis(parts[_i].match));
}

return ret;
} else {
return null;
}
};
/**
* Returns if given string has lost paranthesis
* @param {String} str String to check
Expand All @@ -373,6 +424,16 @@
closing = closing == null ? 0 : closing.length;
return opening !== closing;
};
/**
* Strip paranthesis
* @param {String} str String that should stripped from border paranthesis
*/


this._stripParanthesis = function _stripParanthesis(str) {
var match = str.match(/^\s*\(\s*(.+)\s*\)\s*$/);
return match == null ? str : match[1];
};
}

/**
Expand Down
4 changes: 2 additions & 2 deletions dist/meval.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions docs/Constants.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
<h1><a href="index.html" class="link">meval</a></h1>
<span class="version">

v0.5.1
v0.6.1

</span>
</div>



<div class="scroll">
<div class="nav-api hidden"><h3>Global</h3><ul><li class="nav-item"><a href="global.html#Evaluation">Evaluation</a></li><li class="nav-item"><a href="global.html#get1ArgOpRegex">get1ArgOpRegex</a></li><li class="nav-item"><a href="global.html#get2ArgOpRegex">get2ArgOpRegex</a></li><li class="nav-item"><a href="global.html#meval">meval</a></li><li class="nav-item"><a href="global.html#run">run</a></li></ul></div>
<div class="nav-api hidden"><h3>Global</h3><ul><li class="nav-item"><a href="global.html#_hasLostParanthesis">_hasLostParanthesis</a></li><li class="nav-item"><a href="global.html#_match3ArgOp">_match3ArgOp</a></li><li class="nav-item"><a href="global.html#_stripParanthesis">_stripParanthesis</a></li><li class="nav-item"><a href="global.html#Evaluation">Evaluation</a></li><li class="nav-item"><a href="global.html#get1ArgOpRegex">get1ArgOpRegex</a></li><li class="nav-item"><a href="global.html#get2ArgOpRegex">get2ArgOpRegex</a></li><li class="nav-item"><a href="global.html#meval">meval</a></li><li class="nav-item"><a href="global.html#run">run</a></li></ul></div>
</div>
</nav>

Expand All @@ -57,7 +57,8 @@ <h1><a href="index.html" class="link">meval</a></h1>
REGEXP.NAME = '[A-Za-z$_][A-Za-z0-9$_.]*';
REGEXP.METHOD = `(${REGEXP.NAME})\\((.*)\\)`;
REGEXP.PART = `${REGEXP.NAME}(?:\\(.*\\))?`;
REGEXP.THREE_ARG = '^(.+)\\?(.+):(.+)$';
REGEXP.PARANTHESIS = '(?:^| )\\(([^)]+)\\)';
REGEXP.THREE_ARG = '^.+\\?.+:.+$';
REGEXP.ACCESSOR = `(${REGEXP.PART})\\.(${REGEXP.PART})`;
REGEXP.STRING = '(["\\\'])([^"]*)\\1';
REGEXP.FLOAT = '[0-9]+\\.[0-9]+';
Expand Down
97 changes: 93 additions & 4 deletions docs/Evaluation.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
<h1><a href="index.html" class="link">meval</a></h1>
<span class="version">

v0.5.1
v0.6.1

</span>
</div>



<div class="scroll">
<div class="nav-api hidden"><h3>Global</h3><ul><li class="nav-item"><a href="global.html#Evaluation">Evaluation</a></li><li class="nav-item"><a href="global.html#get1ArgOpRegex">get1ArgOpRegex</a></li><li class="nav-item"><a href="global.html#get2ArgOpRegex">get2ArgOpRegex</a></li><li class="nav-item"><a href="global.html#meval">meval</a></li><li class="nav-item"><a href="global.html#run">run</a></li></ul></div>
<div class="nav-api hidden"><h3>Global</h3><ul><li class="nav-item"><a href="global.html#_hasLostParanthesis">_hasLostParanthesis</a></li><li class="nav-item"><a href="global.html#_match3ArgOp">_match3ArgOp</a></li><li class="nav-item"><a href="global.html#_stripParanthesis">_stripParanthesis</a></li><li class="nav-item"><a href="global.html#Evaluation">Evaluation</a></li><li class="nav-item"><a href="global.html#get1ArgOpRegex">get1ArgOpRegex</a></li><li class="nav-item"><a href="global.html#get2ArgOpRegex">get2ArgOpRegex</a></li><li class="nav-item"><a href="global.html#meval">meval</a></li><li class="nav-item"><a href="global.html#run">run</a></li></ul></div>
</div>
</nav>

Expand All @@ -60,6 +60,9 @@ <h1><a href="index.html" class="link">meval</a></h1>
get2ArgOpRegex
} from './Constants';

const commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
const searchFor3ArgOp = ['?', ':', null];

/**
* Evaluation of expression in given context
*
Expand Down Expand Up @@ -93,7 +96,7 @@ <h1><a href="index.html" class="link">meval</a></h1>
expression = expression.trim();

// Check 3 argument operator
parts = expression.match(new RegExp(REGEXP.THREE_ARG));
parts = this._match3ArgOp(expression);
if(parts != null) {
return this._digest(parts[1]) ? this._digest(parts[2]) : this._digest(parts[3]);
}
Expand Down Expand Up @@ -211,6 +214,12 @@ <h1><a href="index.html" class="link">meval</a></h1>
return typeof parts[2];
}

// Check paranthesis
parts = expression.match(new RegExp(REGEXP.PARANTHESIS));
if(parts != null) {
return this._digest(parts[1]);
}

// Check accessor
parts = expression.match(new RegExp(REGEXP.ACCESSOR));
if(parts != null) {
Expand Down Expand Up @@ -273,7 +282,7 @@ <h1><a href="index.html" class="link">meval</a></h1>

// Check allowed global objects
if(ALLOWED_GLOBAL_OBJECTS.indexOf(expression) > -1) {
return global[expression];
return commonjsGlobal[expression];
}

// Return
Expand Down Expand Up @@ -303,11 +312,91 @@ <h1><a href="index.html" class="link">meval</a></h1>
this._match2ArgOp = function _match2ArgOp(op, expression, digestRightSide = true) {
var parts = expression.match(new RegExp(get2ArgOpRegex(op)));
if(parts != null) {
if(this._hasLostParanthesis(parts[1]) || this._hasLostParanthesis(parts[3])) {
return null;
}

parts[1] = this._digest(parts[1]);
parts[3] = digestRightSide ? this._digest(parts[3]) : parts[3];
}
return parts;
};

/**
* Match 3 argument operator
* @param {String} expression
* @returns {Array|null}
*/
this._match3ArgOp = function _match3ArgOp(expression) {
if(typeof expression !== 'string' || !new RegExp(REGEXP.THREE_ARG).test(expression)) {
return null;
}

var char;
var state = 0;
var parts = [{
match: '',
paranthesis: 0
}, {
match: '',
paranthesis: 0
}, {
match: '',
paranthesis: 0
}];

for(let i = 0; i &lt; expression.length; i++) {
char = expression[i];

if(searchFor3ArgOp[state] != null &amp;&amp; char === searchFor3ArgOp[state] &amp;&amp; parts[state].paranthesis === 0) {
state++;
} else {
if(char === '(') {
parts[state].paranthesis++;
} else if(char === ')') {
parts[state].paranthesis--;
}

parts[state].match += char;
}
}

if(!parts[0].paranthesis &amp;&amp; !parts[1].paranthesis &amp;&amp; !parts[2].paranthesis) {
let ret = [expression];

for(let i = 0; i &lt; 3; i++) {
ret.push( this._stripParanthesis(parts[i].match) );
}

return ret;
} else {
return null;
}
};

/**
* Returns if given string has lost paranthesis
* @param {String} str String to check
* @returns {Boolean}
*/
this._hasLostParanthesis = function _detectLostParanthesis(str) {
var opening = str.match(/\(/g);
opening = opening == null ? 0 : opening.length;

var closing = str.match(/\)/g);
closing = closing == null ? 0 : closing.length;

return opening !== closing;
};

/**
* Strip paranthesis
* @param {String} str String that should stripped from border paranthesis
*/
this._stripParanthesis = function _stripParanthesis(str) {
var match = str.match(/^\s*\(\s*(.+)\s*\)\s*$/);
return match == null ? str : match[1];
};
}

export default Evaluation;
Expand Down
Loading

0 comments on commit bbdc431

Please sign in to comment.