StringUtils.ts
1.21 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
/**
* StringUtils class.
*/
export class StringUtils {
/**
* Check obj is not empty.
*
* @param {object} obj
* @return {boolean} true(not empty)
*/
static isNotEmpty(obj: any): boolean {
// return (obj && obj !== '');
// 或
return (obj !== undefined && obj !== null && obj !== '');
}
static isEmpty(...params: any): boolean {
if (params.length === 0) {
return true;
}
for (const param of params) {
if (!param) { // param === undefined || param === null || param === '';
return true;
}
}
return false;
}
static formatStringForJS(template: string, ...args: any[]): string {
let formattedString = template;
for (const arg of args) {
formattedString = formattedString.replace(/%s/, arg.toString());
}
return formattedString;
}
static escapeDoubleQuotes(obj:any): any {
if (typeof obj === 'string') {
return obj.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
} else if (typeof obj === 'object') {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
obj[key] = StringUtils.escapeDoubleQuotes(obj[key]);
}
}
}
return obj;
}
}
// export default new StringUtils();