Update eval logic to be more streamlined

a11a8fe95624271d16ea879f916332fb45dc727f

Wolfsblvt <wolfsblvt@gmail.com>

1 files changed, +20 -16Showing whitespace changes
public/scripts/variables.js+20 -16
@@ -505,28 +505,36 @@ export function parseBooleanOperands(args) {
505505
506506 const left = getOperand(args.a ?? args.left ?? args.first ?? args.x);
507507 const right = getOperand(args.b ?? args.right ?? args.second ?? args.y);
508508 const rule = args.rule ?? 'eq';
509509
510510 return { a: left, b: right, rule };
511511}
512512
513513/**
514514 * Evaluates a boolean comparison rule.
515- * @param {string} rule Boolean comparison rule
515+ *
516+ * @param {string?} rule Boolean comparison rule
516517 * @param {string|number} a The left operand
517518 * @param {string|number?} b The right operand
518519 * @returns {boolean} True if the rule yields true, false otherwise
519520 */
520521export function evalBoolean(rule, a, b) {
521- if (b === undefined && rule === 'eq') {
522522 // If right-hand side was not provided, whe just check if the left side is truthy
523- if (isTrueBoolean(String(a))) return true;
523+ if (b === undefined) {
524- if (isFalseBoolean(String(a))) return false;
524+ switch (rule) {
525- return !!a;
525+ case undefined:
526+ case 'not':
527+ const resultOnTruthy = rule !== 'not';
528+ if (isTrueBoolean(String(a))) return resultOnTruthy;
529+ if (isFalseBoolean(String(a))) return !resultOnTruthy;
530+ return !!a ? resultOnTruthy : !resultOnTruthy;
531+ default:
532+ throw new Error(`Unknown boolean comparison rule for truthy check. If right-hand side is not provided, the rule must not provided or be "not". Provided: ${rule}`);
533+ }
526534 }
527535
528- // Restore old behavior, where b cannot be undefined
536+ // If no rule was provided, we are implicitly using 'eq', as defined for the slash commands
529537 b = brule ??= 'eq';
530538
531539 if (typeof a === 'number' && typeof b === 'number') {
532540 // only do numeric comparison if both operands are numbers
@@ -534,8 +542,6 @@ export function evalBoolean(rule, a, b) {
534542 const bNumber = Number(b);
535543
536544 switch (rule) {
537- case 'not':
538- return !aNumber;
539545 case 'gt':
540546 return aNumber > bNumber;
541547 case 'gte':
@@ -549,10 +555,10 @@ export function evalBoolean(rule, a, b) {
549555 case 'neq':
550556 return aNumber !== bNumber;
551557 default:
552558 toastr.errorthrow new Error('`Unknown boolean comparison rule for type number.' Accepted: gt, 'Invalidgte, command'lt, lte, eq, neq. Provided: ${rule}`);
553- throw new Error('Invalid command.');
554559 }
555560 } else {
561+
556562 // otherwise do case-insensitive string comparsion, stringify non-strings
557563 let aString = (typeof a === 'string') ? a.toLowerCase() : JSON.stringify(a).toLowerCase();
558564 let bString = (typeof b === 'string') ? b.toLowerCase() : JSON.stringify(b).toLowerCase();
@@ -567,9 +573,7 @@ export function evalBoolean(rule, a, b) {
567573 case 'neq':
568574 return aString !== bString;
569575 default:
570- toastr.error('Unknown boolean comparison rule for type string.', 'Invalid /if command');
576+ throw new Error(`Unknown boolean comparison rule for type number. Accepted: in, nin, eq, neq. Provided: ${rule}`);
571- throw new Error('Unknown boolean comparison rule for type string.');
572- }
573577 }
574578}
575579