mirror of
https://github.com/theoleuthardt/werkzeugkiste.git
synced 2026-06-13 09:37:53 +00:00
feat: word-counter fully implemented + lower case revision
This commit is contained in:
parent
ccca78cec0
commit
8cf282e127
7 changed files with 148 additions and 7 deletions
|
|
@ -6,6 +6,7 @@ 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";
|
import { regexTest } from "./src/routes/regextest.route";
|
||||||
import { tmzConvert } from "./src/routes/tmzconvert.route";
|
import { tmzConvert } from "./src/routes/tmzconvert.route";
|
||||||
|
import { wordCounter } from "./src/routes/wordcounter.route";
|
||||||
|
|
||||||
const app = Fastify({ logger: true });
|
const app = Fastify({ logger: true });
|
||||||
|
|
||||||
|
|
@ -21,6 +22,7 @@ app.register(colorConvert);
|
||||||
app.register(passwordGenerate);
|
app.register(passwordGenerate);
|
||||||
app.register(regexTest);
|
app.register(regexTest);
|
||||||
app.register(tmzConvert);
|
app.register(tmzConvert);
|
||||||
|
app.register(wordCounter);
|
||||||
|
|
||||||
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" }, () => {
|
||||||
|
|
|
||||||
|
|
@ -34,12 +34,12 @@ export async function regexTest(app: FastifyInstance) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = regexPattern.test(data.test);
|
const result = regexPattern.test(data.test);
|
||||||
|
|
||||||
let output = "";
|
let output = "";
|
||||||
if (result) {
|
if (result) {
|
||||||
output = `The input matches the regular expression!`;
|
output = "the input matches the regular expression!";
|
||||||
} else {
|
} else {
|
||||||
output = `The input does not match the regular expression.`;
|
output = "the input does not match the regular expression.";
|
||||||
}
|
}
|
||||||
|
|
||||||
reply.header("Content-Type", "text/plain").status(200).send(output);
|
reply.header("Content-Type", "text/plain").status(200).send(output);
|
||||||
|
|
|
||||||
34
backend/src/routes/wordcounter.route.ts
Normal file
34
backend/src/routes/wordcounter.route.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||||
|
|
||||||
|
interface RequestBody {
|
||||||
|
input: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function wordCounter(app: FastifyInstance) {
|
||||||
|
app.post(
|
||||||
|
"/api/word-counter",
|
||||||
|
async (
|
||||||
|
request: FastifyRequest<{ Body: RequestBody }>,
|
||||||
|
reply: FastifyReply,
|
||||||
|
) => {
|
||||||
|
try {
|
||||||
|
const data = request.body;
|
||||||
|
if (!data) {
|
||||||
|
return reply.status(400).send({ error: "No text declared!" });
|
||||||
|
} else if (data.input == "") {
|
||||||
|
return reply.status(400).send({ error: "No text!" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const output = data.input.trim().split(/\s+/).length;
|
||||||
|
|
||||||
|
reply
|
||||||
|
.header("Content-Type", "text/plain")
|
||||||
|
.status(200)
|
||||||
|
.send(output.toString());
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Convert error:", error);
|
||||||
|
reply.status(500).send({ error: "Error while converting!" });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -5,7 +5,7 @@ import Navbar from "../../components/Navbar";
|
||||||
import Footer from "../../components/Footer";
|
import Footer from "../../components/Footer";
|
||||||
import Button from "../../components/Button";
|
import Button from "../../components/Button";
|
||||||
|
|
||||||
export default function RgbToHex() {
|
export default function PasswordGenerator() {
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [password, setPassword] = useState("");
|
const [password, setPassword] = useState("");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,7 @@ export default function RgbToHex() {
|
||||||
<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">
|
<label className="mr-2" htmlFor="red">
|
||||||
Red:
|
red:
|
||||||
</label>
|
</label>
|
||||||
<input
|
<input
|
||||||
type="number"
|
type="number"
|
||||||
|
|
@ -83,7 +83,7 @@ export default function RgbToHex() {
|
||||||
onInput={checkInput}
|
onInput={checkInput}
|
||||||
/>
|
/>
|
||||||
<label className="mx-2 text-white" htmlFor="green">
|
<label className="mx-2 text-white" htmlFor="green">
|
||||||
Green:
|
green:
|
||||||
</label>
|
</label>
|
||||||
<input
|
<input
|
||||||
type="number"
|
type="number"
|
||||||
|
|
@ -95,7 +95,7 @@ export default function RgbToHex() {
|
||||||
onChange={checkInput}
|
onChange={checkInput}
|
||||||
/>
|
/>
|
||||||
<label className="mx-2" htmlFor="blue">
|
<label className="mx-2" htmlFor="blue">
|
||||||
Blue:
|
blue:
|
||||||
</label>
|
</label>
|
||||||
<input
|
<input
|
||||||
type="number"
|
type="number"
|
||||||
|
|
|
||||||
20
frontend/src/app/word-counter/layout.tsx
Normal file
20
frontend/src/app/word-counter/layout.tsx
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
import React from "react";
|
||||||
|
import type { Metadata } from "next";
|
||||||
|
import { toolLinks } from "@/constants";
|
||||||
|
|
||||||
|
export const metadata: Metadata = {
|
||||||
|
title: toolLinks[7].title,
|
||||||
|
description: "Counter for words in text!",
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function RootLayout({
|
||||||
|
children,
|
||||||
|
}: Readonly<{
|
||||||
|
children: React.ReactNode;
|
||||||
|
}>) {
|
||||||
|
return (
|
||||||
|
<html lang="en">
|
||||||
|
<body className={`antialiased`}>{children}</body>
|
||||||
|
</html>
|
||||||
|
);
|
||||||
|
}
|
||||||
85
frontend/src/app/word-counter/page.tsx
Normal file
85
frontend/src/app/word-counter/page.tsx
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
"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 WordCounter() {
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [output, setOutput] = useState("");
|
||||||
|
|
||||||
|
const countWords = async () => {
|
||||||
|
setLoading(true);
|
||||||
|
|
||||||
|
const input = (document.getElementById("input") as HTMLInputElement).value;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(
|
||||||
|
process.env.backend_url + "/api/word-counter",
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ input: input }),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
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 input = document.getElementById("input") as HTMLInputElement;
|
||||||
|
input.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">word-counter</h2>
|
||||||
|
<div className="border-2 border-white p-3 rounded-xl text-center text-white flex flex-col justify-between">
|
||||||
|
<label className="mr-2 m" htmlFor="input">
|
||||||
|
input text:
|
||||||
|
</label>
|
||||||
|
<textarea
|
||||||
|
id="input"
|
||||||
|
name="input"
|
||||||
|
className="resize max-h-80 max-w-screen-md field-sizing-content bg-black border-1 border-white text-center"
|
||||||
|
/>
|
||||||
|
</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" />
|
||||||
|
) : (
|
||||||
|
"count words"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
onClick={countWords}
|
||||||
|
/>
|
||||||
|
<Button content="clear" onClick={clearInAndOutput} />
|
||||||
|
</div>
|
||||||
|
<div className="p-3 rounded-xl text-center">
|
||||||
|
<output id="output" className="text-blue-400 text-3xl">
|
||||||
|
{output && "word count: " + output}
|
||||||
|
</output>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<Footer />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue