@whitesev/utils 2.6.7 → 2.6.9

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.
@@ -0,0 +1,109 @@
1
+ declare class CommonUtil {
2
+ /**
3
+ * JSON数据从源端替换到目标端中,如果目标端存在该数据则替换,不添加,返回结果为目标端替换完毕的结果
4
+ * @param target 目标数据
5
+ * @param source 源数据
6
+ * @param isAdd 是否可以追加键,默认false
7
+ * @example
8
+ * Utils.assign({"1":1,"2":{"3":3}}, {"2":{"3":4}});
9
+ * >
10
+ * {
11
+ "1": 1,
12
+ "2": {
13
+ "3": 4
14
+ }
15
+ }
16
+ */
17
+ assign<T1, T2 extends object, T3 extends boolean>(target: T1, source: T2, isAdd?: T3): T3 extends true ? T1 & T2 : T1;
18
+ /**
19
+ * 判断对象或数据是否为空
20
+ * + `String`判空的值,如 ""、"null"、"undefined"、" "
21
+ * + `Number`判空的值,如 0
22
+ * + `Object`判空的值,如 {}、null、undefined
23
+ * + `Array`(存在属性Symbol.iterator)判空的值,如 []
24
+ * + `Boolean`判空的值,如false
25
+ * + `Function`判空的值,如()=>{}、(xxx="")=>{}、function(){}、function(xxx=""){}
26
+ * @returns
27
+ * + true 为空
28
+ * + false 不为空
29
+ * @example
30
+ Utils.isNull({});
31
+ > true
32
+ * @example
33
+ Utils.isNull([]);
34
+ > true
35
+ * @example
36
+ Utils.isNull(" ");
37
+ > true
38
+ * @example
39
+ Utils.isNull(function(){});
40
+ > true
41
+ * @example
42
+ Utils.isNull(()=>{}));
43
+ > true
44
+ * @example
45
+ Utils.isNull("undefined");
46
+ > true
47
+ * @example
48
+ Utils.isNull("null");
49
+ > true
50
+ * @example
51
+ Utils.isNull(" ", false);
52
+ > true
53
+ * @example
54
+ Utils.isNull([1],[]);
55
+ > false
56
+ * @example
57
+ Utils.isNull([],[1]);
58
+ > false
59
+ * @example
60
+ Utils.isNull(false,[123]);
61
+ > false
62
+ **/
63
+ isNull<T>(value: T | undefined | null): value is null | undefined;
64
+ isNull(...args: any[]): boolean;
65
+ /**
66
+ * 判断对象是否是元素
67
+ * @param target
68
+ * @returns
69
+ * + true 是元素
70
+ * + false 不是元素
71
+ * @example
72
+ * Utils.isDOM(document.querySelector("a"))
73
+ * > true
74
+ */
75
+ isDOM(target: any): boolean;
76
+ /**
77
+ * 判断对象是否不为空
78
+ * @returns {boolean}
79
+ * + true 不为空
80
+ * + false 为空
81
+ * @example
82
+ * Utils.isNotNull("123");
83
+ * > true
84
+ */
85
+ isNotNull<T>(value: T | null | undefined): value is T;
86
+ isNotNull(...args: any[]): boolean;
87
+ /**
88
+ * 深拷贝
89
+ * @param obj 对象
90
+ */
91
+ deepClone<T extends object | undefined | null>(obj?: T): T;
92
+ /**
93
+ * 覆盖对象中的函数this指向
94
+ * @param target 需要覆盖的对象
95
+ * @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
96
+ */
97
+ coverObjectFunctionThis(target: any, objectThis?: any): void;
98
+ /**
99
+ * 字符串转Object对象,类似'{"test":""}' => {"test":""}
100
+ * @param data
101
+ * @param errorCallBack (可选)错误回调
102
+ * @example
103
+ * Utils.toJSON("{123:123}")
104
+ * > {123:123}
105
+ */
106
+ toJSON<T = any>(data: string | null, errorCallBack?: (error: Error) => void): T;
107
+ }
108
+ declare let commonUtil: CommonUtil;
109
+ export { commonUtil as CommonUtil };
@@ -0,0 +1,95 @@
1
+ import type { WindowApiOption } from "./types/WindowApi";
2
+ declare class DOMUtils {
3
+ private windowApi;
4
+ constructor(option?: WindowApiOption);
5
+ /**
6
+ * 选择器,可使用以下的额外语法
7
+ *
8
+ * + :contains([text]) 作用: 找到包含指定文本内容的指定元素
9
+ * + :empty 作用:找到既没有文本内容也没有子元素的指定元素
10
+ * + :regexp([text]) 作用: 找到符合正则表达式的内容的指定元素
11
+ * @param selector 选择器
12
+ * @param parent 指定父元素
13
+ * @example
14
+ * DOMUtils.selector("div:contains('测试')")
15
+ * > div.xxx
16
+ * @example
17
+ * DOMUtils.selector("div:empty")
18
+ * > div.xxx
19
+ * @example
20
+ * DOMUtils.selector("div:regexp('^xxxx$')")
21
+ * > div.xxx
22
+ */
23
+ selector<K extends keyof HTMLElementTagNameMap>(selector: K, parent?: Element | Document | DocumentFragment | ShadowRoot): HTMLElementTagNameMap[K] | undefined;
24
+ selector<E extends Element = Element>(selector: string, parent?: Element | Document | DocumentFragment | ShadowRoot): E | undefined;
25
+ /**
26
+ * 选择器,可使用以下的额外语法
27
+ *
28
+ * + :contains([text]) 作用: 找到包含指定文本内容的指定元素
29
+ * + :empty 作用:找到既没有文本内容也没有子元素的指定元素
30
+ * + :regexp([text]) 作用: 找到符合正则表达式的内容的指定元素
31
+ * @param selector 选择器
32
+ * @param parent 指定父元素
33
+ * @example
34
+ * DOMUtils.selectorAll("div:contains('测试')")
35
+ * > [div.xxx]
36
+ * @example
37
+ * DOMUtils.selectorAll("div:empty")
38
+ * > [div.xxx]
39
+ * @example
40
+ * DOMUtils.selectorAll("div:regexp('^xxxx$')")
41
+ * > [div.xxx]
42
+ * @example
43
+ * DOMUtils.selectorAll("div:regexp(/^xxx/ig)")
44
+ * > [div.xxx]
45
+ */
46
+ selectorAll<K extends keyof HTMLElementTagNameMap>(selector: K, parent?: Element | Document | DocumentFragment | ShadowRoot): HTMLElementTagNameMap[K][];
47
+ selectorAll<E extends Element = Element>(selector: string, parent?: Element | Document | DocumentFragment | ShadowRoot): E[];
48
+ /**
49
+ * 匹配元素,可使用以下的额外语法
50
+ *
51
+ * + :contains([text]) 作用: 找到包含指定文本内容的指定元素
52
+ * + :empty 作用:找到既没有文本内容也没有子元素的指定元素
53
+ * + :regexp([text]) 作用: 找到符合正则表达式的内容的指定元素
54
+ * @param $el 元素
55
+ * @param selector 选择器
56
+ * @example
57
+ * DOMUtils.matches("div:contains('测试')")
58
+ * > true
59
+ * @example
60
+ * DOMUtils.matches("div:empty")
61
+ * > true
62
+ * @example
63
+ * DOMUtils.matches("div:regexp('^xxxx$')")
64
+ * > true
65
+ * @example
66
+ * DOMUtils.matches("div:regexp(/^xxx/ig)")
67
+ * > false
68
+ */
69
+ matches($el: HTMLElement | Element | null | undefined, selector: string): boolean;
70
+ /**
71
+ * 根据选择器获取上层元素,可使用以下的额外语法
72
+ *
73
+ * + :contains([text]) 作用: 找到包含指定文本内容的指定元素
74
+ * + :empty 作用:找到既没有文本内容也没有子元素的指定元素
75
+ * + :regexp([text]) 作用: 找到符合正则表达式的内容的指定元素
76
+ * @param $el 元素
77
+ * @param selector 选择器
78
+ * @example
79
+ * DOMUtils.closest("div:contains('测试')")
80
+ * > div.xxx
81
+ * @example
82
+ * DOMUtils.closest("div:empty")
83
+ * > div.xxx
84
+ * @example
85
+ * DOMUtils.closest("div:regexp('^xxxx$')")
86
+ * > div.xxxx
87
+ * @example
88
+ * DOMUtils.closest("div:regexp(/^xxx/ig)")
89
+ * > null
90
+ */
91
+ closest<K extends keyof HTMLElementTagNameMap>($el: HTMLElement | Element, selector: string): HTMLElementTagNameMap[K] | null;
92
+ closest<E extends Element = Element>($el: HTMLElement | Element, selector: string): E | null;
93
+ }
94
+ declare let domUtils: DOMUtils;
95
+ export { domUtils };
@@ -44,7 +44,7 @@ declare class Utils {
44
44
  }
45
45
  }
46
46
  */
47
- assign<T1, T2 extends object, T3 extends boolean>(target: T1, source: T2, isAdd?: T3): T3 extends true ? T1 & T2 : T1;
47
+ assign: <T1, T2 extends object, T3 extends boolean>(target: T1, source: T2, isAdd?: T3 | undefined) => T3 extends true ? T1 & T2 : T1;
48
48
  /**
49
49
  * 异步替换字符串
50
50
  * @param string 需要被替换的目标字符串
@@ -121,7 +121,7 @@ declare class Utils {
121
121
  * 深拷贝
122
122
  * @param obj 对象
123
123
  */
124
- deepClone<T extends object | undefined | null>(obj?: T): T;
124
+ deepClone: <T extends object | undefined | null>(obj?: T | undefined) => T;
125
125
  /**
126
126
  * 防抖函数
127
127
  * @param fn 需要触发的回调
@@ -811,7 +811,7 @@ declare class Utils {
811
811
  * Utils.isDOM(document.querySelector("a"))
812
812
  * > true
813
813
  */
814
- isDOM(target: any): boolean;
814
+ isDOM: (target: any) => boolean;
815
815
  /**
816
816
  * 判断浏览器是否支持全屏
817
817
  */
@@ -853,8 +853,10 @@ declare class Utils {
853
853
  * Utils.isNotNull("123");
854
854
  * > true
855
855
  */
856
- isNotNull<T>(value: T | null | undefined): value is T;
857
- isNotNull(...args: any[]): boolean;
856
+ isNotNull: {
857
+ <T>(value: T | null | undefined): value is T;
858
+ (...args: any[]): boolean;
859
+ };
858
860
  /**
859
861
  * 判断对象或数据是否为空
860
862
  * + `String`判空的值,如 ""、"null"、"undefined"、" "
@@ -900,8 +902,10 @@ declare class Utils {
900
902
  Utils.isNull(false,[123]);
901
903
  > false
902
904
  **/
903
- isNull<T>(value: T | undefined | null): value is null | undefined;
904
- isNull(...args: any[]): boolean;
905
+ isNull: {
906
+ <T>(value: T | undefined | null): value is null | undefined;
907
+ (...args: any[]): boolean;
908
+ };
905
909
  /**
906
910
  * 判断浏览器主题是否是暗黑|深色模式
907
911
  */
@@ -1348,7 +1352,7 @@ declare class Utils {
1348
1352
  * Utils.toJSON("{123:123}")
1349
1353
  * > {123:123}
1350
1354
  */
1351
- toJSON<T = any>(data: string | null, errorCallBack?: (error: Error) => void): T;
1355
+ toJSON: <T = any>(data: string | null, errorCallBack?: (error: Error) => void) => T;
1352
1356
  /**
1353
1357
  * 对象转为UrlSearchParams格式的字符串
1354
1358
  * @param obj 目标对象,可以是对象组成的数组
@@ -1804,7 +1808,7 @@ declare class Utils {
1804
1808
  * @param target 需要覆盖的对象
1805
1809
  * @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
1806
1810
  */
1807
- coverObjectFunctionThis(target: any, objectThis?: any): void;
1811
+ coverObjectFunctionThis: (target: any, objectThis?: any) => void;
1808
1812
  /**
1809
1813
  * 生成uuid
1810
1814
  * @example
@@ -1847,6 +1851,19 @@ declare class Utils {
1847
1851
  * @param timeId setInterval 返回的`id`
1848
1852
  */
1849
1853
  workerClearInterval(timeId: number | undefined): void;
1854
+ /**
1855
+ * 获取剪贴板信息
1856
+ */
1857
+ getClipboardInfo(): Promise<{
1858
+ /**
1859
+ * 错误信息,如果为null,则表示读取成功
1860
+ */
1861
+ error: Error | null;
1862
+ /**
1863
+ * 剪贴板内容
1864
+ */
1865
+ content: string;
1866
+ }>;
1850
1867
  }
1851
1868
  declare let utils: Utils;
1852
1869
  export { utils as Utils };
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@whitesev/utils",
3
- "version": "2.6.7",
3
+ "version": "2.6.9",
4
4
  "description": "一个常用的工具库",
5
+ "$schema": "https://json.schemastore.org/package.json",
5
6
  "main": "dist/index.cjs.js",
6
7
  "module": "dist/index.esm.js",
7
8
  "types": "dist/types/index.d.ts",
@@ -0,0 +1,302 @@
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(
53
+ targetValue,
54
+ sourceValue,
55
+ isAdd
56
+ );
57
+ continue;
58
+ }
59
+ (target as any)[sourceKeyName] = sourceValue;
60
+ }
61
+ } else {
62
+ for (const targetKeyName in target) {
63
+ if (targetKeyName in source) {
64
+ let targetValue = (target as any)[targetKeyName];
65
+ let sourceValue = (source as any)[targetKeyName];
66
+ if (
67
+ typeof sourceValue === "object" &&
68
+ sourceValue != null &&
69
+ !UtilsContext.isDOM(sourceValue) &&
70
+ Object.keys(sourceValue).length
71
+ ) {
72
+ /* 源端的值是object类型,且不是元素节点 */
73
+ (target as any)[targetKeyName] = UtilsContext.assign(
74
+ targetValue,
75
+ sourceValue,
76
+ isAdd
77
+ );
78
+ continue;
79
+ }
80
+ /* 直接赋值 */
81
+ (target as any)[targetKeyName] = sourceValue;
82
+ }
83
+ }
84
+ }
85
+
86
+ return target;
87
+ }
88
+ /**
89
+ * 判断对象或数据是否为空
90
+ * + `String`判空的值,如 ""、"null"、"undefined"、" "
91
+ * + `Number`判空的值,如 0
92
+ * + `Object`判空的值,如 {}、null、undefined
93
+ * + `Array`(存在属性Symbol.iterator)判空的值,如 []
94
+ * + `Boolean`判空的值,如false
95
+ * + `Function`判空的值,如()=>{}、(xxx="")=>{}、function(){}、function(xxx=""){}
96
+ * @returns
97
+ * + true 为空
98
+ * + false 不为空
99
+ * @example
100
+ Utils.isNull({});
101
+ > true
102
+ * @example
103
+ Utils.isNull([]);
104
+ > true
105
+ * @example
106
+ Utils.isNull(" ");
107
+ > true
108
+ * @example
109
+ Utils.isNull(function(){});
110
+ > true
111
+ * @example
112
+ Utils.isNull(()=>{}));
113
+ > true
114
+ * @example
115
+ Utils.isNull("undefined");
116
+ > true
117
+ * @example
118
+ Utils.isNull("null");
119
+ > true
120
+ * @example
121
+ Utils.isNull(" ", false);
122
+ > true
123
+ * @example
124
+ Utils.isNull([1],[]);
125
+ > false
126
+ * @example
127
+ Utils.isNull([],[1]);
128
+ > false
129
+ * @example
130
+ Utils.isNull(false,[123]);
131
+ > false
132
+ **/
133
+ isNull<T>(value: T | undefined | null): value is null | undefined;
134
+ isNull(...args: any[]): boolean;
135
+ isNull(...args: any[]): boolean {
136
+ let result = true;
137
+ let checkList = [...args];
138
+ for (const objItem of checkList) {
139
+ let itemResult = false;
140
+ if (objItem === null || objItem === undefined) {
141
+ itemResult = true;
142
+ } else {
143
+ switch (typeof objItem) {
144
+ case "object":
145
+ if (typeof objItem[Symbol.iterator] === "function") {
146
+ /* 可迭代 */
147
+ itemResult = objItem.length === 0;
148
+ } else {
149
+ itemResult = Object.keys(objItem).length === 0;
150
+ }
151
+ break;
152
+ case "number":
153
+ itemResult = objItem === 0;
154
+ break;
155
+ case "string":
156
+ itemResult =
157
+ objItem.trim() === "" ||
158
+ objItem === "null" ||
159
+ objItem === "undefined";
160
+ break;
161
+ case "boolean":
162
+ itemResult = !objItem;
163
+ break;
164
+ case "function":
165
+ let funcStr = objItem.toString().replace(/\s/g, "");
166
+ /* 排除()=>{}、(xxx="")=>{}、function(){}、function(xxx=""){} */
167
+ itemResult = Boolean(
168
+ funcStr.match(/^\(.*?\)=>\{\}$|^function.*?\(.*?\)\{\}$/)
169
+ );
170
+ break;
171
+ }
172
+ }
173
+ result = result && itemResult;
174
+ }
175
+
176
+ return result;
177
+ }
178
+ /**
179
+ * 判断对象是否是元素
180
+ * @param target
181
+ * @returns
182
+ * + true 是元素
183
+ * + false 不是元素
184
+ * @example
185
+ * Utils.isDOM(document.querySelector("a"))
186
+ * > true
187
+ */
188
+ isDOM(target: any): boolean {
189
+ return target instanceof Node;
190
+ }
191
+ /**
192
+ * 判断对象是否不为空
193
+ * @returns {boolean}
194
+ * + true 不为空
195
+ * + false 为空
196
+ * @example
197
+ * Utils.isNotNull("123");
198
+ * > true
199
+ */
200
+ isNotNull<T>(value: T | null | undefined): value is T;
201
+ isNotNull(...args: any[]): boolean;
202
+ isNotNull(...args: any[]): boolean {
203
+ let UtilsContext = this;
204
+ return !UtilsContext.isNull.apply(this, args);
205
+ }
206
+ /**
207
+ * 深拷贝
208
+ * @param obj 对象
209
+ */
210
+ deepClone<T extends object | undefined | null>(obj?: T): T;
211
+ deepClone<T extends object | undefined | null>(obj?: T) {
212
+ let UtilsContext = this;
213
+ if (obj === void 0) return void 0;
214
+ if (obj === null) return null;
215
+ let clone = obj instanceof Array ? [] : {};
216
+ for (const [key, value] of Object.entries(obj)) {
217
+ (clone as any)[key] =
218
+ typeof value === "object" ? UtilsContext.deepClone(value) : value;
219
+ }
220
+ return clone;
221
+ }
222
+ /**
223
+ * 覆盖对象中的函数this指向
224
+ * @param target 需要覆盖的对象
225
+ * @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
226
+ */
227
+ coverObjectFunctionThis(target: any, objectThis?: any) {
228
+ if (typeof target !== "object" || target === null) {
229
+ throw new Error("target must be object");
230
+ }
231
+ objectThis = objectThis || target;
232
+ Object.keys(target).forEach((key) => {
233
+ if (typeof target[key] === "function") {
234
+ target[key] = target[key].bind(objectThis);
235
+ }
236
+ });
237
+ }
238
+ /**
239
+ * 字符串转Object对象,类似'{"test":""}' => {"test":""}
240
+ * @param data
241
+ * @param errorCallBack (可选)错误回调
242
+ * @example
243
+ * Utils.toJSON("{123:123}")
244
+ * > {123:123}
245
+ */
246
+ toJSON<T = any>(
247
+ data: string | null,
248
+ errorCallBack?: (error: Error) => void
249
+ ): T;
250
+ toJSON<T = any>(
251
+ data: string | null,
252
+ errorCallBack?: (error: Error) => void
253
+ ): T {
254
+ let result: any = {};
255
+ if (typeof data === "object") {
256
+ return data as any;
257
+ }
258
+ TryCatch()
259
+ .config({ log: false })
260
+ .error((error: Error) => {
261
+ TryCatch()
262
+ .error(() => {
263
+ try {
264
+ result = new Function("return " + data)();
265
+ } catch (error2: any) {
266
+ if (typeof errorCallBack === "function") {
267
+ errorCallBack(error2);
268
+ }
269
+ }
270
+ })
271
+ .run(() => {
272
+ if (
273
+ data &&
274
+ /^[\],:{}\s]*$/.test(
275
+ data
276
+ .replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
277
+ .replace(
278
+ /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
279
+ "]"
280
+ )
281
+ .replace(/(?:^|:|,)(?:\s*\[)+/g, "")
282
+ )
283
+ ) {
284
+ result = new Function("return " + data)();
285
+ } else {
286
+ if (typeof errorCallBack === "function") {
287
+ errorCallBack(new Error("target is not a JSON"));
288
+ }
289
+ }
290
+ });
291
+ })
292
+ .run(() => {
293
+ data = (data as string).trim();
294
+ result = JSON.parse(data);
295
+ });
296
+ return result as any;
297
+ }
298
+ }
299
+
300
+ let commonUtil = new CommonUtil();
301
+
302
+ export { commonUtil as CommonUtil };