-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstring.ts
More file actions
33 lines (29 loc) · 992 Bytes
/
string.ts
File metadata and controls
33 lines (29 loc) · 992 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
export const truncate = (value: string, maxLength: number) => {
if (value?.length ?? 0 <= maxLength) {
return value;
} else {
return value.substring(0, maxLength - 3) + "...";
}
};
/**
* Truncates HTML content by first stripping HTML tags, then truncating the plain text
* @param htmlContent - HTML string to truncate
* @param maxLength - Maximum length of the plain text
* @returns Truncated plain text with ellipsis
*/
export const truncateHtml = (htmlContent: string, maxLength: number): string => {
if (!htmlContent) return "";
// Create a temporary div to parse HTML and get text content
const tempDiv = document.createElement("div");
tempDiv.innerHTML = htmlContent;
const textContent = tempDiv.textContent || "";
// Use the regular truncate function on the plain text
return truncate(textContent, maxLength);
};
export const isValidHttpUrl = (value: string) => {
try {
return Boolean(new URL(value));
} catch (_) {
return false;
}
};