Merge pull request #13 from theoleuthardt/feat/regex-tester

feat: regex-tester fully implemented
This commit is contained in:
Theo Leuthardt 2025-02-20 20:52:38 +01:00 committed by GitHub
commit 1399809ebb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 323 additions and 130 deletions

View file

@ -30,6 +30,7 @@ files, generate content, or using other handy digital utilities. This page is ma
To install and run the project locally, follow these steps: To install and run the project locally, follow these steps:
1. **Clone the repository**: 1. **Clone the repository**:
```bash ```bash
git clone https://github.com/theoleuthardt/werkzeugkiste.git git clone https://github.com/theoleuthardt/werkzeugkiste.git
cd werkzeugkiste cd werkzeugkiste

View file

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

View file

@ -18,7 +18,8 @@ export async function colorConvert(app: FastifyInstance) {
if (!data) { if (!data) {
return reply.status(400).send({ error: "No RGB declared!" }); return reply.status(400).send({ error: "No RGB declared!" });
} }
const hex = (`#${(+data.red).toString(16).padStart(2, "0")}${(+data.green).toString(16).padStart(2, "0")}${(+data.blue).toString(16).padStart(2, "0")}`).toUpperCase(); const hex =
`#${(+data.red).toString(16).padStart(2, "0")}${(+data.green).toString(16).padStart(2, "0")}${(+data.blue).toString(16).padStart(2, "0")}`.toUpperCase();
reply.header("Content-Type", "text/plain").status(200).send(hex); reply.header("Content-Type", "text/plain").status(200).send(hex);
} catch (error) { } catch (error) {
console.error("Convert error:", error); console.error("Convert error:", error);

View file

@ -18,7 +18,9 @@ export async function passwordGenerate(app: FastifyInstance) {
try { try {
const data = request.body; const data = request.body;
if (!data) { if (!data) {
return reply.status(400).send({ error: "No length or type declared!" }); return reply
.status(400)
.send({ error: "No length or type declared!" });
} }
let numArray: string[] = "0123456789".split(""); let numArray: string[] = "0123456789".split("");
let lowerArray: string[] = "abcdefghijklmnopqrstuvwxyz".split(""); let lowerArray: string[] = "abcdefghijklmnopqrstuvwxyz".split("");
@ -26,19 +28,19 @@ export async function passwordGenerate(app: FastifyInstance) {
let specialArray: string[] = "!@#$%^&*()_+[]{}|;:,.<>?".split(""); let specialArray: string[] = "!@#$%^&*()_+[]{}|;:,.<>?".split("");
let passwordArray: string[] = []; let passwordArray: string[] = [];
if(data.numb){ if (data.numb) {
passwordArray = passwordArray.concat(numArray); passwordArray = passwordArray.concat(numArray);
} }
if(data.lower){ if (data.lower) {
passwordArray = passwordArray.concat(lowerArray); passwordArray = passwordArray.concat(lowerArray);
} }
if(data.upper){ if (data.upper) {
passwordArray = passwordArray.concat(upperArray); passwordArray = passwordArray.concat(upperArray);
} }
if(data.special){ if (data.special) {
passwordArray = passwordArray.concat(specialArray); passwordArray = passwordArray.concat(specialArray);
} }

View file

@ -0,0 +1,52 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
interface RequestBody {
regex: string;
test: 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!" });
}
if (!data.regex || !data.test) {
return reply
.status(400)
.send({ error: "Regex or test string missing!" });
}
let regexPattern;
try {
regexPattern = new RegExp(data.regex);
} catch (e) {
return reply
.status(400)
.send({ error: "Invalid regular expression!" });
}
const result = regexPattern.test(data.test);
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!" });
}
},
);
}

View file

@ -3,13 +3,13 @@ import type { Metadata } from "next";
import { toolLinks } from "@/constants"; import { toolLinks } from "@/constants";
export const metadata: Metadata = { export const metadata: Metadata = {
title: toolLinks[2].title, title: toolLinks[5].title,
description: "Generator for secure strong passwords!", description: "Generator for secure strong passwords!",
}; };
export default function RootLayout({ export default function RootLayout({
children, children,
}: Readonly<{ }: Readonly<{
children: React.ReactNode; children: React.ReactNode;
}>) { }>) {
return ( return (

View file

@ -6,19 +6,21 @@ import Footer from "../../components/Footer";
import Button from "../../components/Button"; import Button from "../../components/Button";
export default function RgbToHex() { export default function RgbToHex() {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
const generatePassword = async () => { const generatePassword = async () => {
setLoading(true); setLoading(true);
const length = (document.getElementById("length") as HTMLInputElement).value; const length = (document.getElementById("length") as HTMLInputElement)
.value;
const numb = (document.getElementById("numb") as HTMLInputElement).checked; const numb = (document.getElementById("numb") as HTMLInputElement).checked;
const lower = (document.getElementById("lower") as HTMLInputElement).checked; const lower = (document.getElementById("lower") as HTMLInputElement)
const upper = (document.getElementById("upper") as HTMLInputElement).checked; .checked;
const special = (document.getElementById("special") as HTMLInputElement).checked; const upper = (document.getElementById("upper") as HTMLInputElement)
.checked;
const special = (document.getElementById("special") as HTMLInputElement)
.checked;
try { try {
const response = await fetch( const response = await fetch(
@ -28,7 +30,13 @@ export default function RgbToHex() {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ length: length, numb: numb, lower: lower, upper: upper, special: special }), body: JSON.stringify({
length: length,
numb: numb,
lower: lower,
upper: upper,
special: special,
}),
}, },
); );
if (!response.ok) { if (!response.ok) {
@ -37,7 +45,6 @@ export default function RgbToHex() {
const password: string = await response.text(); const password: string = await response.text();
console.log(password); console.log(password);
setPassword(password); setPassword(password);
} catch (error) { } catch (error) {
console.error("Error while converting:", error); console.error("Error while converting:", error);
alert("Error while converting"); alert("Error while converting");
@ -61,10 +68,10 @@ export default function RgbToHex() {
}; };
const checkInput = (event: React.ChangeEvent<HTMLInputElement>) => { const checkInput = (event: React.ChangeEvent<HTMLInputElement>) => {
const length = (document.getElementById(event.target.id) as HTMLInputElement); const length = document.getElementById(event.target.id) as HTMLInputElement;
const lengthValue = +length.value; const lengthValue = +length.value;
if (lengthValue < 1 || lengthValue > 64 ) { if (lengthValue < 1 || lengthValue > 64) {
alert("Invalid input. Please enter a number between 1 and 64."); alert("Invalid input. Please enter a number between 1 and 64.");
} }
if (lengthValue < 1) { if (lengthValue < 1) {
@ -72,20 +79,32 @@ export default function RgbToHex() {
} else if (lengthValue > 64) { } else if (lengthValue > 64) {
length.value = "64"; length.value = "64";
} }
} };
return ( return (
<div className="h-screen w-screen bg-black text-white font-noto flex flex-col items-center"> <div className="h-screen w-screen bg-black text-white font-noto flex flex-col items-center">
<Navbar renderHomeLink={true} /> <Navbar renderHomeLink={true} />
<div className="w-screen h-screen flex flex-col items-center justify-center"> <div className="w-screen h-screen flex flex-col items-center justify-center">
<h2 className="text-5xl font-bold text-white mb-16">password-generator</h2> <h2 className="text-5xl font-bold text-white mb-16">
password-generator
</h2>
<div className="border-2 border-white p-3 rounded-xl text-center text-white flex flex-row justify-between"> <div className="border-2 border-white p-3 rounded-xl text-center text-white flex flex-row justify-between">
<div className="flex flex-col items-start"> <div className="flex flex-col items-start">
<label className="mx-2" htmlFor="length">length:</label> <label className="mx-2" htmlFor="length">
<label className="mx-2 text-white" htmlFor="numb">numbers</label> length:
<label className="mx-2" htmlFor="lower">lower case characters</label> </label>
<label className="mx-2" htmlFor="upper">upper case characters</label> <label className="mx-2 text-white" htmlFor="numb">
<label className="mx-2" htmlFor="special">special characters</label> numbers
</label>
<label className="mx-2" htmlFor="lower">
lower case characters
</label>
<label className="mx-2" htmlFor="upper">
upper case characters
</label>
<label className="mx-2" htmlFor="special">
special characters
</label>
</div> </div>
<div className="flex flex-col items-end justify-evenly gap-2"> <div className="flex flex-col items-end justify-evenly gap-2">
<input <input
@ -134,16 +153,12 @@ export default function RgbToHex() {
} }
onClick={generatePassword} onClick={generatePassword}
/> />
<Button <Button content="clear" onClick={clearInAndOutput} />
content="clear"
onClick={clearInAndOutput}
/>
</div> </div>
<div className="p-3 rounded-xl text-center"> <div className="p-3 rounded-xl text-center">
<output <output id="password" className="text-blue-400 text-3xl">
id="password" {password}
className="text-blue-400 text-3xl" </output>
>{password}</output>
</div> </div>
</div> </div>
<Footer /> <Footer />

View file

@ -0,0 +1,20 @@
import React from "react";
import type { Metadata } from "next";
import { toolLinks } from "@/constants";
export const metadata: Metadata = {
title: toolLinks[3].title,
description: "Tester for regular expressions!",
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body className={`antialiased`}>{children}</body>
</html>
);
}

View file

@ -0,0 +1,102 @@
"use client";
import React, { useState } from "react";
import Navbar from "../../components/Navbar";
import Footer from "../../components/Footer";
import Button from "../../components/Button";
export default function RgbToHex() {
const [loading, setLoading] = useState(false);
const [output, setOutput] = useState("");
const testRegex = async () => {
setLoading(true);
const regex = (document.getElementById("regex") as HTMLInputElement).value;
const test = (document.getElementById("test") as HTMLInputElement).value;
try {
const response = await fetch(
process.env.backend_url + "/api/regex-test",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ regex: regex, test: test }),
},
);
if (!response.ok) {
return new Error(`Error: ${response.statusText}`);
}
const output: string = await response.text();
console.log(output);
setOutput(output);
} catch (error) {
console.error("Error while converting:", error);
alert("Error while converting");
} finally {
setLoading(false);
}
};
const clearInAndOutput = () => {
setOutput("");
const regex = document.getElementById("regex") as HTMLInputElement;
const test = document.getElementById("test") as HTMLInputElement;
test.value = "";
regex.value = "";
};
return (
<div className="h-screen w-screen bg-black text-white font-noto flex flex-col items-center">
<Navbar renderHomeLink={true} />
<div className="w-screen h-screen flex flex-col items-center justify-center">
<h2 className="text-5xl font-bold text-white mb-16">regex-tester</h2>
<div className="border-2 border-white p-3 rounded-xl text-center text-white flex flex-col justify-between">
<div>
<label className="mr-2 m" htmlFor="regex">
regex:
</label>
<input
type="text"
id="regex"
name="regex"
className="field-sizing-content bg-black border-1 border-white text-center mb-3"
/>
</div>
<div>
<label className="mr-2" htmlFor="test">
test string:
</label>
<input
type="text"
id="test"
name="test"
className="field-sizing-content bg-black border-1 border-white text-center"
/>
</div>
</div>
<div className={"flex flex-row items-center gap-4 mt-4 mb-16"}>
<Button
content={
loading ? (
<div className="h-10 w-10 border-8 border-blue-100 border-t-blue-500 rounded-full animate-spin" />
) : (
"test"
)
}
onClick={testRegex}
/>
<Button content="clear" onClick={clearInAndOutput} />
</div>
<div className="p-3 rounded-xl text-center">
<output id="output" className="text-blue-400 text-3xl">
{output}
</output>
</div>
</div>
<Footer />
</div>
);
}

View file

@ -9,7 +9,7 @@ export const metadata: Metadata = {
export default function RootLayout({ export default function RootLayout({
children, children,
}: Readonly<{ }: Readonly<{
children: React.ReactNode; children: React.ReactNode;
}>) { }>) {
return ( return (

View file

@ -6,12 +6,10 @@ import Footer from "../../components/Footer";
import Button from "../../components/Button"; import Button from "../../components/Button";
export default function RgbToHex() { export default function RgbToHex() {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [hex, setHex] = useState(""); const [hex, setHex] = useState("");
const convertToHex = async () => { const convertToHex = async () => {
setLoading(true); setLoading(true);
const red = (document.getElementById("red") as HTMLInputElement).value; const red = (document.getElementById("red") as HTMLInputElement).value;
@ -34,7 +32,6 @@ export default function RgbToHex() {
} }
const hex: string = await response.text(); const hex: string = await response.text();
setHex(hex); setHex(hex);
} catch (error) { } catch (error) {
console.error("Error while converting:", error); console.error("Error while converting:", error);
alert("Error while converting"); alert("Error while converting");
@ -54,10 +51,10 @@ export default function RgbToHex() {
}; };
const checkInput = (event: React.ChangeEvent<HTMLInputElement>) => { const checkInput = (event: React.ChangeEvent<HTMLInputElement>) => {
const color = (document.getElementById(event.target.id) as HTMLInputElement); const color = document.getElementById(event.target.id) as HTMLInputElement;
const colorValue = +color.value; const colorValue = +color.value;
if (colorValue < 0 || colorValue > 255 ) { if (colorValue < 0 || colorValue > 255) {
alert("Invalid input. Please enter a number between 0 and 255."); alert("Invalid input. Please enter a number between 0 and 255.");
} }
if (colorValue < 0) { if (colorValue < 0) {
@ -65,7 +62,7 @@ export default function RgbToHex() {
} else if (colorValue > 255) { } else if (colorValue > 255) {
color.value = "255"; color.value = "255";
} }
} };
return ( return (
<div className="h-screen w-screen bg-black text-white font-noto flex flex-col items-center"> <div className="h-screen w-screen bg-black text-white font-noto flex flex-col items-center">
@ -73,7 +70,9 @@ export default function RgbToHex() {
<div className="w-screen h-screen flex flex-col items-center justify-center"> <div className="w-screen h-screen flex flex-col items-center justify-center">
<h2 className="text-5xl font-bold text-white mb-16">rgb-to-hex</h2> <h2 className="text-5xl font-bold text-white mb-16">rgb-to-hex</h2>
<div className="border-2 border-white p-3 rounded-xl text-center text-white"> <div className="border-2 border-white p-3 rounded-xl text-center text-white">
<label className="mr-2" htmlFor="red">Red:</label> <label className="mr-2" htmlFor="red">
Red:
</label>
<input <input
type="number" type="number"
id="red" id="red"
@ -83,7 +82,9 @@ export default function RgbToHex() {
className="w-16 bg-black border-1 border-white text-center" className="w-16 bg-black border-1 border-white text-center"
onInput={checkInput} onInput={checkInput}
/> />
<label className="mx-2 text-white" htmlFor="green">Green:</label> <label className="mx-2 text-white" htmlFor="green">
Green:
</label>
<input <input
type="number" type="number"
id="green" id="green"
@ -93,7 +94,9 @@ export default function RgbToHex() {
className="w-16 bg-black border-1 border-white text-center" className="w-16 bg-black border-1 border-white text-center"
onChange={checkInput} onChange={checkInput}
/> />
<label className="mx-2" htmlFor="blue">Blue:</label> <label className="mx-2" htmlFor="blue">
Blue:
</label>
<input <input
type="number" type="number"
id="blue" id="blue"
@ -115,16 +118,12 @@ export default function RgbToHex() {
} }
onClick={convertToHex} onClick={convertToHex}
/> />
<Button <Button content="clear" onClick={clearInAndOutput} />
content="clear"
onClick={clearInAndOutput}
/>
</div> </div>
<div className="p-3 rounded-xl text-center"> <div className="p-3 rounded-xl text-center">
<output <output id="hex" className="text-blue-400 text-3xl">
id="hex" {hex}
className="text-blue-400 text-3xl" </output>
>{hex}</output>
</div> </div>
</div> </div>
<Footer /> <Footer />

View file

@ -12,8 +12,8 @@ export const toolLinks = [
link: "/rgb-to-hex", link: "/rgb-to-hex",
}, },
{ {
title: "data-visualizer", title: "regex-tester",
link: "/data-visualizer", link: "/regex-tester",
}, },
{ {
title: "qr-code-generator", title: "qr-code-generator",

View file

@ -22,7 +22,6 @@
"@/*": ["./src/*"] "@/*": ["./src/*"]
} }
}, },
"include": [ "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"] "exclude": ["node_modules"]
} }