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
506 const left = getOperand(args.a ?? args.left ?? args.first ?? args.x);506 const left = getOperand(args.a ?? args.left ?? args.first ?? args.x);
507 const right = getOperand(args.b ?? args.right ?? args.second ?? args.y);507 const right = getOperand(args.b ?? args.right ?? args.second ?? args.y);
508 const rule = args.rule ?? 'eq';508 const rule = args.rule;
509509
510 return { a: left, b: right, rule };510 return { a: left, b: right, rule };
511}511}
512512
513/**513/**
514 * Evaluates a boolean comparison rule.514 * Evaluates a boolean comparison rule.
515 * @param {string} rule Boolean comparison rule515 *
516 * @param {string?} rule Boolean comparison rule
516 * @param {string|number} a The left operand517 * @param {string|number} a The left operand
517 * @param {string|number?} b The right operand518 * @param {string|number?} b The right operand
518 * @returns {boolean} True if the rule yields true, false otherwise519 * @returns {boolean} True if the rule yields true, false otherwise
519 */520 */
520export function evalBoolean(rule, a, b) {521export function evalBoolean(rule, a, b) {
521 if (b === undefined && rule === 'eq') {
522 // If right-hand side was not provided, whe just check if the left side is truthy522 // 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 }
526 }534 }
527535
528 // Restore old behavior, where b cannot be undefined536 // If no rule was provided, we are implicitly using 'eq', as defined for the slash commands
529 b = b ?? '';537 rule ??= 'eq';
530538
531 if (typeof a === 'number' && typeof b === 'number') {539 if (typeof a === 'number' && typeof b === 'number') {
532 // only do numeric comparison if both operands are numbers540 // only do numeric comparison if both operands are numbers
@@ -534,8 +542,6 @@ export function evalBoolean(rule, a, b) {
534 const bNumber = Number(b);542 const bNumber = Number(b);
535543
536 switch (rule) {544 switch (rule) {
537 case 'not':
538 return !aNumber;
539 case 'gt':545 case 'gt':
540 return aNumber > bNumber;546 return aNumber > bNumber;
541 case 'gte':547 case 'gte':
@@ -549,10 +555,10 @@ export function evalBoolean(rule, a, b) {
549 case 'neq':555 case 'neq':
550 return aNumber !== bNumber;556 return aNumber !== bNumber;
551 default:557 default:
552 toastr.error('Unknown boolean comparison rule for type number.', 'Invalid command');558 throw new Error(`Unknown boolean comparison rule for type number. Accepted: gt, gte, lt, lte, eq, neq. Provided: ${rule}`);
553 throw new Error('Invalid command.');
554 }559 }
555 } else {560 }
561
556 // otherwise do case-insensitive string comparsion, stringify non-strings562 // otherwise do case-insensitive string comparsion, stringify non-strings
557 let aString = (typeof a === 'string') ? a.toLowerCase() : JSON.stringify(a).toLowerCase();563 let aString = (typeof a === 'string') ? a.toLowerCase() : JSON.stringify(a).toLowerCase();
558 let bString = (typeof b === 'string') ? b.toLowerCase() : JSON.stringify(b).toLowerCase();564 let bString = (typeof b === 'string') ? b.toLowerCase() : JSON.stringify(b).toLowerCase();
@@ -567,9 +573,7 @@ export function evalBoolean(rule, a, b) {
567 case 'neq':573 case 'neq':
568 return aString !== bString;574 return aString !== bString;
569 default:575 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 }
573 }577 }
574}578}
575579