| 505 | 505 | |
| 506 | 506 | const left = getOperand(args.a ?? args.left ?? args.first ?? args.x); |
| 507 | 507 | const right = getOperand(args.b ?? args.right ?? args.second ?? args.y); |
| 508 | 508 | const rule = args.rule ?? 'eq'; |
| 509 | 509 | |
| 510 | 510 | return { a: left, b: right, rule }; |
| 511 | 511 | } |
| 512 | 512 | |
| 513 | 513 | /** |
| 514 | 514 | * Evaluates a boolean comparison rule. |
| 515 | | - * @param {string} rule Boolean comparison rule |
| 515 | + * |
| 516 | + * @param {string?} rule Boolean comparison rule |
| 516 | 517 | * @param {string|number} a The left operand |
| 517 | 518 | * @param {string|number?} b The right operand |
| 518 | 519 | * @returns {boolean} True if the rule yields true, false otherwise |
| 519 | 520 | */ |
| 520 | 521 | export function evalBoolean(rule, a, b) { |
| 521 | | - if (b === undefined && rule === 'eq') { |
| 522 | 522 | // 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 | } |
| 527 | 535 | |
| 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 |
| 529 | 537 | b = brule ??= 'eq'; |
| 530 | 538 | |
| 531 | 539 | if (typeof a === 'number' && typeof b === 'number') { |
| 532 | 540 | // only do numeric comparison if both operands are numbers |