regex-tester first version implemented

This commit is contained in:
Domenik 2025-02-20 11:29:17 +01:00
parent bbcfcddedf
commit 8787ee5c2b
5 changed files with 163 additions and 2 deletions

View file

@ -4,6 +4,7 @@ import multipart from "@fastify/multipart";
import { libreConvert } from "./src/routes/libreconvert.route";
import { colorConvert } from "./src/routes/colorconvert.route";
import {passwordGenerate} from "./src/routes/passwordgenerate.route";
import { regexTest } from "./src/routes/regextest.route";
const app = Fastify({ logger: true });
@ -17,6 +18,7 @@ app.register(multipart);
app.register(libreConvert);
app.register(colorConvert);
app.register(passwordGenerate);
app.register(regexTest);
const PORT = process.env.PORT || 4000;
app.listen({ port: Number(PORT), host: "0.0.0.0" }, () => {

View file

@ -0,0 +1,48 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
interface RequestBody {
input: string;
}
export async function regexTest(app: FastifyInstance) {
app.post(
"/api/regex-test",
async (
request: FastifyRequest<{ Body: RequestBody }>,
reply: FastifyReply,
) => {
try {
const data = request.body;
if (!data) {
return reply.status(400).send({ error: "No Regex declared!" });
}
const regexPattern = data.input;
// Überprüfe, ob der Regex-Pattern korrekt ist
let regex: RegExp;
try {
regex = new RegExp(regexPattern);
} catch (e) {
return reply.status(400).send({ error: "Invalid regular expression!" });
}
// Teste den Regex
const result = regex.test(data.input);
// Erstelle eine Ausgabe basierend auf dem Test-Ergebnis
let output = "";
if (result) {
output = `The input matches the regular expression!`;
} else {
output = `The input does not match the regular expression.`;
}
reply.header("Content-Type", "text/plain").status(200).send(output);
} catch (error) {
console.error("Convert error:", error);
reply.status(500).send({ error: "Error while converting!" });
}
},
);
}