-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstring.ts
More file actions
40 lines (33 loc) · 1.16 KB
/
string.ts
File metadata and controls
40 lines (33 loc) · 1.16 KB
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
export type EmptyString = `${""}`;
export const EMPTY_STRING: EmptyString = "";
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;
}
};
export const utf8StringToBase64 = (value: string): string =>
Buffer.from(value, "utf8").toString("base64");