Update eval logic to be more streamlined

a11a8fe95624271d16ea879f916332fb45dc727f

Wolfsblvt <wolfsblvt@gmail.com>

1 files changed, +34 -30Ignore whitespace
public/scripts/variables.js+34 -30
@@ -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 truthy
522 // If right-hand side was not provided, whe just check if the left side is truthy523 if (b === undefined) {
523 if (isTrueBoolean(String(a))) return true;524 switch (rule) {
524 if (isFalseBoolean(String(a))) return false;525 case undefined:
525 return !!a;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,27 +555,25 @@ 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 }
556 // otherwise do case-insensitive string comparsion, stringify non-strings
557 let aString = (typeof a === 'string') ? a.toLowerCase() : JSON.stringify(a).toLowerCase();
558 let bString = (typeof b === 'string') ? b.toLowerCase() : JSON.stringify(b).toLowerCase();
559561
560 switch (rule) {562 // otherwise do case-insensitive string comparsion, stringify non-strings
561 case 'in':563 let aString = (typeof a === 'string') ? a.toLowerCase() : JSON.stringify(a).toLowerCase();
562 return aString.includes(bString);564 let bString = (typeof b === 'string') ? b.toLowerCase() : JSON.stringify(b).toLowerCase();
563 case 'nin':565
564 return !aString.includes(bString);566 switch (rule) {
565 case 'eq':567 case 'in':
566 return aString === bString;568 return aString.includes(bString);
567 case 'neq':569 case 'nin':
568 return aString !== bString;570 return !aString.includes(bString);
569 default:571 case 'eq':
570 toastr.error('Unknown boolean comparison rule for type string.', 'Invalid /if command');572 return aString === bString;
571 throw new Error('Unknown boolean comparison rule for type string.');573 case 'neq':
572 }574 return aString !== bString;
575 default:
576 throw new Error(`Unknown boolean comparison rule for type number. Accepted: in, nin, eq, neq. Provided: ${rule}`);
573 }577 }
574}578}
575579