@whitesev/utils 2.8.0 → 2.8.1

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