fix: RegExp operand's in logic to evaluator. (#293)

* fix: RegExp operand's in logic to evaluator.

* Add <> operator comparison.
pull/3759/head
EdwinBetanc0urt 2020-01-31 18:02:44 -04:00 committed by GitHub
parent e4729a4841
commit c51f09c745
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 73 additions and 62 deletions

View File

@ -17,7 +17,7 @@ class evaluator {
* @param {string} parentUuid Parent (Window / Process / Smart Browser) * @param {string} parentUuid Parent (Window / Process / Smart Browser)
*/ */
static evaluateLogic(objectToEvaluate) { static evaluateLogic(objectToEvaluate) {
var defaultUndefined = false let defaultUndefined = false
if (objectToEvaluate.type === 'displayed') { if (objectToEvaluate.type === 'displayed') {
defaultUndefined = true defaultUndefined = true
} }
@ -27,22 +27,19 @@ class evaluator {
objectToEvaluate.logic.trim() === '') { objectToEvaluate.logic.trim() === '') {
return defaultUndefined return defaultUndefined
} }
var st = objectToEvaluate.logic.trim().replace('\n', '') let st = objectToEvaluate.logic.trim().replace('\n', '')
st = st.replace('|', '~') st = st.replace('|', '~')
var expr = /(~|&)/ const expr = /(~|&)/
var stList = st.split(expr) const stList = st.split(expr)
var it = stList.length const it = stList.length
if (((it / 2) - ((it + 1) / 2)) === 0) { if (((it / 2) - ((it + 1) / 2)) === 0) {
console.info( console.info(`Logic does not comply with format "<expression> [<logic> <expression>]" --> ${objectToEvaluate.logic}`)
'Logic does not comply with format "<expression> [<logic> <expression>]"' +
' --> ' + objectToEvaluate.logic
)
return defaultUndefined return defaultUndefined
} }
var retValue = null let retValue = null
var logOp = '' let logOp = ''
stList.forEach(function(element) { stList.forEach(function(element) {
if (element === '~' || element === '&') { if (element === '~' || element === '&') {
logOp = element logOp = element
@ -63,7 +60,7 @@ class evaluator {
conditional: element conditional: element
}) })
} else { } else {
console.info("Logic operant '|' or '&' expected --> " + objectToEvaluate.logic) console.info(`Logic operant '|' or '&' expected --> ${objectToEvaluate.logic}`)
return defaultUndefined return defaultUndefined
} }
} }
@ -78,53 +75,44 @@ class evaluator {
* @return {boolean} * @return {boolean}
*/ */
static evaluateLogicTuples(objectToEvaluate) { static evaluateLogicTuples(objectToEvaluate) {
var _defaultUndefined = false let _defaultUndefined = false
if (objectToEvaluate.type === 'displayed') { if (objectToEvaluate.type === 'displayed') {
_defaultUndefined = true _defaultUndefined = true
} }
var logic = objectToEvaluate.conditional const logic = objectToEvaluate.conditional
// not context info, not logic // not context info, not logic
if (logic === undefined) { if (logic === undefined) {
return _defaultUndefined return _defaultUndefined
} }
var expr = /^(['"@#$a-zA-Z0-9\-_\s]){0,}((!*={1})|(!{1})|(<{1})|(>{1}))([\s"'@#$a-zA-Z0-9\-_]){0,}$/i let expr = /^(['"@#$a-zA-Z0-9\-_\s]){0,}((<>|<=|<|=|>=|>|!=|!|\^){1,2})([\s"'@#$a-zA-Z0-9\-_]){0,}$/i
var st = expr.test(logic) let st = expr.test(logic)
if (!st) { if (!st) {
console.info( console.info(`.Logic tuple does not comply with format '@context@=value' where operand could be one of '=!^><' --> ${logic}`)
".Logic tuple does not comply with format '@context@=value' where operand" +
" could be one of '=!^><' --> " + logic
)
return _defaultUndefined return _defaultUndefined
} }
expr = /(!{1}|={1})/
expr = /(<>|<=|<|=|>=|>|!=|!|\^){1,2}/i
st = logic.split(expr) st = logic.split(expr)
if (st.length === 1) {
expr = /(<{1})/
st = logic.split(expr)
}
if (st.length === 1) {
expr = /(>)/
st = logic.split(expr)
}
// First Part (or column name) // First Part (or column name)
var first = st[0].trim() let first = st[0].trim()
var firstEval = first let firstEval = first
let isCountable = false
let isGlobal = false
expr = /@/ expr = /@/
if (expr.test(first)) { if (expr.test(first)) {
first = first.replace(/@/g, '').trim() first = first.replace(/@/g, '').trim()
var isGlobal = first.startsWith('#') isGlobal = first.startsWith('#')
var isCountable = first.startsWith('$') isCountable = first.startsWith('$')
var value = objectToEvaluate.context.getContext({ const value = objectToEvaluate.context.getContext({
parentUuid: (isGlobal || isCountable) ? '' : objectToEvaluate.parentUuid, parentUuid: (isGlobal || isCountable) ? '' : objectToEvaluate.parentUuid,
containerUuid: (isGlobal || isCountable) ? '' : objectToEvaluate.containerUuid, containerUuid: (isGlobal || isCountable) ? '' : objectToEvaluate.containerUuid,
columnName: first columnName: first
}) })
// in context exists this column name // in context exists this column name
if (value === null || value === undefined) { if (value === null || value === undefined) {
console.info( console.info(`.The column ${first} not exists in context.`)
'.The column ' + first + ' not exists in context.'
)
return _defaultUndefined return _defaultUndefined
} }
firstEval = value // replace with it's value firstEval = value // replace with it's value
@ -138,10 +126,10 @@ class evaluator {
} }
// Operator // Operator
var operand = st[1] const operand = st[1]
// Second Part // Second Part
var second = st[2].trim() let second = st[2].trim()
var secondEval = second.trim() let secondEval = second.trim()
if (expr.test(second)) { if (expr.test(second)) {
second = second.replace(/@/g, ' ').trim() // strip tag second = second.replace(/@/g, ' ').trim() // strip tag
secondEval = objectToEvaluate.context.getContext({ secondEval = objectToEvaluate.context.getContext({
@ -163,7 +151,7 @@ class evaluator {
} }
// Logical Comparison // Logical Comparison
var result = this.evaluateLogicTuple(firstEval, operand, secondEval) const result = this.evaluateLogicTuple(firstEval, operand, secondEval)
return result return result
} }
@ -190,19 +178,42 @@ class evaluator {
value2 = false value2 = false
} }
if (value1 == null || operand == null || value2 == null) { if ([value1, operand, value2].includes(null)) {
return false return false
} }
if (operand === '=') {
return value1 === value2 let isValueLogic
} else if (operand === '<') { // TODO: Add '^' operand comparison
return value1 < value2 switch (operand) {
} else if (operand === '>') { case '=':
return value1 > value2 case '==':
} else { isValueLogic = value1 === value2
// interpreted as not break
return value1 !== value2
case '<':
isValueLogic = value1 < value2
break
case '<=':
isValueLogic = value1 <= value2
break
case '>':
isValueLogic = value1 > value2
break
case '>=':
isValueLogic = value1 >= value2
break
case '!':
case '!=':
case '<>':
default:
isValueLogic = value1 !== value2
break
} }
return isValueLogic
} }
/** /**
@ -211,7 +222,7 @@ class evaluator {
* @return {array} * @return {array}
*/ */
static parseDepends(parseString) { static parseDepends(parseString) {
var listFields = [] const listFields = []
if (parseString === null || parseString === undefined) { if (parseString === null || parseString === undefined) {
// return array empty // return array empty
return listFields return listFields