@whitesev/utils 2.7.0 → 2.7.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 (48) hide show
  1. package/README.md +19 -19
  2. package/dist/index.amd.js +205 -235
  3. package/dist/index.amd.js.map +1 -1
  4. package/dist/index.cjs.js +205 -235
  5. package/dist/index.cjs.js.map +1 -1
  6. package/dist/index.esm.js +205 -235
  7. package/dist/index.esm.js.map +1 -1
  8. package/dist/index.iife.js +205 -235
  9. package/dist/index.iife.js.map +1 -1
  10. package/dist/index.system.js +205 -235
  11. package/dist/index.system.js.map +1 -1
  12. package/dist/index.umd.js +205 -235
  13. package/dist/index.umd.js.map +1 -1
  14. package/dist/types/src/ColorConversion.d.ts +3 -8
  15. package/dist/types/src/Dictionary.d.ts +21 -14
  16. package/dist/types/src/GBKEncoder.d.ts +1 -2
  17. package/dist/types/src/Hooks.d.ts +1 -2
  18. package/dist/types/src/Httpx.d.ts +45 -46
  19. package/dist/types/src/LockFunction.d.ts +1 -2
  20. package/dist/types/src/Log.d.ts +1 -2
  21. package/dist/types/src/Progress.d.ts +1 -2
  22. package/dist/types/src/Utils.d.ts +1 -1
  23. package/dist/types/src/UtilsGMMenu.d.ts +1 -2
  24. package/dist/types/src/WindowApi.d.ts +1 -2
  25. package/dist/types/src/indexedDB.d.ts +1 -2
  26. package/dist/types/src/types/Httpx.d.ts +73 -67
  27. package/dist/types/src/types/env.d.ts +2 -0
  28. package/dist/types/src/types/global.d.ts +3 -0
  29. package/package.json +1 -1
  30. package/src/ColorConversion.ts +14 -25
  31. package/src/DOMUtils.ts +14 -16
  32. package/src/Dictionary.ts +39 -35
  33. package/src/GBKEncoder.ts +8 -12
  34. package/src/Hooks.ts +1 -3
  35. package/src/Httpx.ts +194 -174
  36. package/src/LockFunction.ts +3 -3
  37. package/src/Log.ts +1 -3
  38. package/src/Progress.ts +1 -3
  39. package/src/TryCatch.ts +4 -4
  40. package/src/Utils.ts +29 -45
  41. package/src/UtilsGMMenu.ts +19 -22
  42. package/src/Vue.ts +4 -7
  43. package/src/WindowApi.ts +2 -5
  44. package/src/ajaxHooker/ajaxHooker.js +35 -21
  45. package/src/indexedDB.ts +8 -8
  46. package/src/types/Httpx.d.ts +73 -67
  47. package/src/types/env.d.ts +2 -0
  48. package/src/types/global.d.ts +3 -0
@@ -18,11 +18,14 @@ export type ArgsType<T extends JSTypeNames[]> = {
18
18
  export declare interface UtilsOwnObject<V extends any> {
19
19
  [key: string]: V | UtilsOwnObject<V>;
20
20
  }
21
+
21
22
  export declare interface AnyObject {
22
23
  [key: string]: any | AnyObject;
23
24
  toString(): string;
24
25
  }
26
+
25
27
  export type PartialKeys<T, K extends keyof T> = {
26
28
  [P in K]?: T[P];
27
29
  };
30
+
28
31
  export type Values<T> = T[keyof T];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@whitesev/utils",
4
- "version": "2.7.0",
4
+ "version": "2.7.2",
5
5
  "type": "module",
6
6
  "description": "一个常用的工具库",
7
7
  "main": "dist/index.cjs.js",
@@ -1,4 +1,4 @@
1
- class ColorConversion {
1
+ export class ColorConversion {
2
2
  /**
3
3
  * 判断是否是16进制颜色
4
4
  * @param str
@@ -15,14 +15,13 @@ class ColorConversion {
15
15
  /**
16
16
  * 16进制颜色转rgba
17
17
  *
18
- * #ff0000 rgba(123,123,123, 0.4)
18
+ * 例如:`#ff0000` 转为 `rgba(123,123,123, 0.4)`
19
19
  * @param hex
20
20
  * @param opacity
21
21
  */
22
22
  hexToRgba(hex: string, opacity: number): string {
23
23
  if (!this.isHex(hex)) {
24
- // @ts-ignore
25
- throw new TypeError("输入错误的hex", hex);
24
+ throw new TypeError("输入错误的hex:" + hex);
26
25
  }
27
26
  return hex && hex.replace(/\s+/g, "").length === 7
28
27
  ? "rgba(" +
@@ -39,20 +38,17 @@ class ColorConversion {
39
38
  /**
40
39
  * hex转rgb
41
40
  * @param str
42
- * @returns
43
41
  */
44
42
  hexToRgb(str: string) {
45
43
  if (!this.isHex(str)) {
46
- // @ts-ignore
47
- throw new TypeError("输入错误的hex", str);
44
+ throw new TypeError("输入错误的hex:" + str);
48
45
  }
49
46
  /* replace替换查找的到的字符串 */
50
47
  str = str.replace("#", "");
51
48
  /* match得到查询数组 */
52
- let hxs = str.match(/../g);
49
+ let hxs = str.match(/../g)!;
53
50
  for (let index = 0; index < 3; index++) {
54
- // @ts-ignore
55
- hxs[index] = parseInt(hxs[index], 16);
51
+ (hxs as any)[index as any] = parseInt(hxs[index], 16);
56
52
  }
57
53
 
58
54
  return hxs;
@@ -62,7 +58,6 @@ class ColorConversion {
62
58
  * @param redValue
63
59
  * @param greenValue
64
60
  * @param blueValue
65
- * @returns
66
61
  */
67
62
  rgbToHex(
68
63
  redValue: string | number,
@@ -90,41 +85,35 @@ class ColorConversion {
90
85
  * 获取颜色变暗或亮
91
86
  * @param color 颜色
92
87
  * @param level 0~1.0
93
- * @returns
94
88
  */
95
89
  getDarkColor(color: string, level: string): string {
96
90
  if (!this.isHex(color)) {
97
- // @ts-ignore
98
- throw new TypeError("输入错误的hex", color);
91
+ throw new TypeError("输入错误的hex:" + color);
99
92
  }
100
93
  let rgbc = this.hexToRgb(color);
101
94
  for (let index = 0; index < 3; index++) {
102
- // @ts-ignore
103
- rgbc[index] = Math.floor(rgbc[index] * (1 - level));
95
+ (rgbc as any)[index] = Math.floor(
96
+ (rgbc as any)[index] * (1 - (level as any))
97
+ );
104
98
  }
105
99
 
106
- // @ts-ignore
107
100
  return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
108
101
  }
109
102
  /**
110
103
  * 获取颜色变亮
111
104
  * @param color 颜色
112
105
  * @param level 0~1.0
113
- * @returns
114
106
  */
115
107
  getLightColor(color: string, level: number): string {
116
108
  if (!this.isHex(color)) {
117
- // @ts-ignore
118
- throw new TypeError("输入错误的hex", color);
109
+ throw new TypeError("输入错误的hex:" + color);
119
110
  }
120
111
  let rgbc = this.hexToRgb(color);
121
112
  for (let index = 0; index < 3; index++) {
122
- // @ts-ignore
123
- rgbc[index] = Math.floor((255 - rgbc[index]) * level + rgbc[index]);
113
+ (rgbc as any)[index] = Math.floor(
114
+ (255 - (rgbc as any)[index]) * (level as any) + (rgbc as any)[index]
115
+ );
124
116
  }
125
- // @ts-ignore
126
117
  return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
127
118
  }
128
119
  }
129
-
130
- export { ColorConversion };
package/src/DOMUtils.ts CHANGED
@@ -88,10 +88,11 @@ class DOMUtils {
88
88
  let textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
89
89
  let text = textMatch![2];
90
90
  selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
91
- return Array.from(parent.querySelectorAll<E>(selector)).filter(($ele) => {
92
- // @ts-ignore
93
- return ($ele?.textContent || $ele?.innerText)?.includes(text);
94
- });
91
+ return Array.from(parent.querySelectorAll<HTMLElement>(selector)).filter(
92
+ ($ele) => {
93
+ return ($ele?.textContent || $ele?.innerText)?.includes(text);
94
+ }
95
+ );
95
96
  } else if (
96
97
  selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) ||
97
98
  selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)
@@ -107,10 +108,11 @@ class DOMUtils {
107
108
  }
108
109
  let regexp = new RegExp(pattern, flags);
109
110
  selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
110
- return Array.from(parent.querySelectorAll<E>(selector)).filter(($ele) => {
111
- // @ts-ignore
112
- return Boolean(($ele?.textContent || $ele?.innerText)?.match(regexp));
113
- });
111
+ return Array.from(parent.querySelectorAll<HTMLElement>(selector)).filter(
112
+ ($ele) => {
113
+ return Boolean(($ele?.textContent || $ele?.innerText)?.match(regexp));
114
+ }
115
+ );
114
116
  } else {
115
117
  // 普通语法
116
118
  return Array.from(parent.querySelectorAll<E>(selector));
@@ -158,8 +160,7 @@ class DOMUtils {
158
160
  let textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
159
161
  let text = textMatch![2];
160
162
  selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
161
- // @ts-ignore
162
- let content = $el?.textContent || $el?.innerText;
163
+ let content = $el?.textContent || (<HTMLElement>$el)?.innerText;
163
164
  if (typeof content !== "string") {
164
165
  content = "";
165
166
  }
@@ -179,8 +180,7 @@ class DOMUtils {
179
180
  }
180
181
  let regexp = new RegExp(pattern, flags);
181
182
  selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
182
- // @ts-ignore
183
- let content = $el?.textContent || $el?.innerText;
183
+ let content = $el?.textContent || (<HTMLElement>$el)?.innerText;
184
184
  if (typeof content !== "string") {
185
185
  content = "";
186
186
  }
@@ -243,8 +243,7 @@ class DOMUtils {
243
243
  selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
244
244
  let $closest = $el?.closest<E>(selector);
245
245
  if ($closest) {
246
- // @ts-ignore
247
- let content = $el?.textContent || $el?.innerText;
246
+ let content = $el?.textContent || (<HTMLElement>$el)?.innerText;
248
247
  if (typeof content === "string" && content.includes(text)) {
249
248
  return $closest;
250
249
  }
@@ -267,8 +266,7 @@ class DOMUtils {
267
266
  selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
268
267
  let $closest = $el?.closest<E>(selector);
269
268
  if ($closest) {
270
- // @ts-ignore
271
- let content = $el?.textContent || $el?.innerText;
269
+ let content = $el?.textContent || (<HTMLElement>$el)?.innerText;
272
270
  if (typeof content === "string" && content.match(regexp)) {
273
271
  return $closest;
274
272
  }
package/src/Dictionary.ts CHANGED
@@ -1,17 +1,47 @@
1
1
  import { CommonUtil } from "./CommonUtil";
2
2
 
3
- class UtilsDictionary<K, V> {
3
+ export class UtilsDictionary<
4
+ K extends string | number | symbol,
5
+ V extends unknown
6
+ > {
4
7
  private items: {
5
- // @ts-ignore
6
- [key: K]: V;
7
- } = {};
8
+ [key: string | number | symbol]: V;
9
+ };
8
10
  constructor();
9
11
  constructor(key: K, value: V);
10
12
  constructor(key?: K, value?: V) {
13
+ this.items = {};
11
14
  if (key != null) {
12
15
  this.set(key, value!);
13
16
  }
14
17
  }
18
+ /**
19
+ * 获取字典的长度,同this.size
20
+ */
21
+ get length() {
22
+ return this.size();
23
+ }
24
+ /**
25
+ * 迭代器
26
+ */
27
+ get entries() {
28
+ let that = this;
29
+ return function* (): IterableIterator<[K, V]> {
30
+ let itemKeys = Object.keys(that.getItems());
31
+ for (const keyName of itemKeys) {
32
+ yield [keyName as K, that.get(keyName as K) as V];
33
+ }
34
+ };
35
+ }
36
+ /**
37
+ * 是否可遍历
38
+ */
39
+ get [Symbol.iterator]() {
40
+ let that = this;
41
+ return function () {
42
+ return that.entries();
43
+ };
44
+ }
15
45
  /**
16
46
  * 检查是否有某一个键
17
47
  * @param key 键
@@ -111,8 +141,7 @@ class UtilsDictionary<K, V> {
111
141
  /**
112
142
  * 返回字典本身
113
143
  */
114
- getItems(): UtilsDictionary<K, V> {
115
- // @ts-ignore
144
+ getItems() {
116
145
  return this.items;
117
146
  }
118
147
  /**
@@ -122,6 +151,10 @@ class UtilsDictionary<K, V> {
122
151
  concat(data: UtilsDictionary<K, V>) {
123
152
  this.items = CommonUtil.assign(this.items, data.getItems());
124
153
  }
154
+ /**
155
+ * 迭代字典
156
+ * @param callbackfn 回调函数
157
+ */
125
158
  forEach(
126
159
  callbackfn: (value: V, key: K, dictionary: UtilsDictionary<K, V>) => void
127
160
  ) {
@@ -129,33 +162,4 @@ class UtilsDictionary<K, V> {
129
162
  callbackfn(this.get(key as any) as V, key as K, this.getItems() as any);
130
163
  }
131
164
  }
132
- /**
133
- * 获取字典的长度,同this.size
134
- */
135
- get length() {
136
- return this.size();
137
- }
138
- /**
139
- * 迭代器
140
- */
141
- get entries() {
142
- let that = this;
143
- return function* (): IterableIterator<[K, V]> {
144
- let itemKeys = Object.keys(that.getItems());
145
- for (const keyName of itemKeys) {
146
- yield [keyName as K, that.get(keyName as K) as V];
147
- }
148
- };
149
- }
150
- /**
151
- * 是否可遍历
152
- */
153
- get [Symbol.iterator]() {
154
- let that = this;
155
- return function () {
156
- return that.entries();
157
- };
158
- }
159
165
  }
160
-
161
- export { UtilsDictionary };
package/src/GBKEncoder.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { UtilsOwnObject } from "./types/global";
2
2
 
3
- class GBKEncoder {
3
+ export class GBKEncoder {
4
4
  #data: RegExpMatchArray = [] as any;
5
5
  #U2Ghash: UtilsOwnObject<string> = {};
6
6
  #G2Uhash: UtilsOwnObject<string> = {};
@@ -93,16 +93,15 @@ class GBKEncoder {
93
93
  * @param str
94
94
  */
95
95
  decode(str: string) {
96
- var GBKMatcher = /%[0-9A-F]{2}%[0-9A-F]{2}/;
97
- var UTFMatcher = /%[0-9A-F]{2}/;
98
- // @ts-ignore
99
- var gbk = true,
100
- utf = true;
101
- let that = this;
96
+ let GBKMatcher = /%[0-9A-F]{2}%[0-9A-F]{2}/;
97
+ let UTFMatcher = /%[0-9A-F]{2}/;
98
+ // let gbk = true;
99
+ let utf = true;
100
+ const that = this;
102
101
  while (utf) {
103
102
  let gbkMatch = str.match(GBKMatcher);
104
103
  let utfMatch = str.match(UTFMatcher);
105
- gbk = Boolean(gbkMatch);
104
+ // gbk = Boolean(gbkMatch);
106
105
  utf = Boolean(utfMatch);
107
106
  if (gbkMatch && (gbkMatch as any) in that.#G2Uhash) {
108
107
  str = str.replace(
@@ -110,12 +109,9 @@ class GBKEncoder {
110
109
  String.fromCharCode(("0x" + that.#G2Uhash[gbkMatch as any]) as any)
111
110
  );
112
111
  } else {
113
- // @ts-ignore
114
- str = str.replace(utfMatch, decodeURIComponent(utfMatch));
112
+ str = str.replace(utfMatch as any, decodeURIComponent(utfMatch as any));
115
113
  }
116
114
  }
117
115
  return str;
118
116
  }
119
117
  }
120
-
121
- export { GBKEncoder };
package/src/Hooks.ts CHANGED
@@ -1,4 +1,4 @@
1
- class Hooks {
1
+ export class Hooks {
2
2
  /**
3
3
  * 在Function原型上添加自定义方法.hook和.unhook
4
4
  */
@@ -82,5 +82,3 @@ class Hooks {
82
82
  return true;
83
83
  }
84
84
  }
85
-
86
- export { Hooks };