feat: word-counter fully implemented + lower case revision

This commit is contained in:
Domenik 2025-02-21 11:53:06 +01:00
parent ccca78cec0
commit 8cf282e127
7 changed files with 148 additions and 7 deletions

View file

@ -6,6 +6,7 @@ import { colorConvert } from "./src/routes/colorconvert.route";
import { passwordGenerate } from "./src/routes/passwordgenerate.route";
import { regexTest } from "./src/routes/regextest.route";
import { tmzConvert } from "./src/routes/tmzconvert.route";
import { wordCounter } from "./src/routes/wordcounter.route";
const app = Fastify({ logger: true });
@ -21,6 +22,7 @@ app.register(colorConvert);
app.register(passwordGenerate);
app.register(regexTest);
app.register(tmzConvert);
app.register(wordCounter);
const PORT = process.env.PORT || 4000;
app.listen({ port: Number(PORT), host: "0.0.0.0" }, () => {

View file

@ -34,12 +34,12 @@ export async function regexTest(app: FastifyInstance) {
}
const result = regexPattern.test(data.test);
let output = "";
if (result) {
output = `The input matches the regular expression!`;
output = "the input matches the regular expression!";
} 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);

View 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!" });
}
},
);
}