-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.ts
More file actions
71 lines (53 loc) · 1.84 KB
/
strings.ts
File metadata and controls
71 lines (53 loc) · 1.84 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { reservedWords } from "../libs/words";
const prepareWords = (value: string): string => {
value = value.replace(/([^\w\d]|[-/_])+/g, " ");
for (const word of reservedWords) {
value = value.replace(
new RegExp(`\\b${word}\\b`, "i"),
word.toLowerCase(),
);
}
return value;
};
const normalizeWords = (value: string): string => {
for (const word of reservedWords) {
value = value.replace(new RegExp(`\\b${word}\\b`, "i"), word);
}
return value;
};
export const titleCase = (title?: string) => {
if (title === "" || title === undefined) {
return "";
}
const upper: string = title.toUpperCase();
if (upper === title) {
title = title.toLowerCase();
}
title = prepareWords(title)
.replace(/([A-Z])/g, " $1")
.toLowerCase()
.replace(/(^|\s|-|_)\S/g, (match: string) => match.toUpperCase())
.replace(/(\s|\u3164|\u1160)+/gu, " ")
.trim();
const normalized = normalizeWords(title);
return normalized.charAt(0).toUpperCase() + normalized.slice(1);
};
export const removeImages = (content: string): string =>
content
.replace(/^(#\s+.+[\n\s]+)\s*<picture>[.\w\W]+<\/picture>[\n\s]*/, "$1")
.replace(/^(#\s+.+[\n\s]+)(!\[.+]\(.*\)\n?){1,2}[\n\s]*/, "$1")
.replace(/^(#\s+.+[\n\s]+)(<img\s.*\/>\n?){1,2}[\n\s]*/, "$1");
export const encodeUri = (value: string | undefined): string => {
if (value === "" || value === undefined) {
return "";
}
return encodeURIComponent(value);
};
export const randomString = (length: number = 8) => {
const chars = "abcdefghijklmnopqrstuvwxyz";
let result: string = "";
for (let i: number = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
};