@whitesev/utils 2.8.0 → 2.8.2

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 (71) hide show
  1. package/README.md +176 -176
  2. package/dist/index.amd.js +896 -875
  3. package/dist/index.amd.js.map +1 -1
  4. package/dist/index.cjs.js +896 -875
  5. package/dist/index.cjs.js.map +1 -1
  6. package/dist/index.esm.js +896 -875
  7. package/dist/index.esm.js.map +1 -1
  8. package/dist/index.iife.js +896 -875
  9. package/dist/index.iife.js.map +1 -1
  10. package/dist/index.system.js +896 -875
  11. package/dist/index.system.js.map +1 -1
  12. package/dist/index.umd.js +896 -875
  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/index.ts +3 -0
  38. package/package.json +19 -8
  39. package/src/ColorConversion.ts +105 -106
  40. package/src/CommonUtil.ts +280 -279
  41. package/src/DOMUtils.ts +251 -272
  42. package/src/Dictionary.ts +153 -154
  43. package/src/GBKEncoder.ts +108 -112
  44. package/src/Hooks.ts +73 -81
  45. package/src/Httpx.ts +1457 -1466
  46. package/src/LockFunction.ts +62 -62
  47. package/src/Log.ts +258 -259
  48. package/src/ModuleRaid.js +1 -0
  49. package/src/Progress.ts +108 -114
  50. package/src/TryCatch.ts +86 -86
  51. package/src/Utils.ts +4773 -4825
  52. package/src/UtilsCommon.ts +14 -14
  53. package/src/UtilsGMCookie.ts +254 -261
  54. package/src/UtilsGMMenu.ts +445 -454
  55. package/src/Vue.ts +233 -229
  56. package/src/WindowApi.ts +59 -59
  57. package/src/ajaxHooker/ajaxHooker.js +1 -0
  58. package/src/indexedDB.ts +497 -502
  59. package/src/types/Event.d.ts +188 -188
  60. package/src/types/Httpx.d.ts +1344 -1343
  61. package/src/types/Log.d.ts +19 -19
  62. package/src/types/Progress.d.ts +20 -20
  63. package/src/types/React.d.ts +119 -119
  64. package/src/types/TryCatch.d.ts +9 -9
  65. package/src/types/UtilsGMCookie.d.ts +93 -93
  66. package/src/types/UtilsGMMenu.d.ts +77 -77
  67. package/src/types/Vue2.d.ts +166 -166
  68. package/src/types/WindowApi.d.ts +14 -14
  69. package/src/types/ajaxHooker.d.ts +151 -151
  70. package/src/types/env.d.ts +7 -2
  71. package/src/types/global.d.ts +31 -31
package/src/Progress.ts CHANGED
@@ -1,114 +1,108 @@
1
- import { CommonUtil } from "./CommonUtil";
2
- import type { ProgressParamConfig } from "./types/Progress";
3
-
4
- export class Progress {
5
- #config: ProgressParamConfig = {
6
- /**
7
- * canvas元素节点
8
- */
9
- canvasNode: null as any,
10
- /**
11
- * 绘制角度
12
- */
13
- deg: 95,
14
- /**
15
- * 进度
16
- */
17
- progress: 0,
18
- /**
19
- * 绘制的线宽度
20
- */
21
- lineWidth: 10,
22
- /**
23
- * 绘制的背景颜色
24
- */
25
- lineBgColor: "#1e637c",
26
- /**
27
- * 绘制的线的颜色
28
- */
29
- lineColor: "#25deff",
30
- /**
31
- * 绘制的字体颜色
32
- */
33
- textColor: "#000000",
34
- /**
35
- * 绘制的字体大小(px)
36
- */
37
- fontSize: 22,
38
- /**
39
- * 绘制的圆的半径
40
- */
41
- circleRadius: 50,
42
- };
43
- #ctx: CanvasRenderingContext2D = null as any;
44
- #width: number = null as any;
45
- #height: number = null as any;
46
- /**
47
- *
48
- * @param paramConfig 配置信息
49
- */
50
- constructor(paramConfig: ProgressParamConfig) {
51
- this.#config = CommonUtil.assign(this.#config, paramConfig);
52
- if (!(this.#config.canvasNode instanceof HTMLCanvasElement)) {
53
- throw new Error("Utils.Progress 参数 canvasNode 必须是 HTMLCanvasElement");
54
- }
55
- this.init();
56
- }
57
- /**
58
- * 初始化
59
- */
60
- private init() {
61
- /* 获取画笔 */
62
- let ctx = this.#config.canvasNode.getContext("2d");
63
- if (ctx == null) {
64
- throw new Error("Utils.Progress 获取画笔失败");
65
- }
66
- this.#ctx = ctx;
67
- /* 元素宽度 */
68
- this.#width = this.#config.canvasNode.width;
69
- /* 元素高度 */
70
- this.#height = this.#config.canvasNode.height;
71
- /* 清除锯齿 */
72
- if (window.devicePixelRatio) {
73
- this.#config.canvasNode.style.width = this.#width + "px";
74
- this.#config.canvasNode.style.height = this.#height + "px";
75
- this.#config.canvasNode.height = this.#height * window.devicePixelRatio;
76
- this.#config.canvasNode.width = this.#width * window.devicePixelRatio;
77
- this.#ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
78
- }
79
- /* 设置线宽 */
80
- this.#ctx.lineWidth = this.#config.lineWidth;
81
- }
82
- /**
83
- * 绘制
84
- */
85
- draw() {
86
- let degActive = (this.#config.progress * 360) / 100;
87
- /* 清除画布 */
88
- this.#ctx.clearRect(0, 0, this.#width, this.#height);
89
- /* 开始绘制底圆 */
90
- this.#ctx.beginPath();
91
- this.#ctx.arc(this.#width / 2, this.#height / 2, this.#config.circleRadius, 1, 8);
92
- this.#ctx.strokeStyle = this.#config.lineBgColor;
93
- this.#ctx.stroke();
94
- /* 开始绘制动态圆 */
95
- this.#ctx.beginPath();
96
- this.#ctx.arc(
97
- this.#width / 2,
98
- this.#height / 2,
99
- this.#config.circleRadius,
100
- -Math.PI / 2,
101
- (degActive * Math.PI) / 180 - Math.PI / 2
102
- );
103
- this.#ctx.strokeStyle = this.#config.lineColor;
104
- this.#ctx.stroke();
105
- /* 获取百分比 */
106
- let txt = parseInt(this.#config.progress.toString()) + "%";
107
- this.#ctx.font = this.#config.fontSize + "px SimHei";
108
- /* 获取文本宽度 */
109
- let w = this.#ctx.measureText(txt).width;
110
- let h = this.#config.fontSize / 2;
111
- this.#ctx.fillStyle = this.#config.textColor;
112
- this.#ctx.fillText(txt, this.#width / 2 - w / 2, this.#height / 2 + h / 2);
113
- }
114
- }
1
+ import { CommonUtil } from "./CommonUtil";
2
+ import type { ProgressParamConfig } from "./types/Progress";
3
+
4
+ export class Progress {
5
+ #config: ProgressParamConfig = {
6
+ /**
7
+ * canvas元素节点
8
+ */
9
+ canvasNode: null as any,
10
+ /**
11
+ * 绘制角度
12
+ */
13
+ deg: 95,
14
+ /**
15
+ * 进度
16
+ */
17
+ progress: 0,
18
+ /**
19
+ * 绘制的线宽度
20
+ */
21
+ lineWidth: 10,
22
+ /**
23
+ * 绘制的背景颜色
24
+ */
25
+ lineBgColor: "#1e637c",
26
+ /**
27
+ * 绘制的线的颜色
28
+ */
29
+ lineColor: "#25deff",
30
+ /**
31
+ * 绘制的字体颜色
32
+ */
33
+ textColor: "#000000",
34
+ /**
35
+ * 绘制的字体大小(px)
36
+ */
37
+ fontSize: 22,
38
+ /**
39
+ * 绘制的圆的半径
40
+ */
41
+ circleRadius: 50,
42
+ };
43
+ #ctx: CanvasRenderingContext2D;
44
+ #width: number;
45
+ #height: number;
46
+ /**
47
+ *
48
+ * @param paramConfig 配置信息
49
+ */
50
+ constructor(paramConfig: ProgressParamConfig) {
51
+ this.#config = CommonUtil.assign(this.#config, paramConfig);
52
+ if (!(this.#config.canvasNode instanceof HTMLCanvasElement)) {
53
+ throw new Error("Utils.Progress 参数 canvasNode 必须是 HTMLCanvasElement");
54
+ }
55
+ /* 获取画笔 */
56
+ const ctx = this.#config.canvasNode.getContext("2d");
57
+ if (ctx == null) {
58
+ throw new Error("Utils.Progress 获取画笔失败");
59
+ }
60
+ this.#ctx = ctx;
61
+ /* 元素宽度 */
62
+ this.#width = this.#config.canvasNode.width;
63
+ /* 元素高度 */
64
+ this.#height = this.#config.canvasNode.height;
65
+ /* 清除锯齿 */
66
+ if (window.devicePixelRatio) {
67
+ this.#config.canvasNode.style.width = `${this.#width}px`;
68
+ this.#config.canvasNode.style.height = `${this.#height}px`;
69
+ this.#config.canvasNode.height = this.#height * window.devicePixelRatio;
70
+ this.#config.canvasNode.width = this.#width * window.devicePixelRatio;
71
+ this.#ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
72
+ }
73
+ /* 设置线宽 */
74
+ this.#ctx.lineWidth = this.#config.lineWidth;
75
+ }
76
+ /**
77
+ * 绘制
78
+ */
79
+ draw() {
80
+ const degActive = (this.#config.progress * 360) / 100;
81
+ /* 清除画布 */
82
+ this.#ctx.clearRect(0, 0, this.#width, this.#height);
83
+ /* 开始绘制底圆 */
84
+ this.#ctx.beginPath();
85
+ this.#ctx.arc(this.#width / 2, this.#height / 2, this.#config.circleRadius, 1, 8);
86
+ this.#ctx.strokeStyle = this.#config.lineBgColor;
87
+ this.#ctx.stroke();
88
+ /* 开始绘制动态圆 */
89
+ this.#ctx.beginPath();
90
+ this.#ctx.arc(
91
+ this.#width / 2,
92
+ this.#height / 2,
93
+ this.#config.circleRadius,
94
+ -Math.PI / 2,
95
+ (degActive * Math.PI) / 180 - Math.PI / 2
96
+ );
97
+ this.#ctx.strokeStyle = this.#config.lineColor;
98
+ this.#ctx.stroke();
99
+ /* 获取百分比 */
100
+ const txt = `${parseInt(this.#config.progress.toString())}%`;
101
+ this.#ctx.font = `${this.#config.fontSize}px SimHei`;
102
+ /* 获取文本宽度 */
103
+ const w = this.#ctx.measureText(txt).width;
104
+ const h = this.#config.fontSize / 2;
105
+ this.#ctx.fillStyle = this.#config.textColor;
106
+ this.#ctx.fillText(txt, this.#width / 2 - w / 2, this.#height / 2 + h / 2);
107
+ }
108
+ }
package/src/TryCatch.ts CHANGED
@@ -1,86 +1,86 @@
1
- import type { UtilsTryCatchConfig, UtilsTryCatchType } from "./types/TryCatch";
2
-
3
- export const TryCatch = function (...args: any) {
4
- /* 定义变量和函数 */
5
- let callbackFunction = null;
6
- let context = null;
7
- let handleError = (error: Error) => {};
8
- let defaultDetails = {
9
- log: true,
10
- };
11
-
12
- const TryCatchCore = {
13
- /**
14
- *
15
- * @param paramDetails 配置
16
- * @returns
17
- */
18
- config(paramDetails: UtilsTryCatchConfig) {
19
- defaultDetails = Object.assign(defaultDetails, paramDetails);
20
- return TryCatchCore;
21
- },
22
- /**
23
- * 处理错误
24
- * @param handler
25
- */
26
- error(handler: ((...args: any[]) => any) | string | Function) {
27
- handleError = handler as (error: Error) => void;
28
- return TryCatchCore;
29
- },
30
- /**
31
- * 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
32
- * @param callback 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
33
- * @param __context__ 待执行函数的作用域,用于apply指定
34
- * @returns 如果函数有返回值,则返回该返回值;否则返回 tryCatchObj 函数以支持链式调用。
35
- * @throws {Error} 如果传入参数不符合要求,则会抛出相应类型的错误。
36
- */
37
- run<A extends any[], R>(
38
- callback: ((...args: A) => R) | string | Function,
39
- __context__?: any
40
- ): UtilsTryCatchType {
41
- callbackFunction = callback;
42
- context = __context__ || this;
43
- let result = executeTryCatch(callbackFunction, handleError, context);
44
- return result !== void 0 ? result : (TryCatchCore as any as UtilsTryCatchType);
45
- },
46
- };
47
-
48
- /**
49
- * 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
50
- * @param callback - 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
51
- * @param handleErrorFunc - 错误处理函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
52
- * @param funcThis - 待执行函数的作用域,用于apply指定
53
- * @returns 如果函数有返回值,则返回该返回值;否则返回 undefined。
54
- */
55
- function executeTryCatch<A extends any[], R>(
56
- callback: string | ((...args: A) => R) | Function,
57
- handleErrorFunc: string | ((...args: any[]) => any),
58
- funcThis?: any
59
- ) {
60
- let result = void 0;
61
- try {
62
- if (typeof callback === "string") {
63
- result = new Function(callback).apply(funcThis, args);
64
- } else {
65
- result = (callback as Function).apply(funcThis, args);
66
- }
67
- } catch (error) {
68
- if (defaultDetails.log) {
69
- callback = callback as Function;
70
- console.log(`%c ${callback?.name ? callback?.name : callback + "出现错误"} `, "color: #f20000");
71
- console.log(`%c 错误原因:${error}`, "color: #f20000");
72
- console.trace(callback);
73
- }
74
- if (handleErrorFunc) {
75
- if (typeof handleErrorFunc === "string") {
76
- result = new Function(handleErrorFunc).apply(funcThis, [...args, error]);
77
- } else {
78
- result = handleErrorFunc.apply(funcThis, [...args, error]);
79
- }
80
- }
81
- }
82
- return result;
83
- }
84
-
85
- return TryCatchCore;
86
- };
1
+ import type { UtilsTryCatchConfig, UtilsTryCatchType } from "./types/TryCatch";
2
+
3
+ export const TryCatch = function (...args: any) {
4
+ /* 定义变量和函数 */
5
+ let callbackFunction = null;
6
+ let context = null;
7
+ let handleError: (error: Error) => void = () => {};
8
+ let defaultDetails = {
9
+ log: true,
10
+ };
11
+
12
+ const TryCatchCore = {
13
+ /**
14
+ *
15
+ * @param paramDetails 配置
16
+ * @returns
17
+ */
18
+ config(paramDetails: UtilsTryCatchConfig) {
19
+ defaultDetails = Object.assign(defaultDetails, paramDetails);
20
+ return TryCatchCore;
21
+ },
22
+ /**
23
+ * 处理错误
24
+ * @param handler
25
+ */
26
+ error(handler: ((...args: any[]) => any) | string | ((...args: any[]) => any)) {
27
+ handleError = handler as (error: Error) => void;
28
+ return TryCatchCore;
29
+ },
30
+ /**
31
+ * 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
32
+ * @param callback 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
33
+ * @param __context__ 待执行函数的作用域,用于apply指定
34
+ * @returns 如果函数有返回值,则返回该返回值;否则返回 tryCatchObj 函数以支持链式调用。
35
+ * @throws {Error} 如果传入参数不符合要求,则会抛出相应类型的错误。
36
+ */
37
+ run<A extends any[], R>(
38
+ callback: ((...args: A) => R) | string | ((...args: any[]) => any),
39
+ __context__?: any
40
+ ): UtilsTryCatchType {
41
+ callbackFunction = callback;
42
+ context = __context__ || this;
43
+ const result = executeTryCatch(callbackFunction, handleError, context);
44
+ return result !== void 0 ? result : (TryCatchCore as any as UtilsTryCatchType);
45
+ },
46
+ };
47
+
48
+ /**
49
+ * 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
50
+ * @param callback - 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
51
+ * @param handleErrorFunc - 错误处理函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
52
+ * @param funcThis - 待执行函数的作用域,用于apply指定
53
+ * @returns 如果函数有返回值,则返回该返回值;否则返回 undefined。
54
+ */
55
+ function executeTryCatch<A extends any[], R>(
56
+ callback: string | ((...args: A) => R) | ((...args: any[]) => any),
57
+ handleErrorFunc: string | ((...args: any[]) => any),
58
+ funcThis?: any
59
+ ) {
60
+ let result = void 0;
61
+ try {
62
+ if (typeof callback === "string") {
63
+ result = new Function(callback).apply(funcThis, args);
64
+ } else {
65
+ result = (callback as (...args: any[]) => any).apply(funcThis, args);
66
+ }
67
+ } catch (error) {
68
+ if (defaultDetails.log) {
69
+ callback = callback as (...args: any[]) => any;
70
+ console.log(`%c ${callback?.name ? callback?.name : `${callback}`} `, "color: #f20000");
71
+ console.log(`%c ${error}`, "color: #f20000");
72
+ console.trace(callback);
73
+ }
74
+ if (handleErrorFunc) {
75
+ if (typeof handleErrorFunc === "string") {
76
+ result = new Function(handleErrorFunc).apply(funcThis, [...args, error]);
77
+ } else {
78
+ result = handleErrorFunc.apply(funcThis, [...args, error]);
79
+ }
80
+ }
81
+ }
82
+ return result;
83
+ }
84
+
85
+ return TryCatchCore;
86
+ };