@whitesev/utils 2.8.2 → 2.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +176 -176
- package/dist/index.amd.js +269 -777
- package/dist/index.amd.js.map +1 -1
- package/dist/index.amd.min.js +2 -0
- package/dist/index.amd.min.js.map +1 -0
- package/dist/index.cjs.js +269 -777
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +2 -0
- package/dist/index.cjs.min.js.map +1 -0
- package/dist/index.esm.js +269 -777
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +2 -0
- package/dist/index.esm.min.js.map +1 -0
- package/dist/index.iife.js +269 -777
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +2 -0
- package/dist/index.iife.min.js.map +1 -0
- package/dist/index.system.js +269 -777
- package/dist/index.system.js.map +1 -1
- package/dist/index.system.min.js +2 -0
- package/dist/index.system.min.js.map +1 -0
- package/dist/index.umd.js +269 -777
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +2 -0
- package/dist/index.umd.min.js.map +1 -0
- package/dist/types/src/Utils.d.ts +103 -450
- package/dist/types/src/UtilsGMCookie.d.ts +4 -0
- package/dist/types/src/UtilsGMMenu.d.ts +3 -6
- package/dist/types/src/types/Httpx.d.ts +1344 -1344
- package/dist/types/src/types/Log.d.ts +19 -19
- package/dist/types/src/types/Progress.d.ts +20 -20
- package/dist/types/src/types/React.d.ts +119 -119
- package/dist/types/src/types/TryCatch.d.ts +9 -9
- package/dist/types/src/types/UtilsGMCookie.d.ts +93 -93
- package/dist/types/src/types/UtilsGMMenu.d.ts +77 -77
- package/dist/types/src/types/Vue2.d.ts +166 -166
- package/dist/types/src/types/WindowApi.d.ts +14 -14
- package/dist/types/src/types/ajaxHooker.d.ts +151 -151
- package/dist/types/src/types/env.d.ts +7 -7
- package/dist/types/src/types/global.d.ts +31 -31
- package/package.json +13 -9
- package/src/ColorConversion.ts +105 -105
- package/src/CommonUtil.ts +280 -280
- package/src/DOMUtils.ts +251 -251
- package/src/Dictionary.ts +153 -153
- package/src/GBKEncoder.ts +108 -108
- package/src/Hooks.ts +73 -73
- package/src/Httpx.ts +1457 -1457
- package/src/LockFunction.ts +62 -62
- package/src/Log.ts +258 -258
- package/src/Progress.ts +108 -108
- package/src/TryCatch.ts +86 -86
- package/src/Utils.ts +3778 -4773
- package/src/UtilsCommon.ts +14 -14
- package/src/UtilsGMCookie.ts +272 -254
- package/src/UtilsGMMenu.ts +441 -445
- package/src/Vue.ts +233 -233
- package/src/WindowApi.ts +59 -59
- package/src/indexedDB.ts +497 -497
- package/src/types/Httpx.d.ts +1344 -1344
- package/src/types/Log.d.ts +19 -19
- package/src/types/Progress.d.ts +20 -20
- package/src/types/React.d.ts +119 -119
- package/src/types/TryCatch.d.ts +9 -9
- package/src/types/UtilsGMCookie.d.ts +93 -93
- package/src/types/UtilsGMMenu.d.ts +77 -77
- package/src/types/Vue2.d.ts +166 -166
- package/src/types/WindowApi.d.ts +14 -14
- package/src/types/ajaxHooker.d.ts +151 -151
- package/src/types/env.d.ts +7 -7
- package/src/types/global.d.ts +31 -31
- package/dist/types/src/types/Event.d.ts +0 -188
- package/src/types/Event.d.ts +0 -188
package/src/CommonUtil.ts
CHANGED
|
@@ -1,280 +1,280 @@
|
|
|
1
|
-
import { TryCatch } from "./TryCatch";
|
|
2
|
-
|
|
3
|
-
class CommonUtil {
|
|
4
|
-
/**
|
|
5
|
-
* JSON数据从源端替换到目标端中,如果目标端存在该数据则替换,不添加,返回结果为目标端替换完毕的结果
|
|
6
|
-
* @param target 目标数据
|
|
7
|
-
* @param source 源数据
|
|
8
|
-
* @param isAdd 是否可以追加键,默认false
|
|
9
|
-
* @example
|
|
10
|
-
* Utils.assign({"1":1,"2":{"3":3}}, {"2":{"3":4}});
|
|
11
|
-
* >
|
|
12
|
-
* {
|
|
13
|
-
"1": 1,
|
|
14
|
-
"2": {
|
|
15
|
-
"3": 4
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
*/
|
|
19
|
-
assign<T1, T2 extends object, T3 extends boolean>(target: T1, source: T2, isAdd?: T3): T3 extends true ? T1 & T2 : T1;
|
|
20
|
-
assign(target = {}, source = {}, isAdd = false) {
|
|
21
|
-
const UtilsContext = this;
|
|
22
|
-
if (Array.isArray(source)) {
|
|
23
|
-
const canTraverse = source.filter((item) => {
|
|
24
|
-
return typeof item === "object";
|
|
25
|
-
});
|
|
26
|
-
if (!canTraverse.length) {
|
|
27
|
-
return source;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
if (source == null) {
|
|
31
|
-
return target;
|
|
32
|
-
}
|
|
33
|
-
if (target == null) {
|
|
34
|
-
target = {};
|
|
35
|
-
}
|
|
36
|
-
if (isAdd) {
|
|
37
|
-
for (const sourceKeyName in source) {
|
|
38
|
-
const targetKeyName = sourceKeyName;
|
|
39
|
-
const targetValue = Reflect.get(target, targetKeyName);
|
|
40
|
-
const sourceValue = Reflect.get(source, sourceKeyName);
|
|
41
|
-
if (
|
|
42
|
-
typeof sourceValue === "object" &&
|
|
43
|
-
sourceValue != null &&
|
|
44
|
-
sourceKeyName in target &&
|
|
45
|
-
!UtilsContext.isDOM(sourceValue)
|
|
46
|
-
) {
|
|
47
|
-
/* 源端的值是object类型,且不是元素节点 */
|
|
48
|
-
Reflect.set(target, sourceKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
|
|
49
|
-
continue;
|
|
50
|
-
}
|
|
51
|
-
Reflect.set(target, sourceKeyName, sourceValue);
|
|
52
|
-
}
|
|
53
|
-
} else {
|
|
54
|
-
for (const targetKeyName in target) {
|
|
55
|
-
if (targetKeyName in source) {
|
|
56
|
-
const targetValue = Reflect.get(target, targetKeyName);
|
|
57
|
-
const sourceValue = Reflect.get(source, targetKeyName);
|
|
58
|
-
if (
|
|
59
|
-
typeof sourceValue === "object" &&
|
|
60
|
-
sourceValue != null &&
|
|
61
|
-
!UtilsContext.isDOM(sourceValue) &&
|
|
62
|
-
Object.keys(sourceValue).length
|
|
63
|
-
) {
|
|
64
|
-
/* 源端的值是object类型,且不是元素节点 */
|
|
65
|
-
Reflect.set(target, targetKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
/* 直接赋值 */
|
|
69
|
-
Reflect.set(target, targetKeyName, sourceValue);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return target;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* 判断对象或数据是否为空
|
|
78
|
-
* + `String`判空的值,如 ""、"null"、"undefined"、" "
|
|
79
|
-
* + `Number`判空的值,如 0
|
|
80
|
-
* + `Object`判空的值,如 {}、null、undefined
|
|
81
|
-
* + `Array`(存在属性Symbol.iterator)判空的值,如 []
|
|
82
|
-
* + `Boolean`判空的值,如false
|
|
83
|
-
* + `Function`判空的值,如()=>{}、(xxx="")=>{}、function(){}、function(xxx=""){}
|
|
84
|
-
* @returns
|
|
85
|
-
* + true 为空
|
|
86
|
-
* + false 不为空
|
|
87
|
-
* @example
|
|
88
|
-
Utils.isNull({});
|
|
89
|
-
> true
|
|
90
|
-
* @example
|
|
91
|
-
Utils.isNull([]);
|
|
92
|
-
> true
|
|
93
|
-
* @example
|
|
94
|
-
Utils.isNull(" ");
|
|
95
|
-
> true
|
|
96
|
-
* @example
|
|
97
|
-
Utils.isNull(function(){});
|
|
98
|
-
> true
|
|
99
|
-
* @example
|
|
100
|
-
Utils.isNull(()=>{}));
|
|
101
|
-
> true
|
|
102
|
-
* @example
|
|
103
|
-
Utils.isNull("undefined");
|
|
104
|
-
> true
|
|
105
|
-
* @example
|
|
106
|
-
Utils.isNull("null");
|
|
107
|
-
> true
|
|
108
|
-
* @example
|
|
109
|
-
Utils.isNull(" ", false);
|
|
110
|
-
> true
|
|
111
|
-
* @example
|
|
112
|
-
Utils.isNull([1],[]);
|
|
113
|
-
> false
|
|
114
|
-
* @example
|
|
115
|
-
Utils.isNull([],[1]);
|
|
116
|
-
> false
|
|
117
|
-
* @example
|
|
118
|
-
Utils.isNull(false,[123]);
|
|
119
|
-
> false
|
|
120
|
-
**/
|
|
121
|
-
isNull<T>(value: T | undefined | null): value is null | undefined;
|
|
122
|
-
isNull(...args: any[]): boolean;
|
|
123
|
-
isNull(...args: any[]): boolean {
|
|
124
|
-
let result = true;
|
|
125
|
-
const checkList = [...args];
|
|
126
|
-
for (const objItem of checkList) {
|
|
127
|
-
let itemResult = false;
|
|
128
|
-
if (objItem === null || objItem === undefined) {
|
|
129
|
-
itemResult = true;
|
|
130
|
-
} else {
|
|
131
|
-
switch (typeof objItem) {
|
|
132
|
-
case "object":
|
|
133
|
-
if (typeof objItem[Symbol.iterator] === "function") {
|
|
134
|
-
/* 可迭代 */
|
|
135
|
-
itemResult = objItem.length === 0;
|
|
136
|
-
} else {
|
|
137
|
-
itemResult = Object.keys(objItem).length === 0;
|
|
138
|
-
}
|
|
139
|
-
break;
|
|
140
|
-
case "number":
|
|
141
|
-
itemResult = objItem === 0;
|
|
142
|
-
break;
|
|
143
|
-
case "string":
|
|
144
|
-
itemResult = objItem.trim() === "" || objItem === "null" || objItem === "undefined";
|
|
145
|
-
break;
|
|
146
|
-
case "boolean":
|
|
147
|
-
itemResult = !objItem;
|
|
148
|
-
break;
|
|
149
|
-
case "function": {
|
|
150
|
-
const funcStr = objItem.toString().replace(/\s/g, "");
|
|
151
|
-
/* 排除()=>{}、(xxx="")=>{}、function(){}、function(xxx=""){} */
|
|
152
|
-
itemResult = Boolean(funcStr.match(/^\(.*?\)=>\{\}$|^function.*?\(.*?\)\{\}$/));
|
|
153
|
-
break;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
result = result && itemResult;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return result;
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* 判断对象是否是元素
|
|
164
|
-
* @param target
|
|
165
|
-
* @returns
|
|
166
|
-
* + true 是元素
|
|
167
|
-
* + false 不是元素
|
|
168
|
-
* @example
|
|
169
|
-
* Utils.isDOM(document.querySelector("a"))
|
|
170
|
-
* > true
|
|
171
|
-
*/
|
|
172
|
-
isDOM(target: any): boolean {
|
|
173
|
-
return target instanceof Node;
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* 判断对象是否不为空
|
|
177
|
-
* @returns {boolean}
|
|
178
|
-
* + true 不为空
|
|
179
|
-
* + false 为空
|
|
180
|
-
* @example
|
|
181
|
-
* Utils.isNotNull("123");
|
|
182
|
-
* > true
|
|
183
|
-
*/
|
|
184
|
-
isNotNull<T>(value: T | null | undefined): value is T;
|
|
185
|
-
isNotNull(...args: any[]): boolean;
|
|
186
|
-
isNotNull(...args: any[]): boolean {
|
|
187
|
-
const UtilsContext = this;
|
|
188
|
-
return !UtilsContext.isNull.apply(this, args);
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* 深拷贝
|
|
192
|
-
* @param obj 对象
|
|
193
|
-
*/
|
|
194
|
-
deepClone<T extends object | undefined | null>(obj?: T): T;
|
|
195
|
-
deepClone<T extends object | undefined | null>(obj?: T) {
|
|
196
|
-
const UtilsContext = this;
|
|
197
|
-
if (obj === void 0) return void 0;
|
|
198
|
-
if (obj === null) return null;
|
|
199
|
-
const clone = obj instanceof Array ? [] : {};
|
|
200
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
201
|
-
if (typeof value === "object") {
|
|
202
|
-
Reflect.set(clone, key, UtilsContext.deepClone(value));
|
|
203
|
-
} else {
|
|
204
|
-
Reflect.set(clone, key, value);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
return clone;
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* 覆盖对象中的函数this指向
|
|
211
|
-
* @param target 需要覆盖的对象
|
|
212
|
-
* @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
|
|
213
|
-
*/
|
|
214
|
-
coverObjectFunctionThis(target: any, objectThis?: any) {
|
|
215
|
-
if (typeof target !== "object" || target === null) {
|
|
216
|
-
throw new Error("target must be object");
|
|
217
|
-
}
|
|
218
|
-
objectThis = objectThis || target;
|
|
219
|
-
Object.keys(target).forEach((key) => {
|
|
220
|
-
if (typeof target[key] === "function") {
|
|
221
|
-
target[key] = target[key].bind(objectThis);
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* 字符串转Object对象,类似'{"test":""}' => {"test":""}
|
|
227
|
-
* @param data
|
|
228
|
-
* @param errorCallBack (可选)错误回调
|
|
229
|
-
* @example
|
|
230
|
-
* Utils.toJSON("{123:123}")
|
|
231
|
-
* > {123:123}
|
|
232
|
-
*/
|
|
233
|
-
toJSON<T = any>(data: string | null, errorCallBack?: (error: Error) => void): T;
|
|
234
|
-
toJSON<T = any>(data: string | null, errorCallBack?: (error: Error) => void): T {
|
|
235
|
-
let result: any = {};
|
|
236
|
-
if (typeof data === "object") {
|
|
237
|
-
return data as T;
|
|
238
|
-
}
|
|
239
|
-
TryCatch()
|
|
240
|
-
.config({ log: false })
|
|
241
|
-
.error(() => {
|
|
242
|
-
TryCatch()
|
|
243
|
-
.error(() => {
|
|
244
|
-
try {
|
|
245
|
-
result = new Function(`return ${data}`)();
|
|
246
|
-
} catch (error2: any) {
|
|
247
|
-
if (typeof errorCallBack === "function") {
|
|
248
|
-
errorCallBack(error2);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
})
|
|
252
|
-
.run(() => {
|
|
253
|
-
if (
|
|
254
|
-
data &&
|
|
255
|
-
/^[\],:{}\s]*$/.test(
|
|
256
|
-
data
|
|
257
|
-
.replace(/\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
|
|
258
|
-
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?/g, "]")
|
|
259
|
-
.replace(/(?:^|:|,)(?:\s*\[)+/g, "")
|
|
260
|
-
)
|
|
261
|
-
) {
|
|
262
|
-
result = new Function(`return ${data}`)();
|
|
263
|
-
} else {
|
|
264
|
-
if (typeof errorCallBack === "function") {
|
|
265
|
-
errorCallBack(new Error("target is not JSON object"));
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
});
|
|
269
|
-
})
|
|
270
|
-
.run(() => {
|
|
271
|
-
data = (data as string).trim();
|
|
272
|
-
result = JSON.parse(data);
|
|
273
|
-
});
|
|
274
|
-
return result as T;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
const commonUtil = new CommonUtil();
|
|
279
|
-
|
|
280
|
-
export { commonUtil as CommonUtil };
|
|
1
|
+
import { TryCatch } from "./TryCatch";
|
|
2
|
+
|
|
3
|
+
class CommonUtil {
|
|
4
|
+
/**
|
|
5
|
+
* JSON数据从源端替换到目标端中,如果目标端存在该数据则替换,不添加,返回结果为目标端替换完毕的结果
|
|
6
|
+
* @param target 目标数据
|
|
7
|
+
* @param source 源数据
|
|
8
|
+
* @param isAdd 是否可以追加键,默认false
|
|
9
|
+
* @example
|
|
10
|
+
* Utils.assign({"1":1,"2":{"3":3}}, {"2":{"3":4}});
|
|
11
|
+
* >
|
|
12
|
+
* {
|
|
13
|
+
"1": 1,
|
|
14
|
+
"2": {
|
|
15
|
+
"3": 4
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
*/
|
|
19
|
+
assign<T1, T2 extends object, T3 extends boolean>(target: T1, source: T2, isAdd?: T3): T3 extends true ? T1 & T2 : T1;
|
|
20
|
+
assign(target = {}, source = {}, isAdd = false) {
|
|
21
|
+
const UtilsContext = this;
|
|
22
|
+
if (Array.isArray(source)) {
|
|
23
|
+
const canTraverse = source.filter((item) => {
|
|
24
|
+
return typeof item === "object";
|
|
25
|
+
});
|
|
26
|
+
if (!canTraverse.length) {
|
|
27
|
+
return source;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (source == null) {
|
|
31
|
+
return target;
|
|
32
|
+
}
|
|
33
|
+
if (target == null) {
|
|
34
|
+
target = {};
|
|
35
|
+
}
|
|
36
|
+
if (isAdd) {
|
|
37
|
+
for (const sourceKeyName in source) {
|
|
38
|
+
const targetKeyName = sourceKeyName;
|
|
39
|
+
const targetValue = Reflect.get(target, targetKeyName);
|
|
40
|
+
const sourceValue = Reflect.get(source, sourceKeyName);
|
|
41
|
+
if (
|
|
42
|
+
typeof sourceValue === "object" &&
|
|
43
|
+
sourceValue != null &&
|
|
44
|
+
sourceKeyName in target &&
|
|
45
|
+
!UtilsContext.isDOM(sourceValue)
|
|
46
|
+
) {
|
|
47
|
+
/* 源端的值是object类型,且不是元素节点 */
|
|
48
|
+
Reflect.set(target, sourceKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
Reflect.set(target, sourceKeyName, sourceValue);
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
for (const targetKeyName in target) {
|
|
55
|
+
if (targetKeyName in source) {
|
|
56
|
+
const targetValue = Reflect.get(target, targetKeyName);
|
|
57
|
+
const sourceValue = Reflect.get(source, targetKeyName);
|
|
58
|
+
if (
|
|
59
|
+
typeof sourceValue === "object" &&
|
|
60
|
+
sourceValue != null &&
|
|
61
|
+
!UtilsContext.isDOM(sourceValue) &&
|
|
62
|
+
Object.keys(sourceValue).length
|
|
63
|
+
) {
|
|
64
|
+
/* 源端的值是object类型,且不是元素节点 */
|
|
65
|
+
Reflect.set(target, targetKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
/* 直接赋值 */
|
|
69
|
+
Reflect.set(target, targetKeyName, sourceValue);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return target;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* 判断对象或数据是否为空
|
|
78
|
+
* + `String`判空的值,如 ""、"null"、"undefined"、" "
|
|
79
|
+
* + `Number`判空的值,如 0
|
|
80
|
+
* + `Object`判空的值,如 {}、null、undefined
|
|
81
|
+
* + `Array`(存在属性Symbol.iterator)判空的值,如 []
|
|
82
|
+
* + `Boolean`判空的值,如false
|
|
83
|
+
* + `Function`判空的值,如()=>{}、(xxx="")=>{}、function(){}、function(xxx=""){}
|
|
84
|
+
* @returns
|
|
85
|
+
* + true 为空
|
|
86
|
+
* + false 不为空
|
|
87
|
+
* @example
|
|
88
|
+
Utils.isNull({});
|
|
89
|
+
> true
|
|
90
|
+
* @example
|
|
91
|
+
Utils.isNull([]);
|
|
92
|
+
> true
|
|
93
|
+
* @example
|
|
94
|
+
Utils.isNull(" ");
|
|
95
|
+
> true
|
|
96
|
+
* @example
|
|
97
|
+
Utils.isNull(function(){});
|
|
98
|
+
> true
|
|
99
|
+
* @example
|
|
100
|
+
Utils.isNull(()=>{}));
|
|
101
|
+
> true
|
|
102
|
+
* @example
|
|
103
|
+
Utils.isNull("undefined");
|
|
104
|
+
> true
|
|
105
|
+
* @example
|
|
106
|
+
Utils.isNull("null");
|
|
107
|
+
> true
|
|
108
|
+
* @example
|
|
109
|
+
Utils.isNull(" ", false);
|
|
110
|
+
> true
|
|
111
|
+
* @example
|
|
112
|
+
Utils.isNull([1],[]);
|
|
113
|
+
> false
|
|
114
|
+
* @example
|
|
115
|
+
Utils.isNull([],[1]);
|
|
116
|
+
> false
|
|
117
|
+
* @example
|
|
118
|
+
Utils.isNull(false,[123]);
|
|
119
|
+
> false
|
|
120
|
+
**/
|
|
121
|
+
isNull<T>(value: T | undefined | null): value is null | undefined;
|
|
122
|
+
isNull(...args: any[]): boolean;
|
|
123
|
+
isNull(...args: any[]): boolean {
|
|
124
|
+
let result = true;
|
|
125
|
+
const checkList = [...args];
|
|
126
|
+
for (const objItem of checkList) {
|
|
127
|
+
let itemResult = false;
|
|
128
|
+
if (objItem === null || objItem === undefined) {
|
|
129
|
+
itemResult = true;
|
|
130
|
+
} else {
|
|
131
|
+
switch (typeof objItem) {
|
|
132
|
+
case "object":
|
|
133
|
+
if (typeof objItem[Symbol.iterator] === "function") {
|
|
134
|
+
/* 可迭代 */
|
|
135
|
+
itemResult = objItem.length === 0;
|
|
136
|
+
} else {
|
|
137
|
+
itemResult = Object.keys(objItem).length === 0;
|
|
138
|
+
}
|
|
139
|
+
break;
|
|
140
|
+
case "number":
|
|
141
|
+
itemResult = objItem === 0;
|
|
142
|
+
break;
|
|
143
|
+
case "string":
|
|
144
|
+
itemResult = objItem.trim() === "" || objItem === "null" || objItem === "undefined";
|
|
145
|
+
break;
|
|
146
|
+
case "boolean":
|
|
147
|
+
itemResult = !objItem;
|
|
148
|
+
break;
|
|
149
|
+
case "function": {
|
|
150
|
+
const funcStr = objItem.toString().replace(/\s/g, "");
|
|
151
|
+
/* 排除()=>{}、(xxx="")=>{}、function(){}、function(xxx=""){} */
|
|
152
|
+
itemResult = Boolean(funcStr.match(/^\(.*?\)=>\{\}$|^function.*?\(.*?\)\{\}$/));
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
result = result && itemResult;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return result;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* 判断对象是否是元素
|
|
164
|
+
* @param target
|
|
165
|
+
* @returns
|
|
166
|
+
* + true 是元素
|
|
167
|
+
* + false 不是元素
|
|
168
|
+
* @example
|
|
169
|
+
* Utils.isDOM(document.querySelector("a"))
|
|
170
|
+
* > true
|
|
171
|
+
*/
|
|
172
|
+
isDOM(target: any): boolean {
|
|
173
|
+
return target instanceof Node;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* 判断对象是否不为空
|
|
177
|
+
* @returns {boolean}
|
|
178
|
+
* + true 不为空
|
|
179
|
+
* + false 为空
|
|
180
|
+
* @example
|
|
181
|
+
* Utils.isNotNull("123");
|
|
182
|
+
* > true
|
|
183
|
+
*/
|
|
184
|
+
isNotNull<T>(value: T | null | undefined): value is T;
|
|
185
|
+
isNotNull(...args: any[]): boolean;
|
|
186
|
+
isNotNull(...args: any[]): boolean {
|
|
187
|
+
const UtilsContext = this;
|
|
188
|
+
return !UtilsContext.isNull.apply(this, args);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* 深拷贝
|
|
192
|
+
* @param obj 对象
|
|
193
|
+
*/
|
|
194
|
+
deepClone<T extends object | undefined | null>(obj?: T): T;
|
|
195
|
+
deepClone<T extends object | undefined | null>(obj?: T) {
|
|
196
|
+
const UtilsContext = this;
|
|
197
|
+
if (obj === void 0) return void 0;
|
|
198
|
+
if (obj === null) return null;
|
|
199
|
+
const clone = obj instanceof Array ? [] : {};
|
|
200
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
201
|
+
if (typeof value === "object") {
|
|
202
|
+
Reflect.set(clone, key, UtilsContext.deepClone(value));
|
|
203
|
+
} else {
|
|
204
|
+
Reflect.set(clone, key, value);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return clone;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* 覆盖对象中的函数this指向
|
|
211
|
+
* @param target 需要覆盖的对象
|
|
212
|
+
* @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
|
|
213
|
+
*/
|
|
214
|
+
coverObjectFunctionThis(target: any, objectThis?: any) {
|
|
215
|
+
if (typeof target !== "object" || target === null) {
|
|
216
|
+
throw new Error("target must be object");
|
|
217
|
+
}
|
|
218
|
+
objectThis = objectThis || target;
|
|
219
|
+
Object.keys(target).forEach((key) => {
|
|
220
|
+
if (typeof target[key] === "function") {
|
|
221
|
+
target[key] = target[key].bind(objectThis);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* 字符串转Object对象,类似'{"test":""}' => {"test":""}
|
|
227
|
+
* @param data
|
|
228
|
+
* @param errorCallBack (可选)错误回调
|
|
229
|
+
* @example
|
|
230
|
+
* Utils.toJSON("{123:123}")
|
|
231
|
+
* > {123:123}
|
|
232
|
+
*/
|
|
233
|
+
toJSON<T = any>(data: string | null, errorCallBack?: (error: Error) => void): T;
|
|
234
|
+
toJSON<T = any>(data: string | null, errorCallBack?: (error: Error) => void): T {
|
|
235
|
+
let result: any = {};
|
|
236
|
+
if (typeof data === "object") {
|
|
237
|
+
return data as T;
|
|
238
|
+
}
|
|
239
|
+
TryCatch()
|
|
240
|
+
.config({ log: false })
|
|
241
|
+
.error(() => {
|
|
242
|
+
TryCatch()
|
|
243
|
+
.error(() => {
|
|
244
|
+
try {
|
|
245
|
+
result = new Function(`return ${data}`)();
|
|
246
|
+
} catch (error2: any) {
|
|
247
|
+
if (typeof errorCallBack === "function") {
|
|
248
|
+
errorCallBack(error2);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
})
|
|
252
|
+
.run(() => {
|
|
253
|
+
if (
|
|
254
|
+
data &&
|
|
255
|
+
/^[\],:{}\s]*$/.test(
|
|
256
|
+
data
|
|
257
|
+
.replace(/\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
|
|
258
|
+
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?/g, "]")
|
|
259
|
+
.replace(/(?:^|:|,)(?:\s*\[)+/g, "")
|
|
260
|
+
)
|
|
261
|
+
) {
|
|
262
|
+
result = new Function(`return ${data}`)();
|
|
263
|
+
} else {
|
|
264
|
+
if (typeof errorCallBack === "function") {
|
|
265
|
+
errorCallBack(new Error("target is not JSON object"));
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
})
|
|
270
|
+
.run(() => {
|
|
271
|
+
data = (data as string).trim();
|
|
272
|
+
result = JSON.parse(data);
|
|
273
|
+
});
|
|
274
|
+
return result as T;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const commonUtil = new CommonUtil();
|
|
279
|
+
|
|
280
|
+
export { commonUtil as CommonUtil };
|