Merge pull request #3189 from Succubyss/substr Add /substr command

e49301308c5e141db546ef2be71056dc8e66c68d

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

Signed
1 files changed, +40 -0Ignore whitespace
public/scripts/slash-commands.js+40 -0
@@ -1918,6 +1918,46 @@ export function initDefaultSlashCommands() {
19181918 ],
19191919 helpString: 'Converts the provided string to lowercase.',
19201920 }));
1921+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
1922+ name: 'substr',
1923+ aliases: ['substring'],
1924+ callback: (arg, text) => typeof text === 'string' ? text.slice(...[Number(arg.start), arg.end && Number(arg.end)]) : '',
1925+ returns: 'substring',
1926+ namedArgumentList: [
1927+ new SlashCommandNamedArgument(
1928+ 'start', 'start index', [ARGUMENT_TYPE.NUMBER], false, false,
1929+ ),
1930+ new SlashCommandNamedArgument(
1931+ 'end', 'end index', [ARGUMENT_TYPE.NUMBER], false, false,
1932+ ),
1933+ ],
1934+ unnamedArgumentList: [
1935+ new SlashCommandArgument(
1936+ 'string', [ARGUMENT_TYPE.STRING], true, false,
1937+ ),
1938+ ],
1939+ helpString: `
1940+ <div>
1941+ Extracts text from the provided string.
1942+ </div>
1943+ <div>
1944+ If <code>start</code> is omitted, it's treated as 0.<br />
1945+ If <code>start</code> < 0, the index is counted from the end of the string.<br />
1946+ If <code>start</code> >= the string's length, an empty string is returned.<br />
1947+ If <code>end</code> is omitted, or if <code>end</code> >= the string's length, extracts to the end of the string.<br />
1948+ If <code>end</code> < 0, the index is counted from the end of the string.<br />
1949+ If <code>end</code> <= <code>start</code> after normalizing negative values, an empty string is returned.
1950+ </div>
1951+ <div>
1952+ <strong>Example:</strong>
1953+ <pre>/let x The morning is upon us. || </pre>
1954+ <pre>/substr start=-3 {{var::x}} | /echo |/# us. ||</pre>
1955+ <pre>/substr start=-3 end=-1 {{var::x}} | /echo |/# us ||</pre>
1956+ <pre>/substr end=-1 {{var::x}} | /echo |/# The morning is upon us ||</pre>
1957+ <pre>/substr start=4 end=-1 {{var::x}} | /echo |/# morning is upon us ||</pre>
1958+ </div>
1959+ `,
1960+ }));
19211961
19221962 registerVariableCommands();
19231963}