-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.ts
More file actions
36 lines (29 loc) · 809 Bytes
/
text.ts
File metadata and controls
36 lines (29 loc) · 809 Bytes
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
const stripBalancedTags = (value: string): string => {
let cursor = 0;
let output = "";
while (cursor < value.length) {
const tagStart = value.indexOf("<", cursor);
if (tagStart === -1) {
output += value.slice(cursor);
break;
}
const tagEnd = value.indexOf(">", tagStart + 1);
if (tagEnd === -1) {
output += value.slice(cursor);
break;
}
output += value.slice(cursor, tagStart);
output += " ";
cursor = tagEnd + 1;
}
return output;
};
export const normalizeWhitespace = (value: string): string => {
return value.replace(/\s+/g, " ").trim();
};
export const cleanText = (value: string | undefined | null): string => {
if (typeof value !== "string") {
return "";
}
return normalizeWhitespace(stripBalancedTags(value));
};