StringUtils.ts
4.04 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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);
}
//手机号校验
static photoMatch(phone:string) : boolean {
/**
* 移动号段正则表达式
*/
let CM_NUM = "^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
/**
* 联通号段正则表达式
*/
let CU_NUM = "^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
/**
* 电信号段正则表达式
*/
let CT_NUM = "^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
/**
* 官网正则
*/
let GW_NUM = "^1(3|4|5|6|7|8|9)\\d{9}$";
let CM_NUM_reg : RegExp = new RegExp(CM_NUM);
let CU_NUM_reg : RegExp = new RegExp(CU_NUM);
let CT_NUM_reg : RegExp = new RegExp(CT_NUM);
let GW_NUM_reg : RegExp = new RegExp(GW_NUM);
if (CM_NUM_reg.test(phone) || CU_NUM_reg.test(phone) || CT_NUM_reg.test(phone) || GW_NUM_reg.test(phone)) {
return true;
}
console.log('请输入正确的手机号')
return false;
}
}
// export default new StringUtils();