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

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