-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringUtils.ts
118 lines (98 loc) · 3.41 KB
/
StringUtils.ts
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
export default class StringUtils {
public isNull = (obj) => {
return obj === null || obj === undefined || obj === "";
}
public isNotNull = (obj) => {
return obj !== null && obj !== undefined && obj !== "";
}
public firstToUpperCase = (str: string): string => {
return str[0].toUpperCase() + str.substring(1);
}
public capitalizeFirstLetter = (input: string) => {
return input.charAt(0).toUpperCase() + input.slice(1);
}
public toCapital = (str: string): string => {
return str[0].toUpperCase() + str.slice(1);
}
public toTitleCase = (str: string): string => {
let result: string = "";
const strToArray: string[] = str.split(" ");
for (let i = 0; i < strToArray.length; i++) {
result += " " + this.toCapital(strToArray[i]);
}
return result.trimLeft();
}
public textToSlug = (str: string): string => {
return str.replace(/[\s.]/g, "-");
}
public slugToText = (str: string): string => {
return str.replace(/[-]/g, " ");
}
public capitalizeString = (str: string): string => {
if (str === "") {
return str
}
const firstLetter = str.slice(0, 1)
return `${firstLetter.toUpperCase()}${str.slice(1, str.length)}`
}
public generateUID = (str: string): string => {
return (
Math.random().toString(36).substr(2, 9) +
Math.random().toString(36).substr(2, 9)
);
}
public isEmpty = (input: string = '', removeSpaces: boolean = true): boolean => {
return (
removeSpaces
? input.replace(/\s/g, '').length === 0
: input.length === 0
);
}
public normalizeString = (str: string) => {
return str.toLowerCase().trim()
}
public randomString = (length: number) => {
let charset =
'0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._~';
let result = '';
while (length > 0) {
let bytes = new Uint8Array(16);
let random = window.crypto.getRandomValues(bytes);
/* eslint-disable-next-line no-loop-func */
random.forEach(c => {
if (length === 0) {
return;
}
if (c < charset.length) {
result += charset[c];
length--;
}
});
}
return result;
}
public htmlToString = (html: string) => {
return html?.replace(/(<([^>]+)>)/gi, "");
}
public jsonString = (value: any) => {
return JSON.stringify(Array.from(value.entries()));
}
public truncateString = (string: string, length: number, ending: string) => {
if (length === null) {
length = 100;
}
if (ending === null) {
ending = ' ...';
}
if (string?.length > length) {
return string.substring(0, length - ending.length) + ending;
} else {
return string;
}
};
public stripTags = (input: string, allowed: string) => {
allowed = (((allowed || '') + '').toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');
const tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi;
return input.replace(tags, ($0, $1) => (allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : ''));
};
}