feat(slash-commands): add stringify option to /array-wrap command for JSON primitive handling

963786758bd4c3dc7e150289ee32c0dbf9729d83

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +16 -1Ignore whitespace
public/scripts/slash-commands.js+16 -1
@@ -3645,6 +3645,15 @@ export function initDefaultSlashCommands() {
36453645 aliases: ['list-wrap'],
36463646 returns: t`unnamed argument value wrapped into an array`,
36473647 helpString: t`Wraps a single unnamed argument into an array if it's not already an array. If the value is an empty string, returns an empty array.`,
3648+ namedArgumentList: [
3649+ SlashCommandNamedArgument.fromProps({
3650+ name: 'stringify',
3651+ description: t`Whether JSON primitives (numbers, booleans, nulls) should be treated as strings, i.e. ["null"] when stringify=true vs. [null] when stringify=false.`,
3652+ typeList: [ARGUMENT_TYPE.BOOLEAN],
3653+ defaultValue: 'true',
3654+ enumList: commonEnumProviders.boolean('trueFalse')(),
3655+ }),
3656+ ],
36483657 unnamedArgumentList: [
36493658 SlashCommandArgument.fromProps({
36503659 description: t`value`,
@@ -3653,7 +3662,7 @@ export function initDefaultSlashCommands() {
36533662 typeList: [ARGUMENT_TYPE.STRING, ARGUMENT_TYPE.DICTIONARY, ARGUMENT_TYPE.BOOLEAN, ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.LIST],
36543663 }),
36553664 ],
36563665 callback: (_argsargs, value) => {
36573666 // Closures are not supported
36583667 if (value instanceof SlashCommandClosure) {
36593668 throw new SlashCommandExecutionError(t`Closures are not supported as unnamed arguments for /array-wrap. Did you forget to call the closure with parentheses?`);
@@ -3683,6 +3692,12 @@ export function initDefaultSlashCommands() {
36833692 return JSON.stringify([parsedValue]);
36843693 }
36853694
3695+ // For primitive values, check if we should take the parsed or original value based on the stringify argument
3696+ const isJsonPrimitive = parsedValue === null || ['string', 'number', 'boolean'].includes(typeof parsedValue);
3697+ if (isJsonPrimitive && isFalseBoolean(String(args?.stringify?.toString()))) {
3698+ return JSON.stringify([parsedValue]);
3699+ }
3700+
36863701 // Wrap the original value (string, number, boolean) into an array, preserving quotes for strings
36873702 return JSON.stringify([value]);
36883703 } catch {