@whitesev/utils 2.7.5 → 2.7.7

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
  }
@@ -15,6 +15,7 @@ import { Vue } from "./Vue";
15
15
  import { type ArgsType, type JSTypeNames, type UtilsOwnObject } from "./types/global";
16
16
  import type { WindowApiOption } from "./types/WindowApi";
17
17
  import { ModuleRaid } from "./ModuleRaid";
18
+ import type { ReactInstance } from "./types/React";
18
19
  declare class Utils {
19
20
  private windowApi;
20
21
  constructor(option?: WindowApiOption);
@@ -529,14 +530,7 @@ declare class Utils {
529
530
  * @example
530
531
  * Utils.getReactObj(document.querySelector("input"))?.reactProps?.onChange({target:{value:"123"}});
531
532
  */
532
- getReactObj(element: HTMLElement | Element): {
533
- reactFiber?: any;
534
- reactProps?: any;
535
- reactEvents?: any;
536
- reactEventHandlers?: any;
537
- reactInternalInstance?: any;
538
- reactContainer?: any;
539
- };
533
+ getReactObj(element: HTMLElement | Element): ReactInstance;
540
534
  /**
541
535
  * 获取对象上的Symbol属性,如果没设置keyName,那么返回一个对象,对象是所有遍历到的Symbol对象
542
536
  * @param target 目标对象
@@ -0,0 +1,119 @@
1
+ export declare interface ReactFiberNode {
2
+ /**
3
+ * 指向当前节点在上一次更新时的对应节点。
4
+ */
5
+ alternate: ReactFiberNode;
6
+ /**
7
+ * 指向该 FiberNode 的第一个子节点
8
+ */
9
+ child: ReactFiberNode | null;
10
+ /**
11
+ * 代表子节点上的更新优先级。
12
+ */
13
+ childLanes: number;
14
+ /**
15
+ * 存储节点的依赖信息,用于处理 useEffect 等情况。
16
+ */
17
+ dependencies: any | null;
18
+ /**
19
+ * 大部分情况与 type 相同,某些情况不同,比如 FunctionComponent 使用 React.memo 包裹,表示元素的类型
20
+ */
21
+ elementType: string;
22
+ /**
23
+ * 存储 FiberNode 的标记,表示节点上的各种状态和变化(删除、新增、替换等)。flags 的定义在 packages\react-reconciler\src\ReactFiberFlags.js 文件中,flags 为 number 类型。
24
+ */
25
+ flags: number;
26
+ /**
27
+ * 当前节点在父节点的子节点列表中的索引
28
+ */
29
+ index: number;
30
+ /**
31
+ * 节点的唯一标识符,用于进行节点的 diff 和更新
32
+ */
33
+ key: any | null;
34
+ /**
35
+ * 代表当前节点上的更新优先级。
36
+ */
37
+ lanes: number;
38
+ /**
39
+ * 表示节点上一次渲染的 props 。在完成本次更新之前,memoizedProps 中存储的是上一次渲染时的 props ,用于对比新旧 props 是否发生变化。
40
+ */
41
+ memoizedProps: any;
42
+ /**
43
+ * 类组件保存上次渲染后的 state ,函数组件保存的 hooks 信息。
44
+ */
45
+ memoizedState: any | null;
46
+ /**
47
+ * 表示节点的模式,如 ConcurrentMode 、StrictMode 等。
48
+ */
49
+ mode: number;
50
+ /**
51
+ * 表示即将被应用到节点的 props 。当父组件发生更新时,会将新的 props 存储在 pendingProps 中,之后会被应用到节点。
52
+ */
53
+ pendingProps: any;
54
+ /**
55
+ * 存储 FiberNode 的引用信息,与 React 的 Ref 有关。使用 ref 引用值
56
+ */
57
+ ref: any | null;
58
+ /**
59
+ * 指向该 FiberNode 的父节点
60
+ */
61
+ return: ReactFiberNode | null;
62
+ /**
63
+ * 指向右边第一个兄弟 Fiber 节点
64
+ */
65
+ sibling: ReactFiberNode | null;
66
+ /**
67
+ * FiberNode 对应的真实 DOM 节点
68
+ */
69
+ stateNode: HTMLElement;
70
+ /**
71
+ * 表示节点类型的标记,例如 FunctionComponent 、ClassComponent 等
72
+ */
73
+ tag: number;
74
+ /**
75
+ * 表示元素的类型, 对于 FunctionComponent,指函数本身,对于ClassComponent,指 class,对于 HostComponent,指 DOM 节点 tagName
76
+ */
77
+ type: string;
78
+ /**
79
+ * 用于存储组件的更新状态,比如新的状态、属性或者 context 的变化。通过 updateQueue ,React 可以跟踪组件的更新并在合适的时机执行更新。
80
+ */
81
+ updateQueue: any;
82
+ [key: string | symbol]: any;
83
+ }
84
+
85
+ export declare interface ReactProps {
86
+ $$typeof: symbol;
87
+ children: (boolean | ReactProps)[] | ReactProps;
88
+ key: string | null;
89
+ ref: any;
90
+ props: ReactProps;
91
+ type: string;
92
+ _owner: any;
93
+ [key: string | symbol]: any;
94
+ }
95
+
96
+ export declare interface ReactEvents {
97
+ [key: string | symbol]: any;
98
+ }
99
+
100
+ export declare interface ReactEventHandlers {
101
+ [key: string | symbol]: any;
102
+ }
103
+
104
+ export declare interface ReactInternalInstance {
105
+ [key: string | symbol]: any;
106
+ }
107
+
108
+ export declare interface ReactContainer {
109
+ [key: string | symbol]: any;
110
+ }
111
+
112
+ export declare interface ReactInstance {
113
+ reactFiber?: ReactFiberNode;
114
+ reactProps?: ReactProps;
115
+ reactEvents?: ReactEvents;
116
+ reactEventHandlers?: ReactEventHandlers;
117
+ reactInternalInstance?: ReactInternalInstance;
118
+ reactContainer?: ReactContainer;
119
+ }
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.5",
4
+ "version": "2.7.7",
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
@@ -28,6 +28,7 @@ import {
28
28
  import { ModuleRaid } from "./ModuleRaid";
29
29
  import { domUtils } from "./DOMUtils";
30
30
  import { CommonUtil } from "./CommonUtil";
31
+ import type { ReactInstance } from "./types/React";
31
32
 
32
33
  class Utils {
33
34
  private windowApi: typeof WindowApi.prototype;
@@ -35,7 +36,7 @@ class Utils {
35
36
  this.windowApi = new WindowApi(option);
36
37
  }
37
38
  /** 版本号 */
38
- version = "2025.8.21";
39
+ version = "2025.9.8";
39
40
  /**
40
41
  * 在页面中增加style元素,如果html节点存在子节点,添加子节点第一个,反之,添加到html节点的子节点最后一个
41
42
  * @param cssText css字符串
@@ -462,7 +463,6 @@ class Utils {
462
463
  linkElement.click();
463
464
  }
464
465
  }
465
-
466
466
  /**
467
467
  * 选中页面中的文字,类似Ctrl+F的选中
468
468
  * @param str (可选)需要寻找的字符串,默认为空
@@ -532,28 +532,28 @@ class Utils {
532
532
  filter?: (element: T) => boolean
533
533
  ) {
534
534
  let that = this;
535
- if ((element as HTMLElement).outerHTML.includes(text)) {
536
- if ((element as HTMLElement).children.length === 0) {
535
+ if ((<HTMLElement>element).outerHTML.includes(text)) {
536
+ if ((<HTMLElement>element).children.length === 0) {
537
537
  let filterResult = typeof filter === "function" ? filter(element) : false;
538
538
  if (!filterResult) {
539
539
  yield element as any;
540
540
  }
541
541
  } else {
542
542
  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)) {
543
+ for (let $child of textElement) {
544
+ if ((<HTMLElement>$child).textContent.includes(text)) {
545
545
  let filterResult = typeof filter === "function" ? filter(element) : false;
546
546
  if (!filterResult) {
547
- yield ele;
547
+ yield $child;
548
548
  }
549
549
  }
550
550
  }
551
551
  }
552
552
  }
553
553
 
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);
554
+ for (let index = 0; index < (<HTMLElement>element).children.length; index++) {
555
+ let $child = (<HTMLElement>element).children[index] as any;
556
+ yield* that.findElementsWithText($child, text, filter);
557
557
  }
558
558
  }
559
559
  /**
@@ -1338,22 +1338,20 @@ class Utils {
1338
1338
  * @example
1339
1339
  * Utils.getReactObj(document.querySelector("input"))?.reactProps?.onChange({target:{value:"123"}});
1340
1340
  */
1341
- getReactObj(element: HTMLElement | Element): {
1342
- reactFiber?: any;
1343
- reactProps?: any;
1344
- reactEvents?: any;
1345
- reactEventHandlers?: any;
1346
- reactInternalInstance?: any;
1347
- reactContainer?: any;
1348
- } {
1341
+ getReactObj(element: HTMLElement | Element): ReactInstance {
1349
1342
  let result = {};
1350
- Object.keys(element).forEach((domPropsName) => {
1343
+ if (element == null) {
1344
+ return result;
1345
+ }
1346
+ const keys = Object.keys(element);
1347
+ keys.forEach((domPropsName) => {
1351
1348
  if (domPropsName.startsWith("__react")) {
1352
- let propsName = domPropsName.replace(/__(.+)\$.+/i, "$1");
1349
+ const propsName = domPropsName.replace(/__(.+)\$.+/i, "$1");
1350
+ const propsValue = Reflect.get(element, domPropsName);
1353
1351
  if (propsName in result) {
1354
1352
  new Error("重复属性 " + domPropsName);
1355
1353
  } else {
1356
- (result as any)[propsName] = (element as any)[domPropsName];
1354
+ Reflect.set(result, propsName, propsValue);
1357
1355
  }
1358
1356
  }
1359
1357
  });
@@ -0,0 +1,119 @@
1
+ export declare interface ReactFiberNode {
2
+ /**
3
+ * 指向当前节点在上一次更新时的对应节点。
4
+ */
5
+ alternate: ReactFiberNode;
6
+ /**
7
+ * 指向该 FiberNode 的第一个子节点
8
+ */
9
+ child: ReactFiberNode | null;
10
+ /**
11
+ * 代表子节点上的更新优先级。
12
+ */
13
+ childLanes: number;
14
+ /**
15
+ * 存储节点的依赖信息,用于处理 useEffect 等情况。
16
+ */
17
+ dependencies: any | null;
18
+ /**
19
+ * 大部分情况与 type 相同,某些情况不同,比如 FunctionComponent 使用 React.memo 包裹,表示元素的类型
20
+ */
21
+ elementType: string;
22
+ /**
23
+ * 存储 FiberNode 的标记,表示节点上的各种状态和变化(删除、新增、替换等)。flags 的定义在 packages\react-reconciler\src\ReactFiberFlags.js 文件中,flags 为 number 类型。
24
+ */
25
+ flags: number;
26
+ /**
27
+ * 当前节点在父节点的子节点列表中的索引
28
+ */
29
+ index: number;
30
+ /**
31
+ * 节点的唯一标识符,用于进行节点的 diff 和更新
32
+ */
33
+ key: any | null;
34
+ /**
35
+ * 代表当前节点上的更新优先级。
36
+ */
37
+ lanes: number;
38
+ /**
39
+ * 表示节点上一次渲染的 props 。在完成本次更新之前,memoizedProps 中存储的是上一次渲染时的 props ,用于对比新旧 props 是否发生变化。
40
+ */
41
+ memoizedProps: any;
42
+ /**
43
+ * 类组件保存上次渲染后的 state ,函数组件保存的 hooks 信息。
44
+ */
45
+ memoizedState: any | null;
46
+ /**
47
+ * 表示节点的模式,如 ConcurrentMode 、StrictMode 等。
48
+ */
49
+ mode: number;
50
+ /**
51
+ * 表示即将被应用到节点的 props 。当父组件发生更新时,会将新的 props 存储在 pendingProps 中,之后会被应用到节点。
52
+ */
53
+ pendingProps: any;
54
+ /**
55
+ * 存储 FiberNode 的引用信息,与 React 的 Ref 有关。使用 ref 引用值
56
+ */
57
+ ref: any | null;
58
+ /**
59
+ * 指向该 FiberNode 的父节点
60
+ */
61
+ return: ReactFiberNode | null;
62
+ /**
63
+ * 指向右边第一个兄弟 Fiber 节点
64
+ */
65
+ sibling: ReactFiberNode | null;
66
+ /**
67
+ * FiberNode 对应的真实 DOM 节点
68
+ */
69
+ stateNode: HTMLElement;
70
+ /**
71
+ * 表示节点类型的标记,例如 FunctionComponent 、ClassComponent 等
72
+ */
73
+ tag: number;
74
+ /**
75
+ * 表示元素的类型, 对于 FunctionComponent,指函数本身,对于ClassComponent,指 class,对于 HostComponent,指 DOM 节点 tagName
76
+ */
77
+ type: string;
78
+ /**
79
+ * 用于存储组件的更新状态,比如新的状态、属性或者 context 的变化。通过 updateQueue ,React 可以跟踪组件的更新并在合适的时机执行更新。
80
+ */
81
+ updateQueue: any;
82
+ [key: string | symbol]: any;
83
+ }
84
+
85
+ export declare interface ReactProps {
86
+ $$typeof: symbol;
87
+ children: (boolean | ReactProps)[] | ReactProps;
88
+ key: string | null;
89
+ ref: any;
90
+ props: ReactProps;
91
+ type: string;
92
+ _owner: any;
93
+ [key: string | symbol]: any;
94
+ }
95
+
96
+ export declare interface ReactEvents {
97
+ [key: string | symbol]: any;
98
+ }
99
+
100
+ export declare interface ReactEventHandlers {
101
+ [key: string | symbol]: any;
102
+ }
103
+
104
+ export declare interface ReactInternalInstance {
105
+ [key: string | symbol]: any;
106
+ }
107
+
108
+ export declare interface ReactContainer {
109
+ [key: string | symbol]: any;
110
+ }
111
+
112
+ export declare interface ReactInstance {
113
+ reactFiber?: ReactFiberNode;
114
+ reactProps?: ReactProps;
115
+ reactEvents?: ReactEvents;
116
+ reactEventHandlers?: ReactEventHandlers;
117
+ reactInternalInstance?: ReactInternalInstance;
118
+ reactContainer?: ReactContainer;
119
+ }