diff --git a/src/runTest.ts b/src/runTest.ts new file mode 100644 index 0000000..ec0522d --- /dev/null +++ b/src/runTest.ts @@ -0,0 +1,209 @@ +import { type TestInput, type TestOutput } from "./types"; + +function h(unsafe: string): string { + if (unsafe == null) { + return ""; + } + + return unsafe + .replace(/&/g, "&") + .replace(//g, ">") + .replace(/"/g, """) + .replace(/'/g, "'"); +} + +export function runTest(input: TestInput): TestOutput { + if (input.regex == null || input.regex.length == 0) { + return { success: false, message: "No regex to test!" }; + } + + const options = input.option ? input.option.join("") : undefined; + + const html = []; + html.push( + '
Regular Expression | \n"); + html.push("\t\t"); + html.push(h(input.regex)); + html.push(" | \n"); + html.push("\t
Replacement | \n"); + html.push("\t\t"); + html.push(h(input.replacement)); + html.push(" | \n"); + + html.push("\t
Options | \n"); + html.push("\t\t"); + html.push(options ? h(options) : "(none)"); + html.push(" | \n"); + html.push("\t
Test | \n'); + html.push("\t\t\tInput | "); + html.push("\t\t\tregex.test() | "); + html.push("\t\t\tinput.replace() | "); + html.push("\t\t\tinput.replaceAll() | "); + html.push("\t\t\tinput.split()[] | "); + html.push("\t\t\tregex.exec().index | "); + html.push("\t\t\tregex.exec()[] | "); + html.push("\t\t\tregex.lastIndex | "); + html.push("\t\t|||
---|---|---|---|---|---|---|---|---|---|---|---|
'); + html.push(loop + 1); + html.push(" | \n"); + + html.push("\t\t\t"); + html.push(h(target)); + html.push(" | \n"); + + html.push("\t\t\t"); + html.push( + new RegExp(input.regex, options).test(target) ? "true" : "false" + ); + html.push(" | \n"); + + html.push("\t\t\t"); + html.push( + h(target.replace(new RegExp(input.regex, options), input.replacement)) + ); + html.push(" | \n"); + + html.push("\t\t\t"); + try { + html.push( + h( + target.replaceAll( + new RegExp(input.regex, options), + input.replacement + ) + ) + ); + } catch (replaceAllErr) { + const message = + replaceAllErr instanceof Error + ? replaceAllErr.message + : `unknown error ${replaceAllErr}`; + html.push(`${message}`); + } + html.push(" | \n"); + + html.push("\t\t\t");
+ var splits = target.split(new RegExp(input.regex, options));
+ for (var split = 0; split < splits.length; split++) {
+ html.push("[");
+ html.push(split);
+ html.push("]: ");
+ html.push(splits[split] == null ? "(null)" : h(splits[split]));
+ html.push(" "); + } + html.push(" | \n");
+
+ var regex = new RegExp(input.regex, options);
+ var result = regex.exec(target);
+ if (result == null) {
+ html.push('\t\t\t(null) | \n'); + } else { + var first = true; + + while (result != null) { + if (first == true) { + first = false; + } else { + html.push("'); + html.push("regex.exec()"); + html.push(" | \n"); + } + + html.push("\t\t\t"); + html.push(result.index); + html.push(" | \n"); + + html.push("\t\t\t");
+ for (var capture = 0; capture < result.length; capture++) {
+ html.push("[");
+ html.push(capture);
+ html.push("]: ");
+ html.push(
+ result[capture] == null ? "(null)" : h(result[capture])
+ );
+ html.push(" "); + } + html.push(" | \n");
+
+ html.push("\t\t\t"); + html.push(regex.lastIndex); + html.push(" | \n"); + + result = global ? regex.exec(target) : null; + } + } + html.push("\t\t\n"); + count++; + } + } + + if (count == 0) { + html.push("\t\t|
'); + html.push("(no input to test)"); + html.push(" | \n"); + html.push("\t\t