Refactor evalBoolean code

This commit is contained in:
Wolfsblvt 2024-09-22 07:58:16 +02:00
parent aea95adf60
commit cfe08d3a53
1 changed files with 13 additions and 38 deletions

View File

@ -518,8 +518,6 @@ export function parseBooleanOperands(args) {
* @returns {boolean} True if the rule yields true, false otherwise * @returns {boolean} True if the rule yields true, false otherwise
*/ */
export function evalBoolean(rule, a, b) { export function evalBoolean(rule, a, b) {
let result = false;
if (b === undefined && rule === 'eq') { if (b === undefined && rule === 'eq') {
// If right-hand side was not provided, whe just check if the left side is truthy // If right-hand side was not provided, whe just check if the left side is truthy
if (isTrueBoolean(String(a))) return true; if (isTrueBoolean(String(a))) return true;
@ -537,65 +535,42 @@ export function evalBoolean(rule, a, b) {
switch (rule) { switch (rule) {
case 'not': case 'not':
result = !aNumber; return !aNumber;
break;
case 'gt': case 'gt':
result = aNumber > bNumber; return aNumber > bNumber;
break;
case 'gte': case 'gte':
result = aNumber >= bNumber; return aNumber >= bNumber;
break;
case 'lt': case 'lt':
result = aNumber < bNumber; return aNumber < bNumber;
break;
case 'lte': case 'lte':
result = aNumber <= bNumber; return aNumber <= bNumber;
break;
case 'eq': case 'eq':
result = aNumber === bNumber; return aNumber === bNumber;
break;
case 'neq': case 'neq':
result = aNumber !== bNumber; return aNumber !== bNumber;
break;
default: default:
toastr.error('Unknown boolean comparison rule for type number.', 'Invalid command'); toastr.error('Unknown boolean comparison rule for type number.', 'Invalid command');
throw new Error('Invalid command.'); throw new Error('Invalid command.');
} }
} else { } else {
// otherwise do case-insensitive string comparsion, stringify non-strings // otherwise do case-insensitive string comparsion, stringify non-strings
let aString; let aString = (typeof a === 'string') ? a.toLowerCase() : JSON.stringify(a).toLowerCase();
let bString; let bString = (typeof b === 'string') ? b.toLowerCase() : JSON.stringify(b).toLowerCase();
if (typeof a == 'string') {
aString = a.toLowerCase();
} else {
aString = JSON.stringify(a).toLowerCase();
}
if (typeof b == 'string') {
bString = b.toLowerCase();
} else {
bString = JSON.stringify(b).toLowerCase();
}
switch (rule) { switch (rule) {
case 'in': case 'in':
result = aString.includes(bString); return aString.includes(bString);
break;
case 'nin': case 'nin':
result = !aString.includes(bString); return !aString.includes(bString);
break;
case 'eq': case 'eq':
result = aString === bString; return aString === bString;
break;
case 'neq': case 'neq':
result = aString !== bString; return aString !== bString;
break;
default: default:
toastr.error('Unknown boolean comparison rule for type string.', 'Invalid /if command'); toastr.error('Unknown boolean comparison rule for type string.', 'Invalid /if command');
throw new Error('Unknown boolean comparison rule for type string.'); throw new Error('Unknown boolean comparison rule for type string.');
} }
} }
return result;
} }
/** /**