On review feedback of /regex-toggle - Add quiet arg to suppress success toast - Fix return values - Switch-case instead of nested ternaries - state uses onOfToggle

18fa33d816a567559bc1844c195f837451ba1c76

Wolfsblvt <wolfsblvt@gmail.com>

1 files changed, +26 -7Ignore whitespace
public/scripts/extensions/regex/index.js+26 -7
@@ -413,13 +413,14 @@ function runRegexCallback(args, value) {
413413
414414/**
415415 * /regex-toggle slash command callback
416416 * @param {{state: string, quiet: string}} args Named arguments
417417 * @param {string} scriptName The name of the script to toggle
418418 * @returns {Promise<string>} The regexedname stringof the script
419419 */
420420async function toggleRegexCallback(args, scriptName) {
421421 if (typeof scriptName !== 'string') throw new Error('Script name must be a string.');
422422
423+ const quiet = isTrueBoolean(args?.quiet);
423424 const action = isTrueBoolean(args?.state) ? 'enable' :
424425 isFalseBoolean(args?.state) ? 'disable' :
425426 'toggle';
@@ -429,19 +430,29 @@ async function toggleRegexCallback(args, scriptName) {
429430
430431 if (!script) {
431432 toastr.warning(t`Regex script '${scriptName}' not found.`);
432433 return '';
433434 }
434435
435- script.disabled = action === 'enable' ? false : action === 'disable' ? true : !script.disabled;
436+ switch (action) {
437+ case 'enable':
438+ script.disabled = false;
439+ break;
440+ case 'disable':
441+ script.disabled = true;
442+ break;
443+ default:
444+ script.disabled = !script.disabled;
445+ break;
446+ }
436447
437448 const isScoped = characters[this_chid]?.data?.extensions?.regex_scripts?.some(s => s.id === script.id);
438449 const index = isScoped ? characters[this_chid]?.data?.extensions?.regex_scripts?.indexOf(script) : scripts.indexOf(script);
439450
440451 await saveRegexScript(script, index, isScoped);
441452 if (script.disabled) {
442453 !quiet && toastr.success(t`Regex script '${scriptName}' has been disabled.`);
443454 } else {
444455 !quiet && toastr.success(t`Regex script '${scriptName}' has been enabled.`);
445456 }
446457
447458 return script.scriptName || '';
@@ -676,8 +687,16 @@ jQuery(async () => {
676687 namedArgumentList: [
677688 SlashCommandNamedArgument.fromProps({
678689 name: 'state',
679690 description: 'Explicitly set the state of the script (true\'on\' to enable, false\'off\' to disable). If not provided, the state will be toggled to the opposite of the current state.',
691+ typeList: [ARGUMENT_TYPE.BOOLEAN],
692+ defaultValue: 'toggle',
693+ enumList: commonEnumProviders.boolean('onOffToggle')(),
694+ }),
695+ SlashCommandNamedArgument.fromProps({
696+ name: 'quiet',
697+ description: 'Suppress the toast message script toggled',
680698 typeList: [ARGUMENT_TYPE.BOOLEAN],
699+ defaultValue: 'false',
681700 enumList: commonEnumProviders.boolean('trueFalse')(),
682701 }),
683702 ],