diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 45aea41a0..5cb76d06d 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -1918,6 +1918,46 @@ export function initDefaultSlashCommands() { ], helpString: 'Converts the provided string to lowercase.', })); + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'substr', + aliases: ['substring'], + callback: (arg, text) => typeof text === 'string' ? text.slice(...[Number(arg.start), arg.end && Number(arg.end)]) : '', + returns: 'substring', + namedArgumentList: [ + new SlashCommandNamedArgument( + 'start', 'start index', [ARGUMENT_TYPE.NUMBER], false, false, + ), + new SlashCommandNamedArgument( + 'end', 'end index', [ARGUMENT_TYPE.NUMBER], false, false, + ), + ], + unnamedArgumentList: [ + new SlashCommandArgument( + 'string', [ARGUMENT_TYPE.STRING], true, false, + ), + ], + helpString: ` +
+ Extracts text from the provided string. +
+
+ If start is omitted, it's treated as 0.
+ If start < 0, the index is counted from the end of the string.
+ If start >= the string's length, an empty string is returned.
+ If end is omitted, or if end >= the string's length, extracts to the end of the string.
+ If end < 0, the index is counted from the end of the string.
+ If end <= start after normalizing negative values, an empty string is returned. +
+
+ Example: +
/let x The morning is upon us.     ||                                     
+
/substr start=-3 {{var::x}}         | /echo  |/# us.                    ||
+
/substr start=-3 end=-1 {{var::x}}  | /echo  |/# us                     ||
+
/substr end=-1 {{var::x}}           | /echo  |/# The morning is upon us ||
+
/substr start=4 end=-1 {{var::x}}   | /echo  |/# morning is upon us     ||
+
+ `, + })); registerVariableCommands(); }