-
Notifications
You must be signed in to change notification settings - Fork 1
/
text-processor.js
40 lines (29 loc) · 986 Bytes
/
text-processor.js
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
32
33
34
35
36
37
38
39
40
const formatLine = (line) => {
const words = line.split(' ');
for (let i = 0; i < words.length; i++) {
if (/^[^a-zA-Z0-9]*$/.test(words[i])) { // If word is not alphanumeric
words[i] = "";
} else if (words[i].length === 1 && words[i] !== "I") {
words[i] = "";
} else {
break;
}
}
return words.join(' ');
};
const formatText = (text) => {
if (!text) return '';
const lines = text.split('\n');
const formattedLines = lines.map(line => {
if(/(?=.*\b[A-Z][a-z]*\b)(?=.*\b\d{1,2}:\d{2}(?:[ap]m)?\b)/.test(formatLine(line))){
return `${formatLine(line)} \n`;
}
if(/^(?=.*\b[A-Z][a-z]*\b)(?=.*\b\d{1,2}(?:st|nd|rd|th)\b)/.test(formatLine(line))){
return `${formatLine(line)} \n`;
}
return `\t ${formatLine(line)}`;
});
console.log(formattedLines);
return formattedLines.join('\n');
};
module.exports = { formatText };