StringUtils.ts
3.11 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
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
119
120
121
122
123
124
125
126
127
128
129
130
const SLEEP_TIME: number = 10;
/**
* StringUtils class.
*/
export class StringUtils {
/**
* The String utils tag.
*/
private static readonly TAG: string = 'StringUtils';
// 评分-整数或浮点数
private static readonly SCORE_REG_EXP = RegExp(/^(\d+)(\.\d+)?$/);
/**
* Check string is empty.
*
* @param {object} any
* @return {boolean} true(empty)
*/
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;
}
/**
* 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 isObject(value: any): boolean {
return Object.prototype.toString.call(value) === '[object Object]';
}
static isNotNullObject(value: unknown): boolean {
return value !== null ? typeof value === 'object' : false;
}
static isNullOrUndefined(value: any): boolean {
return value === undefined || value === null;
}
static isFunction(object: any): boolean {
return typeof object === 'function';
}
static isValidObject(objectList: any): boolean {
if (!objectList || typeof objectList !== 'object'
|| objectList.constructor.name === 'Object') {
return false;
}
return true;
}
static isClass(object: any): boolean {
if (StringUtils.isFunction(object)) {
const prototype = object.prototype;
if (!StringUtils.isNotNullObject(prototype)) {
return false;
}
const constructor = prototype.constructor;
if (StringUtils.isFunction(constructor)) {
return true;
}
}
return false;
}
static getClassName(clz: any): string {
if (StringUtils.isNullOrUndefined(clz)) {
return clz;
}
let input = clz;
if (!StringUtils.isClass(input)) {
input = clz.constructor;
}
return input.className ? input.className : input.name;
}
static hash(value: string | number): string {
const str = '' + value;
let hash = 5381;
let index = str.length;
while (index) {
hash = (hash * 33) ^ str.charCodeAt(--index);
}
return '' + (hash >>> 0);
}
static parseNumber(input: string): number {
// let input: string = '135' // 135
// let input: string = '135T' // NaN
// let input: string = 'ytr' // NaN
// let input: string = '' // 0
// let input: string = null // 0
// let input: string = undefined // NaN
const parsedInt = Number(input)
if (!isNaN(parsedInt)) {
return parsedInt
} else {
return 0
}
}
static async sleep(times: number): Promise<void> {
if (!times) {
times = SLEEP_TIME;
}
await new Promise((res) => setTimeout(res, times)) // .then().catch().finally();
}
static isScore(text: string): boolean {
return StringUtils.SCORE_REG_EXP.test(text);
}
}
// export default new StringUtils();