1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
export const getHyphenatedText = (req: express.Request, res: express.Response) => {
const text = req.query.text || "";
const textList = [];
let tempTextHolder = "";
text.split("").forEach((character, index) => {
const pushToListResetTempText = () => {
textList.push(tempTextHolder.toLowerCase());
tempTextHolder = "";
};
if (tempTextHolder) {
const isUpperCaseCharater = isUpperCase(character) &&
!isNumber(character);
if (isUpperCaseCharater) {
// add text to the list
pushToListResetTempText();
}
else if (
isNumber(character) && !Number.isInteger(Number(tempTextHolder))
) {
pushToListResetTempText();
}
}
tempTextHolder += character;
if (index === text.length - 1) {
pushToListResetTempText();
}
});
return res.send(
`For text query: ${text}, hyphenated response is: ${textList.join("-")}`,
);
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023