@whitesev/utils 2.7.4 → 2.7.6

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.
@@ -1,4 +1,4 @@
1
- export declare class UtilsDictionary<K extends string | number | symbol, V extends unknown> {
1
+ export declare class UtilsDictionary<K extends unknown, V extends any> {
2
2
  private items;
3
3
  constructor();
4
4
  constructor(key: K, value: V);
@@ -20,15 +20,12 @@ export declare class UtilsDictionary<K extends string | number | symbol, V exten
20
20
  */
21
21
  has(key: K): boolean;
22
22
  /**
23
- * 检查已有的键中是否以xx开头
24
- * @param key 需要匹配的键
25
- */
26
- startsWith(key: K): boolean;
27
- /**
28
- * 获取以xx开头的键的值
29
- * @param key 需要匹配的键
23
+ * 获取某个键的值
24
+ * https://github.com/microsoft/TypeScript/issues/9619
25
+ * 微软到现在都没有实现has和get的联动
26
+ * @param key
30
27
  */
31
- getStartsWith(key: K): V | undefined;
28
+ get(key: K): V;
32
29
  /**
33
30
  * 为字典添加某一个值
34
31
  * @param key 键
@@ -38,15 +35,15 @@ export declare class UtilsDictionary<K extends string | number | symbol, V exten
38
35
  /**
39
36
  * 删除某一个键
40
37
  * @param key 键
38
+ * @returns
39
+ * + true:键存在且成功删除
40
+ * + false:键不存在
41
41
  */
42
42
  delete(key: K): boolean;
43
43
  /**
44
- * 获取某个键的值
45
- * https://github.com/microsoft/TypeScript/issues/9619
46
- * 微软到现在都没有修复has和get的联动
47
- * @param key 键
44
+ * 获取字典所有的键
48
45
  */
49
- get(key: K): V;
46
+ keys(): K[];
50
47
  /**
51
48
  * 返回字典中的所有值
52
49
  */
@@ -59,18 +56,10 @@ export declare class UtilsDictionary<K extends string | number | symbol, V exten
59
56
  * 获取字典的长度
60
57
  */
61
58
  size(): number;
62
- /**
63
- * 获取字典所有的键
64
- */
65
- keys(): (string | symbol)[];
66
59
  /**
67
60
  * 返回字典本身
68
61
  */
69
- getItems(): {
70
- [key: string]: V;
71
- [key: number]: V;
72
- [key: symbol]: V;
73
- };
62
+ getItems(): Map<K, V>;
74
63
  /**
75
64
  * 合并另一个字典
76
65
  * @param data 需要合并的字典
@@ -81,4 +70,14 @@ export declare class UtilsDictionary<K extends string | number | symbol, V exten
81
70
  * @param callbackfn 回调函数
82
71
  */
83
72
  forEach(callbackfn: (value: V, key: K, dictionary: UtilsDictionary<K, V>) => void): void;
73
+ /**
74
+ * 检查已有的键中是否以xx开头
75
+ * @param key 需要匹配的键
76
+ */
77
+ startsWith(key: string): boolean;
78
+ /**
79
+ * 获取以xx开头的键的值
80
+ * @param key 需要匹配的键
81
+ */
82
+ getStartsWith(key: K): V | undefined;
84
83
  }
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.4",
4
+ "version": "2.7.6",
5
5
  "type": "module",
6
6
  "description": "一个常用的工具库",
7
7
  "main": "dist/index.cjs.js",
package/src/Dictionary.ts CHANGED
@@ -1,13 +1,9 @@
1
- import { CommonUtil } from "./CommonUtil";
2
-
3
- export class UtilsDictionary<K extends string | number | symbol, V extends unknown> {
4
- private items: {
5
- [key: string | number | symbol]: V;
6
- };
1
+ export class UtilsDictionary<K extends unknown, V extends any> {
2
+ private items: Map<K, V>;
7
3
  constructor();
8
4
  constructor(key: K, value: V);
9
5
  constructor(key?: K, value?: V) {
10
- this.items = {};
6
+ this.items = new Map();
11
7
  if (key != null) {
12
8
  this.set(key, value!);
13
9
  }
@@ -44,35 +40,16 @@ export class UtilsDictionary<K extends string | number | symbol, V extends unkno
44
40
  * @param key 键
45
41
  */
46
42
  has(key: K): boolean {
47
- return Reflect.has(this.items, key as PropertyKey);
43
+ return this.items.has(key);
48
44
  }
49
45
  /**
50
- * 检查已有的键中是否以xx开头
51
- * @param key 需要匹配的键
52
- */
53
- startsWith(key: K): boolean {
54
- let allKeys = this.keys();
55
- for (const keyName of allKeys) {
56
- if (String(keyName).startsWith(String(key))) {
57
- return true;
58
- }
59
- }
60
- return false;
61
- }
62
- /**
63
- * 获取以xx开头的键的值
64
- * @param key 需要匹配的键
46
+ * 获取某个键的值
47
+ * https://github.com/microsoft/TypeScript/issues/9619
48
+ * 微软到现在都没有实现has和get的联动
49
+ * @param key
65
50
  */
66
- getStartsWith(key: K): V | undefined {
67
- let allKeys = this.keys();
68
- let result: V | undefined = void 0;
69
- for (const keyName of allKeys) {
70
- if (String(keyName).startsWith(String(key))) {
71
- result = this.get(keyName as K)!;
72
- break;
73
- }
74
- }
75
- return result;
51
+ get(key: K): V {
52
+ return this.items.get(key) as V;
76
53
  }
77
54
  /**
78
55
  * 为字典添加某一个值
@@ -83,57 +60,44 @@ export class UtilsDictionary<K extends string | number | symbol, V extends unkno
83
60
  if (key === void 0) {
84
61
  throw new Error("Utils.Dictionary().set 参数 key 不能为空");
85
62
  }
86
- Reflect.set(this.items, key as PropertyKey, val);
63
+ this.items.set(key, val);
87
64
  }
88
65
  /**
89
66
  * 删除某一个键
90
67
  * @param key 键
68
+ * @returns
69
+ * + true:键存在且成功删除
70
+ * + false:键不存在
91
71
  */
92
72
  delete(key: K): boolean {
93
73
  if (this.has(key)) {
94
- return Reflect.deleteProperty(this.items, key as string);
74
+ return this.items.delete(key);
95
75
  }
96
76
  return false;
97
77
  }
98
78
  /**
99
- * 获取某个键的值
100
- * https://github.com/microsoft/TypeScript/issues/9619
101
- * 微软到现在都没有修复has和get的联动
102
- * @param key 键
79
+ * 获取字典所有的键
103
80
  */
104
- get(key: K): V {
105
- return Reflect.get(this.items, key as PropertyKey) as V;
81
+ keys(): K[] {
82
+ return this.items.keys().toArray();
106
83
  }
107
84
  /**
108
85
  * 返回字典中的所有值
109
86
  */
110
87
  values(): V[] {
111
- let resultList: V[] = [];
112
- for (let prop in this.getItems()) {
113
- if (this.has(prop as K)) {
114
- resultList.push(this.get(prop as K)!);
115
- }
116
- }
117
- return resultList;
88
+ return this.items.values().toArray();
118
89
  }
119
90
  /**
120
91
  * 清空字典
121
92
  */
122
93
  clear() {
123
- this.items = null as any;
124
- this.items = {};
94
+ this.items.clear();
125
95
  }
126
96
  /**
127
97
  * 获取字典的长度
128
98
  */
129
99
  size(): number {
130
- return Object.keys(this.getItems()).length;
131
- }
132
- /**
133
- * 获取字典所有的键
134
- */
135
- keys(): (string | symbol)[] {
136
- return Reflect.ownKeys(this.items);
100
+ return this.items.size;
137
101
  }
138
102
  /**
139
103
  * 返回字典本身
@@ -146,15 +110,45 @@ export class UtilsDictionary<K extends string | number | symbol, V extends unkno
146
110
  * @param data 需要合并的字典
147
111
  */
148
112
  concat(data: UtilsDictionary<K, V>) {
149
- this.items = CommonUtil.assign(this.items, data.getItems());
113
+ data.forEach((value, key) => {
114
+ this.items.set(key, value);
115
+ });
150
116
  }
151
117
  /**
152
118
  * 迭代字典
153
119
  * @param callbackfn 回调函数
154
120
  */
155
121
  forEach(callbackfn: (value: V, key: K, dictionary: UtilsDictionary<K, V>) => void) {
156
- for (const key in this.getItems()) {
157
- callbackfn(this.get(key as any) as V, key as K, this.getItems() as any);
122
+ this.items.forEach((value, key, self) => {
123
+ callbackfn(value, key, this);
124
+ });
125
+ }
126
+ /**
127
+ * 检查已有的键中是否以xx开头
128
+ * @param key 需要匹配的键
129
+ */
130
+ startsWith(key: string): boolean {
131
+ const keys = this.keys();
132
+ for (const keyName of keys) {
133
+ if (String(keyName).startsWith(key)) {
134
+ return true;
135
+ }
158
136
  }
137
+ return false;
138
+ }
139
+ /**
140
+ * 获取以xx开头的键的值
141
+ * @param key 需要匹配的键
142
+ */
143
+ getStartsWith(key: K): V | undefined {
144
+ let result: V | undefined = void 0;
145
+ const keys = this.keys();
146
+ for (const keyName of keys) {
147
+ if (String(keyName).startsWith(String(key))) {
148
+ result = this.get(keyName as K);
149
+ break;
150
+ }
151
+ }
152
+ return result;
159
153
  }
160
154
  }
package/src/Utils.ts CHANGED
@@ -35,7 +35,7 @@ class Utils {
35
35
  this.windowApi = new WindowApi(option);
36
36
  }
37
37
  /** 版本号 */
38
- version = "2025.8.21";
38
+ version = "2025.9.8";
39
39
  /**
40
40
  * 在页面中增加style元素,如果html节点存在子节点,添加子节点第一个,反之,添加到html节点的子节点最后一个
41
41
  * @param cssText css字符串
@@ -462,7 +462,6 @@ class Utils {
462
462
  linkElement.click();
463
463
  }
464
464
  }
465
-
466
465
  /**
467
466
  * 选中页面中的文字,类似Ctrl+F的选中
468
467
  * @param str (可选)需要寻找的字符串,默认为空
@@ -532,28 +531,28 @@ class Utils {
532
531
  filter?: (element: T) => boolean
533
532
  ) {
534
533
  let that = this;
535
- if ((element as HTMLElement).outerHTML.includes(text)) {
536
- if ((element as HTMLElement).children.length === 0) {
534
+ if ((<HTMLElement>element).outerHTML.includes(text)) {
535
+ if ((<HTMLElement>element).children.length === 0) {
537
536
  let filterResult = typeof filter === "function" ? filter(element) : false;
538
537
  if (!filterResult) {
539
538
  yield element as any;
540
539
  }
541
540
  } else {
542
541
  let textElement = Array.from(element.childNodes).filter((ele) => ele.nodeType === Node.TEXT_NODE);
543
- for (let ele of textElement) {
544
- if ((ele as any).textContent.includes(text)) {
542
+ for (let $child of textElement) {
543
+ if ((<HTMLElement>$child).textContent.includes(text)) {
545
544
  let filterResult = typeof filter === "function" ? filter(element) : false;
546
545
  if (!filterResult) {
547
- yield ele;
546
+ yield $child;
548
547
  }
549
548
  }
550
549
  }
551
550
  }
552
551
  }
553
552
 
554
- for (let index = 0; index < (element as HTMLElement).children.length; index++) {
555
- let childElement = (element as HTMLElement).children[index] as any;
556
- yield* that.findElementsWithText(childElement, text, filter);
553
+ for (let index = 0; index < (<HTMLElement>element).children.length; index++) {
554
+ let $child = (<HTMLElement>element).children[index] as any;
555
+ yield* that.findElementsWithText($child, text, filter);
557
556
  }
558
557
  }
559
558
  /**
package/src/WindowApi.ts CHANGED
@@ -8,10 +8,10 @@ export class WindowApi {
8
8
  globalThis: globalThis,
9
9
  self: self,
10
10
  top: top!,
11
- setTimeout: globalThis.setTimeout,
12
- setInterval: globalThis.setInterval,
13
- clearTimeout: globalThis.clearTimeout,
14
- clearInterval: globalThis.clearInterval,
11
+ setTimeout: globalThis.setTimeout.bind(globalThis),
12
+ setInterval: globalThis.setInterval.bind(globalThis),
13
+ clearTimeout: globalThis.clearTimeout.bind(globalThis),
14
+ clearInterval: globalThis.clearInterval.bind(globalThis),
15
15
  };
16
16
  /** 使用的配置 */
17
17
  private api: Required<WindowApiOption>;