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
|
|
@ -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);
|
||||
|
|
|
|||
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!" });
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue