@whitesev/utils 1.0.5 → 1.0.8

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 (59) hide show
  1. package/dist/index.amd.js +2123 -2269
  2. package/dist/index.amd.js.map +1 -1
  3. package/dist/index.cjs.js +2123 -2269
  4. package/dist/index.cjs.js.map +1 -1
  5. package/dist/index.esm.js +2123 -2269
  6. package/dist/index.esm.js.map +1 -1
  7. package/dist/index.iife.js +2123 -2269
  8. package/dist/index.iife.js.map +1 -1
  9. package/dist/index.system.js +2123 -2269
  10. package/dist/index.system.js.map +1 -1
  11. package/dist/index.umd.js +2123 -2269
  12. package/dist/index.umd.js.map +1 -1
  13. package/dist/src/Dictionary.d.ts +82 -0
  14. package/dist/src/Hooks.d.ts +11 -0
  15. package/dist/src/Httpx.d.ts +1201 -0
  16. package/dist/src/LockFunction.d.ts +31 -0
  17. package/dist/src/Log.d.ts +96 -0
  18. package/dist/src/Progress.d.ts +37 -0
  19. package/dist/src/UtilsGMMenu.d.ts +156 -0
  20. package/dist/src/index.d.ts +20 -27
  21. package/dist/src/indexedDB.d.ts +73 -0
  22. package/dist/src/tryCatch.d.ts +31 -0
  23. package/package.json +36 -36
  24. package/src/Dictionary.ts +152 -0
  25. package/src/{Hooks/Hooks.js → Hooks.ts} +31 -17
  26. package/src/{Httpx/index.d.ts → Httpx.ts} +837 -46
  27. package/src/LockFunction.ts +62 -0
  28. package/src/Log.ts +281 -0
  29. package/src/Progress.ts +143 -0
  30. package/src/UtilsGMMenu.ts +530 -0
  31. package/src/index.ts +17 -29
  32. package/src/indexedDB.ts +421 -0
  33. package/src/tryCatch.ts +107 -0
  34. package/tsconfig.json +1 -1
  35. package/dist/src/Dictionary/Dictionary.d.ts +0 -85
  36. package/dist/src/Hooks/Hooks.d.ts +0 -5
  37. package/dist/src/Httpx/Httpx.d.ts +0 -50
  38. package/dist/src/LockFunction/LockFunction.d.ts +0 -16
  39. package/dist/src/Log/Log.d.ts +0 -66
  40. package/dist/src/Progress/Progress.d.ts +0 -6
  41. package/dist/src/UtilsGMMenu/UtilsGMMenu.d.ts +0 -119
  42. package/dist/src/indexedDB/indexedDB.d.ts +0 -165
  43. package/dist/src/tryCatch/tryCatch.d.ts +0 -31
  44. package/src/Dictionary/Dictionary.js +0 -157
  45. package/src/Dictionary/index.d.ts +0 -52
  46. package/src/Hooks/index.d.ts +0 -16
  47. package/src/Httpx/Httpx.js +0 -747
  48. package/src/LockFunction/LockFunction.js +0 -35
  49. package/src/LockFunction/index.d.ts +0 -47
  50. package/src/Log/Log.js +0 -256
  51. package/src/Log/index.d.ts +0 -91
  52. package/src/Progress/Progress.js +0 -98
  53. package/src/Progress/index.d.ts +0 -30
  54. package/src/UtilsGMMenu/UtilsGMMenu.js +0 -464
  55. package/src/UtilsGMMenu/index.d.ts +0 -224
  56. package/src/indexedDB/index.d.ts +0 -128
  57. package/src/indexedDB/indexedDB.js +0 -355
  58. package/src/tryCatch/index.d.ts +0 -6
  59. package/src/tryCatch/tryCatch.js +0 -100
@@ -0,0 +1,152 @@
1
+ import { Utils } from ".";
2
+
3
+ class UtilsDictionary<K extends PropertyKey, V extends any> {
4
+ #items: {
5
+ [key: PropertyKey]: V;
6
+ } = {};
7
+ constructor() {}
8
+ /**
9
+ * 检查是否有某一个键
10
+ * @param key 键
11
+ */
12
+ has(key: K): boolean {
13
+ return this.#items.hasOwnProperty(key as PropertyKey);
14
+ }
15
+ /**
16
+ * 检查已有的键中是否以xx开头
17
+ * @param key 需要匹配的键
18
+ */
19
+ startsWith(key: K): boolean {
20
+ let allKeys = this.keys();
21
+ for (const keyName of allKeys) {
22
+ if (keyName.startsWith(key as string)) {
23
+ return true;
24
+ }
25
+ }
26
+ return false;
27
+ }
28
+ /**
29
+ * 获取以xx开头的键的值
30
+ * @param key 需要匹配的键
31
+ */
32
+ getStartsWith(key: K): V {
33
+ let allKeys = this.keys();
34
+ let result = null;
35
+ for (const keyName of allKeys) {
36
+ if (keyName.startsWith(key as string)) {
37
+ result = (this.#items as any)[keyName];
38
+ break;
39
+ }
40
+ }
41
+ return result;
42
+ }
43
+ /**
44
+ * 为字典添加某一个值
45
+ * @param key 键
46
+ * @param val 值,默认为""
47
+ */
48
+ set(key: K, val: V): void {
49
+ if (key === void 0) {
50
+ throw new Error("Utils.Dictionary().set 参数 key 不能为空");
51
+ }
52
+ (this.#items as any)[key] = val;
53
+ }
54
+ /**
55
+ * 删除某一个键
56
+ * @param key 键
57
+ */
58
+ delete(key: K): boolean {
59
+ if (this.has(key)) {
60
+ Reflect.deleteProperty(this.#items, key as string);
61
+ return true;
62
+ }
63
+ return false;
64
+ }
65
+ /**
66
+ * 获取某个键的值
67
+ * @param key 键
68
+ */
69
+ get(key: K) {
70
+ return this.has(key) ? this.getItems()[key] : void 0;
71
+ }
72
+ /**
73
+ * 返回字典中的所有值
74
+ */
75
+ values() {
76
+ let resultList: V[] = [];
77
+ for (let prop in this.getItems()) {
78
+ if (this.has(prop as K)) {
79
+ resultList.push(this.getItems()[prop]);
80
+ }
81
+ }
82
+ return resultList;
83
+ }
84
+ /**
85
+ * 清空字典
86
+ */
87
+ clear() {
88
+ this.#items = void 0 as any;
89
+ this.#items = {};
90
+ }
91
+ /**
92
+ * 获取字典的长度
93
+ */
94
+ size() {
95
+ return Object.keys(this.getItems()).length;
96
+ }
97
+ /**
98
+ * 获取字典所有的键
99
+ */
100
+ keys() {
101
+ return Object.keys(this.getItems());
102
+ }
103
+ /**
104
+ * 返回字典本身
105
+ */
106
+ getItems() {
107
+ return this.#items;
108
+ }
109
+ /**
110
+ * 合并另一个字典
111
+ * @param data 需要合并的字典
112
+ */
113
+ concat(data: UtilsDictionary<K, V>) {
114
+ this.#items = Utils.assign(this.#items, data.getItems());
115
+ }
116
+ forEach(
117
+ callbackfn: (value: V, key: K, dictionary: UtilsDictionary<K, V>) => void
118
+ ) {
119
+ for (const key in this.getItems()) {
120
+ callbackfn(this.get(key as any) as V, key as K, this.getItems() as any);
121
+ }
122
+ }
123
+ /**
124
+ * 获取字典的长度,同this.size
125
+ */
126
+ get length() {
127
+ return this.size();
128
+ }
129
+ /**
130
+ * 迭代器
131
+ */
132
+ get entries() {
133
+ let that = this;
134
+ return function* (): IterableIterator<[K, V]> {
135
+ let itemKeys = Object.keys(that.getItems());
136
+ for (const keyName of itemKeys) {
137
+ yield [keyName as K, that.get(keyName as K) as V];
138
+ }
139
+ };
140
+ }
141
+ /**
142
+ * 是否可遍历
143
+ */
144
+ get [Symbol.iterator]() {
145
+ let that = this;
146
+ return function () {
147
+ return that.entries();
148
+ };
149
+ }
150
+ }
151
+
152
+ export { UtilsDictionary };
@@ -1,21 +1,28 @@
1
- const Hooks = function () {
2
- this.initEnv = function () {
3
- Function.prototype.hook = function (realFunc, hookFunc, context) {
1
+ class Hooks {
2
+ /**
3
+ * 在Function原型上添加自定义方法.hook和.unhook
4
+ */
5
+ initEnv() {
6
+ (Function.prototype as any).hook = function (
7
+ realFunc: any,
8
+ hookFunc: any,
9
+ context: Window & typeof globalThis
10
+ ) {
4
11
  let _context = null; //函数上下文
5
12
  let _funcName = null; //函数名
6
13
 
7
14
  _context = context || window;
8
15
  _funcName = getFuncName(this);
9
- _context["realFunc_" + _funcName] = this;
16
+ (_context as any)["realFunc_" + _funcName] = this;
10
17
 
11
18
  if (
12
- _context[_funcName].prototype &&
13
- _context[_funcName].prototype.isHooked
19
+ (_context[_funcName] as any).prototype &&
20
+ (_context[_funcName] as any).prototype.isHooked
14
21
  ) {
15
22
  console.log("Already has been hooked,unhook first");
16
23
  return false;
17
24
  }
18
- function getFuncName(fn) {
25
+ function getFuncName(fn: any) {
19
26
  // 获取函数名
20
27
  let strFunc = fn.toString();
21
28
  let _regex = /function\s+(\w+)\s*\(/;
@@ -38,36 +45,43 @@ const Hooks = function () {
38
45
  "'].apply(obj,args);\n" +
39
46
  "};"
40
47
  );
41
- _context[_funcName].prototype.isHooked = true;
48
+ (_context as any)[_funcName].prototype.isHooked = true;
42
49
  return true;
43
50
  } catch (e) {
44
51
  console.log("Hook failed,check the params.");
45
52
  return false;
46
53
  }
47
54
  };
48
- Function.prototype.unhook = function (realFunc, funcName, context) {
55
+ (Function.prototype as any).unhook = function (
56
+ realFunc: any,
57
+ funcName: any,
58
+ context: Window & typeof globalThis
59
+ ) {
49
60
  let _context = null;
50
61
  let _funcName = null;
51
62
  _context = context || window;
52
63
  _funcName = funcName;
53
- if (!_context[_funcName].prototype.isHooked) {
64
+ if (!(_context[_funcName] as any).prototype.isHooked) {
54
65
  console.log("No function is hooked on");
55
66
  return false;
56
67
  }
57
- _context[_funcName] = _context["realFunc" + _funcName];
68
+ (_context[_funcName] as any) = (_context as any)["realFunc" + _funcName];
58
69
  Reflect.deleteProperty(_context, "realFunc_" + _funcName);
59
70
  return true;
60
71
  };
61
- };
62
- this.cleanEnv = function () {
72
+ }
73
+ /**
74
+ * 删除在Function原型上添加的自定义方法.hook和.unhook
75
+ */
76
+ cleanEnv() {
63
77
  if (Function.prototype.hasOwnProperty("hook")) {
64
- Reflect.deleteProperty(unction.prototype, "hook");
78
+ Reflect.deleteProperty(Function.prototype, "hook");
65
79
  }
66
80
  if (Function.prototype.hasOwnProperty("unhook")) {
67
- Reflect.deleteProperty(unction.prototype, "unhook");
81
+ Reflect.deleteProperty(Function.prototype, "unhook");
68
82
  }
69
83
  return true;
70
- };
71
- };
84
+ }
85
+ }
72
86
 
73
87
  export { Hooks };