Skip to content

Commit 33b5f53

Browse files
committed
Test functionality working
1 parent 0ac624d commit 33b5f53

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

src/app/advanced/[engine]/index.html/TestForm.tsx

+15-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import OptionsInput from './OptionsInput';
88

99
type TestFormProps = {
1010
engine: RegexEngine;
11+
testUrl?: string; // override for use during engine development
1112
testInput: TestInput;
1213
}
1314

@@ -16,8 +17,10 @@ async function runTest(test_url:string, testInput: TestInput): Promise<TestOutpu
1617
const postData =
1718
`regex=${encodeURIComponent(testInput.regex)}` +
1819
`&replacement=${encodeURIComponent(testInput.replacement)}` +
19-
`&${testInput.option.map((option) => `option=${option}`).join("&")}` +
20-
`&${testInput.inputs.map((input) => `input=${input}`).join("&")}`;
20+
`&${testInput.option.map((option) => `option=${encodeURIComponent(option)}`).join("&")}` +
21+
`&${testInput.inputs.map((input) => `input=${encodeURIComponent(input)}`).join("&")}`;
22+
23+
console.log("posting", test_url, postData);
2124

2225
const response = await fetch(test_url, {
2326
method: "POST",
@@ -45,17 +48,17 @@ export default function TestForm(props: TestFormProps) {
4548
));
4649
console.log("render", testInput.inputs);
4750

48-
4951
const onSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
5052
event.preventDefault();
5153
const form = event.currentTarget;
5254
const formData = new FormData(form);
5355
const localInput = formDataToTestInput( props.engine.handle, formData);
54-
console.log(props.engine.test_url, localInput);
56+
const testUrl = props.testUrl || props.engine.test_url;
57+
console.log(testUrl, localInput);
5558
setTestInput(localInput);
5659
setTestOutput(null);
57-
if (props.engine.test_url) {
58-
setTestOutput(await runTest(props.engine.test_url, localInput));
60+
if (testUrl) {
61+
setTestOutput(await runTest(testUrl, localInput));
5962
}
6063
};
6164

@@ -96,13 +99,17 @@ export default function TestForm(props: TestFormProps) {
9699
}
97100

98101
return (
99-
<>
102+
<>
103+
{
104+
props.testUrl ? <div className="alert alert-warning">Testing against {props.testUrl}!</div> : <></>
105+
}
100106
{(testInput.regex ?
101107
(testOutput ? <TestResults testOutput={testOutput} /> : <><h2>Results</h2><p>Loading...</p></>)
102108
: <></>)
103109
}
104110
<h2>Expression to test</h2>
105-
<form method="post" action="results.html" onSubmit={onSubmit}>
111+
<form method="get" action="index.html" onSubmit={onSubmit}>
112+
{ props.testUrl ? <input type="hidden" name="testurl" value={props.testUrl} /> : <></> }
106113
<div className="mb-3">
107114
<label htmlFor="regex" className="form-label">Regular Expression</label>
108115
<input type="text" className="form-control" id="regex" name="regex" defaultValue={testInput.regex} />

src/app/advanced/[engine]/index.html/page.tsx

+18-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import TestForm from './TestForm';
77
//import OptionsInput from './OptionsInput';
88
import { TestInput } from '@/types/TestInput';
99
import { HelpButton } from '@/components/HelpButton';
10+
import { cleanupSearchParam } from '@/functions/cleanupSearchParam';
11+
import { cleanupSearchParamArray } from '@/functions/cleanupSearchParamArray';
1012

1113
export async function generateMetadata({ params }: { params: { engine: string } }) {
1214
const engine = getEngine(params.engine);
@@ -20,7 +22,13 @@ export async function generateMetadata({ params }: { params: { engine: string }
2022
}
2123
}
2224

23-
export default function Page({ params }: { params: { engine: string } }) {
25+
export default function Page({
26+
params,
27+
searchParams,
28+
}: {
29+
params: { engine: string }
30+
searchParams: { [key: string]: string | string[] | undefined }
31+
}) {
2432
const engine = getEngine(params.engine);
2533
if (!engine) {
2634
return notFound();
@@ -39,10 +47,14 @@ export default function Page({ params }: { params: { engine: string } }) {
3947

4048
const testInput:TestInput = {
4149
engine: engine.handle,
42-
regex: '',
43-
replacement: '',
44-
option: [],
45-
inputs: ["", "", "", "", ""],
50+
regex: cleanupSearchParam(searchParams["regex"]),
51+
replacement: cleanupSearchParam(searchParams["replacement"]),
52+
option: cleanupSearchParamArray(searchParams["option"]),
53+
inputs: cleanupSearchParamArray(searchParams["input"]),
54+
}
55+
56+
while (testInput.inputs.length < 5) {
57+
testInput.inputs.push("");
4658
}
4759

4860
return (
@@ -54,7 +66,7 @@ export default function Page({ params }: { params: { engine: string } }) {
5466
<ShareLinks url={`https://regexplanet.com/advanced/${engine.handle}/index.html`} text={`Test your ${engine.short_name} regular expression`} />
5567
<hr />
5668
{flash}
57-
<TestForm engine={engine} testInput={testInput} />
69+
<TestForm engine={engine} testUrl={cleanupSearchParam(searchParams["testurl"])} testInput={testInput} />
5870
</>
5971
);
6072
/*

src/engines/bun.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,22 @@ export const bun: RegexEngine = {
2424
code: "d",
2525
description: "Generate indices for substring matches. (hasIndices)",
2626
},
27-
{ code: "g", legacyCode: "global", description: "Global search. (global)" },
27+
{ code: "g", description: "Global search. (global)" },
2828
{
2929
code: "i",
30-
legacyCode: "ignorecase",
3130
description: "Case-insensitive search. (ignoreCase)",
31+
portableCode: "ignorecase",
3232
},
3333
{
3434
code: "m",
35-
legacyCode: "multiline",
35+
portableCode: "multiline",
3636
description:
3737
"Allows ^ and $ to match next to newline characters. (multiline)",
3838
},
3939
{
4040
code: "s",
4141
description: "Allows . to match newline characters. (dotAll)",
42+
portableCode: "dotall",
4243
},
4344
{
4445
code: "u",
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function cleanupSearchParamArray(value: string | string[] | null | undefined, defaultValue?: string[]): string[] {
2+
defaultValue = defaultValue || [];
3+
4+
if (!value) {
5+
return defaultValue;
6+
}
7+
if (Array.isArray(value)) {
8+
return value;
9+
}
10+
11+
return [value];
12+
}

0 commit comments

Comments
 (0)