Validate pattern

729830c2fc443867f1cc83055f2a53711e3763ce

Crow <bismuthglass@protonmail.com>

1 files changed, +8 -2Showing whitespace changes
public/scripts/slash-commands.js+8 -2
@@ -2134,10 +2134,13 @@ export function initDefaultSlashCommands() {
2134 SlashCommandParser.addCommandObject(SlashCommand.fromProps({2134 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2135 name: 'test',2135 name: 'test',
2136 callback: (({ pattern }, text) => {2136 callback: (({ pattern }, text) => {
2137 if (pattern === '') {2137 if (!pattern) {
2138 throw new Error('Argument of \'pattern=\' cannot be empty');2138 throw new Error('Argument of \'pattern=\' cannot be empty');
2139 }2139 }
2140 let re = regexFromString(pattern.toString());2140 let re = regexFromString(pattern.toString());
2141 if (!re) {
2142 throw new Error('The value of \'pattern\' argument is not a valid regular expression.');
2143 }
2141 return JSON.stringify(re.test(text.toString()));2144 return JSON.stringify(re.test(text.toString()));
2142 }),2145 }),
2143 returns: 'true | false',2146 returns: 'true | false',
@@ -2170,10 +2173,13 @@ export function initDefaultSlashCommands() {
2170 SlashCommandParser.addCommandObject(SlashCommand.fromProps({2173 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2171 name: 'match',2174 name: 'match',
2172 callback: (({ pattern }, text) => {2175 callback: (({ pattern }, text) => {
2173 if (pattern === '') {2176 if (!pattern) {
2174 throw new Error('Argument of \'pattern=\' cannot be empty');2177 throw new Error('Argument of \'pattern=\' cannot be empty');
2175 }2178 }
2176 let re = regexFromString(pattern.toString());2179 let re = regexFromString(pattern.toString());
2180 if (!re) {
2181 throw new Error('The value of \'pattern\' argument is not a valid regular expression.');
2182 }
2177 if (re.flags.includes('g')) {2183 if (re.flags.includes('g')) {
2178 return JSON.stringify([...text.toString().matchAll(re)]);2184 return JSON.stringify([...text.toString().matchAll(re)]);
2179 } else {2185 } else {