qy-better-lib 0.0.1
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.
- package/.vscode/extensions.json +3 -0
- package/LICENSE +24 -0
- package/README.md +2 -0
- package/index.html +13 -0
- package/package.json +37 -0
- package/packages/core/DOCUMENTATION.md +2681 -0
- package/packages/core/LICENSE +24 -0
- package/packages/core/package.json +67 -0
- package/packages/core/types/index.d.ts +39 -0
- package/packages/core/types/json.d.ts +4 -0
- package/packages/core/types/vue.shim.d.ts +5 -0
- package/packages/core/vitest.config.ts +15 -0
- package/packages/core/yarn.lock +1233 -0
- package/packages/hooks/DOCUMENTATION.md +634 -0
- package/packages/hooks/LICENSE +24 -0
- package/packages/hooks/__tests__/use-chart/index.test.ts +287 -0
- package/packages/hooks/__tests__/use-emit/index.test.ts +248 -0
- package/packages/hooks/__tests__/use-fullscreen/index.test.ts +162 -0
- package/packages/hooks/__tests__/use-image/index.test.ts +230 -0
- package/packages/hooks/__tests__/use-layout-flow/index.test.ts +382 -0
- package/packages/hooks/__tests__/use-mqtt/index.test.ts +392 -0
- package/packages/hooks/__tests__/use-print/index.test.ts +378 -0
- package/packages/hooks/__tests__/use-watermark/index.test.ts +277 -0
- package/packages/hooks/__tests__/use-websocket/index.test.ts +402 -0
- package/packages/hooks/package.json +104 -0
- package/packages/hooks/types/index.d.ts +1 -0
- package/packages/hooks/types/json.d.ts +4 -0
- package/packages/hooks/types/vue.shim.d.ts +5 -0
- package/packages/hooks/yarn.lock +1832 -0
- package/packages/lowcode/core/package.json +60 -0
- package/packages/lowcode/core/types/index.d.ts +1 -0
- package/packages/lowcode/core/types/json.d.ts +4 -0
- package/packages/lowcode/core/types/vue.shim.d.ts +5 -0
- package/packages/lowcode/core/vite.dist.min.config.js +40 -0
- package/packages/lowcode/core/vite.dist.min.config.js.map +1 -0
- package/packages/lowcode/core/vite.lib.config.js +74 -0
- package/packages/lowcode/core/vite.lib.config.js.map +1 -0
- package/packages/lowcode/core/yarn.lock +1132 -0
- package/packages/lowcode/mobile/package.json +63 -0
- package/packages/lowcode/mobile/types/index.d.ts +1 -0
- package/packages/lowcode/mobile/types/json.d.ts +4 -0
- package/packages/lowcode/mobile/types/vue.shim.d.ts +77 -0
- package/packages/lowcode/mobile/vite.config.js +11 -0
- package/packages/lowcode/mobile/vite.config.js.map +1 -0
- package/packages/lowcode/mobile/vite.dist.min.config.js +71 -0
- package/packages/lowcode/mobile/vite.dist.min.config.js.map +1 -0
- package/packages/lowcode/mobile/vite.lib.config.js +137 -0
- package/packages/lowcode/mobile/vite.lib.config.js.map +1 -0
- package/packages/lowcode/mobile/yarn.lock +3888 -0
- package/packages/lowcode/pc/package.json +64 -0
- package/packages/lowcode/pc/vite.config.js +11 -0
- package/packages/lowcode/pc/vite.config.js.map +1 -0
- package/packages/lowcode/pc/vite.dist.min.config.js +71 -0
- package/packages/lowcode/pc/vite.dist.min.config.js.map +1 -0
- package/packages/lowcode/pc/vite.lib.config.js +137 -0
- package/packages/lowcode/pc/vite.lib.config.js.map +1 -0
- package/packages/lowcode/pc/yarn.lock +4026 -0
- package/packages/pc_components/DOCUMENTATION.md +566 -0
- package/packages/pc_components/LICENSE +24 -0
- package/packages/pc_components/lib/commDialog/index.d.ts +3 -0
- package/packages/pc_components/lib/commDialog/index.js +8 -0
- package/packages/pc_components/lib/commDialog/src/index.vue.d.ts +102 -0
- package/packages/pc_components/lib/commDialog/src/index.vue.js +43 -0
- package/packages/pc_components/lib/commDialog/src/index.vue2.js +4 -0
- package/packages/pc_components/lib/commDialog/src/props.d.ts +49 -0
- package/packages/pc_components/lib/commDialog/src/props.js +48 -0
- package/packages/pc_components/lib/commDialog/src/type.d.ts +39 -0
- package/packages/pc_components/lib/commForm/index.d.ts +3 -0
- package/packages/pc_components/lib/commForm/index.js +8 -0
- package/packages/pc_components/lib/commForm/src/index.vue.d.ts +95 -0
- package/packages/pc_components/lib/commForm/src/index.vue.js +50 -0
- package/packages/pc_components/lib/commForm/src/index.vue2.js +4 -0
- package/packages/pc_components/lib/commForm/src/props.d.ts +49 -0
- package/packages/pc_components/lib/commForm/src/props.js +47 -0
- package/packages/pc_components/lib/commForm/src/type.d.ts +8 -0
- package/packages/pc_components/lib/contextMenu/index.d.ts +5 -0
- package/packages/pc_components/lib/contextMenu/index.js +8 -0
- package/packages/pc_components/lib/contextMenu/src/index.vue.d.ts +91 -0
- package/packages/pc_components/lib/contextMenu/src/index.vue.js +153 -0
- package/packages/pc_components/lib/contextMenu/src/index.vue2.js +4 -0
- package/packages/pc_components/lib/contextMenu/src/props.d.ts +32 -0
- package/packages/pc_components/lib/contextMenu/src/props.js +27 -0
- package/packages/pc_components/lib/contextMenu/src/type.d.ts +11 -0
- package/packages/pc_components/lib/describeView/index.d.ts +2 -0
- package/packages/pc_components/lib/describeView/index.js +7 -0
- package/packages/pc_components/lib/describeView/src/index.vue.d.ts +61 -0
- package/packages/pc_components/lib/describeView/src/index.vue.js +84 -0
- package/packages/pc_components/lib/describeView/src/index.vue2.js +4 -0
- package/packages/pc_components/lib/describeView/src/props.d.ts +35 -0
- package/packages/pc_components/lib/describeView/src/props.js +33 -0
- package/packages/pc_components/lib/describeView/src/type.d.ts +10 -0
- package/packages/pc_components/lib/index.d.ts +18 -0
- package/packages/pc_components/lib/index.js +40 -0
- package/packages/pc_components/lib/simpleVirtualList/index.d.ts +2 -0
- package/packages/pc_components/lib/simpleVirtualList/index.js +7 -0
- package/packages/pc_components/lib/simpleVirtualList/src/index.vue.d.ts +28 -0
- package/packages/pc_components/lib/simpleVirtualList/src/index.vue.js +101 -0
- package/packages/pc_components/lib/simpleVirtualList/src/index.vue2.js +4 -0
- package/packages/pc_components/lib/simpleVirtualList/src/type.d.ts +12 -0
- package/packages/pc_components/lib/styles/comm-form.scss +4 -0
- package/packages/pc_components/lib/styles/context-menu.scss +47 -0
- package/packages/pc_components/lib/styles/describe-view.scss +25 -0
- package/packages/pc_components/lib/styles/index.scss +6 -0
- package/packages/pc_components/lib/styles/simple-virtual-list.scss +25 -0
- package/packages/pc_components/lib/styles/table-extend.scss +100 -0
- package/packages/pc_components/lib/styles/text-button.scss +71 -0
- package/packages/pc_components/lib/tableExtend/index.d.ts +3 -0
- package/packages/pc_components/lib/tableExtend/index.js +7 -0
- package/packages/pc_components/lib/tableExtend/props.d.ts +130 -0
- package/packages/pc_components/lib/tableExtend/props.js +128 -0
- package/packages/pc_components/lib/tableExtend/src/colVisible.vue.d.ts +3 -0
- package/packages/pc_components/lib/tableExtend/src/colVisible.vue.js +128 -0
- package/packages/pc_components/lib/tableExtend/src/colVisible.vue2.js +4 -0
- package/packages/pc_components/lib/tableExtend/src/index.vue.d.ts +186 -0
- package/packages/pc_components/lib/tableExtend/src/index.vue.js +234 -0
- package/packages/pc_components/lib/tableExtend/src/index.vue2.js +4 -0
- package/packages/pc_components/lib/tableExtend/src/valueCol.vue.d.ts +24 -0
- package/packages/pc_components/lib/tableExtend/src/valueCol.vue.js +29 -0
- package/packages/pc_components/lib/tableExtend/src/valueCol.vue2.js +4 -0
- package/packages/pc_components/lib/tableExtend/type.d.ts +165 -0
- package/packages/pc_components/lib/tableExtend/type.js +9 -0
- package/packages/pc_components/lib/tableExtend/useTable.d.ts +27 -0
- package/packages/pc_components/lib/tableExtend/useTable.js +115 -0
- package/packages/pc_components/lib/textButton/index.d.ts +2 -0
- package/packages/pc_components/lib/textButton/index.js +7 -0
- package/packages/pc_components/lib/textButton/src/index.vue.d.ts +42 -0
- package/packages/pc_components/lib/textButton/src/index.vue.js +44 -0
- package/packages/pc_components/lib/textButton/src/index.vue2.js +4 -0
- package/packages/pc_components/lib/textButton/src/props.d.ts +23 -0
- package/packages/pc_components/lib/textButton/src/props.js +30 -0
- package/packages/pc_components/lib/textButton/src/type.d.ts +30 -0
- package/packages/pc_components/lib/utils/index.d.ts +77 -0
- package/packages/pc_components/lib/utils/index.js +124 -0
- package/packages/pc_components/package.json +73 -0
- package/packages/pc_components/types/index.d.ts +9 -0
- package/packages/pc_components/types/json.d.ts +4 -0
- package/packages/pc_components/types/vue.shim.d.ts +5 -0
- package/packages/pc_components/yarn.lock +1641 -0
- package/postcss.config.js +19 -0
- package/public/vite.svg +1 -0
- package/src/App.vue +37 -0
- package/src/api/index.ts +42 -0
- package/src/assets/vue.svg +1 -0
- package/src/document/core.md +2680 -0
- package/src/document/hooks.md +632 -0
- package/src/document/pc_components.md +566 -0
- package/src/locales/en.json +1 -0
- package/src/locales/zh-cn.json +1 -0
- package/src/main.ts +13 -0
- package/src/plugins/element-plus.ts +16 -0
- package/src/plugins/i18n.ts +17 -0
- package/src/plugins/index.ts +13 -0
- package/src/plugins/store.ts +12 -0
- package/src/router/index.ts +20 -0
- package/src/shared/chartType.ts +279 -0
- package/src/shared/const.ts +160 -0
- package/src/shared/lib.ts +129 -0
- package/src/shared/request.ts +255 -0
- package/src/shared/type.ts +48 -0
- package/src/shared/utils/date.ts +83 -0
- package/src/shared/utils/utils.ts +85 -0
- package/src/store/index.ts +22 -0
- package/src/store/modules/app.ts +17 -0
- package/src/store/modules/shared.ts +21 -0
- package/src/styles/index.scss +169 -0
- package/src/types/app.d.ts +23 -0
- package/src/types/echarts.d.ts +5 -0
- package/src/types/index.d.ts +1 -0
- package/src/types/json.d.ts +4 -0
- package/src/types/scss.d.ts +9 -0
- package/src/types/vue.shim.d.ts +5 -0
- package/src/views/docs/docs-content.vue +379 -0
- package/src/views/docs/docs-header.vue +130 -0
- package/src/views/docs/docs-sidebar.vue +76 -0
- package/src/views/docs/hooks.ts +600 -0
- package/src/views/docs/index.vue +30 -0
- package/src/views/docs/styles/docs-content.scss +106 -0
- package/src/views/docs/styles/docs-header.scss +20 -0
- package/src/views/docs/styles/docs-sidebar.scss +182 -0
- package/src/views/docs/styles/index.scss +14 -0
- package/src/views/docs/type.ts +101 -0
- package/src/views/index.vue +34 -0
- package/tsconfig.json +37 -0
- package/tsconfig.node.json +8 -0
- package/vite.config.ts +54 -0
|
@@ -0,0 +1,2681 @@
|
|
|
1
|
+
# QY Better Lib Core - 工具函数文档
|
|
2
|
+
|
|
3
|
+
本文档详细介绍了 QY Better Lib Core 包中的工具函数,包括功能描述、参数说明、返回类型和使用示例。
|
|
4
|
+
|
|
5
|
+
## 工具函数分类
|
|
6
|
+
|
|
7
|
+
### 1. 类型检查工具 (is.ts)
|
|
8
|
+
|
|
9
|
+
#### is_array
|
|
10
|
+
- **功能**: 检查值是否为数组
|
|
11
|
+
- **参数**: `val: any` - 要检查的值
|
|
12
|
+
- **返回类型**: `boolean` - 是否为数组
|
|
13
|
+
- **示例**:
|
|
14
|
+
```typescript
|
|
15
|
+
import { is_array } from '@qy_better_lib/core';
|
|
16
|
+
|
|
17
|
+
is_array([]); // true
|
|
18
|
+
is_array({}); // false
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
#### is_object
|
|
22
|
+
- **功能**: 检查值是否为对象
|
|
23
|
+
- **参数**: `val: any` - 要检查的值
|
|
24
|
+
- **返回类型**: `boolean` - 是否为对象
|
|
25
|
+
- **示例**:
|
|
26
|
+
```typescript
|
|
27
|
+
import { is_object } from '@qy_better_lib/core';
|
|
28
|
+
|
|
29
|
+
is_object({}); // true
|
|
30
|
+
is_object([]); // false
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
#### is_string
|
|
34
|
+
- **功能**: 检查值是否为字符串
|
|
35
|
+
- **参数**: `val: any` - 要检查的值
|
|
36
|
+
- **返回类型**: `boolean` - 是否为字符串
|
|
37
|
+
- **示例**:
|
|
38
|
+
```typescript
|
|
39
|
+
import { is_string } from '@qy_better_lib/core';
|
|
40
|
+
|
|
41
|
+
is_string('hello'); // true
|
|
42
|
+
is_string(123); // false
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
#### is_number
|
|
46
|
+
- **功能**: 检查值是否为数字
|
|
47
|
+
- **参数**: `val: any` - 要检查的值
|
|
48
|
+
- **返回类型**: `boolean` - 是否为数字
|
|
49
|
+
- **示例**:
|
|
50
|
+
```typescript
|
|
51
|
+
import { is_number } from '@qy_better_lib/core';
|
|
52
|
+
|
|
53
|
+
is_number(123); // true
|
|
54
|
+
is_number('123'); // false
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### is_function
|
|
58
|
+
- **功能**: 检查值是否为函数
|
|
59
|
+
- **参数**: `val: any` - 要检查的值
|
|
60
|
+
- **返回类型**: `boolean` - 是否为函数
|
|
61
|
+
- **示例**:
|
|
62
|
+
```typescript
|
|
63
|
+
import { is_function } from '@qy_better_lib/core';
|
|
64
|
+
|
|
65
|
+
is_function(() => {}); // true
|
|
66
|
+
is_function({}); // false
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
#### is_boolean
|
|
70
|
+
- **功能**: 检查值是否为布尔值
|
|
71
|
+
- **参数**: `val: any` - 要检查的值
|
|
72
|
+
- **返回类型**: `boolean` - 是否为布尔值
|
|
73
|
+
- **示例**:
|
|
74
|
+
```typescript
|
|
75
|
+
import { is_boolean } from '@qy_better_lib/core';
|
|
76
|
+
|
|
77
|
+
is_boolean(true); // true
|
|
78
|
+
is_boolean('true'); // false
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### is_null
|
|
82
|
+
- **功能**: 检查值是否为 null
|
|
83
|
+
- **参数**: `val: any` - 要检查的值
|
|
84
|
+
- **返回类型**: `boolean` - 是否为 null
|
|
85
|
+
- **示例**:
|
|
86
|
+
```typescript
|
|
87
|
+
import { is_null } from '@qy_better_lib/core';
|
|
88
|
+
|
|
89
|
+
is_null(null); // true
|
|
90
|
+
is_null(undefined); // false
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
#### is_undefined
|
|
94
|
+
- **功能**: 检查值是否为 undefined
|
|
95
|
+
- **参数**: `val: any` - 要检查的值
|
|
96
|
+
- **返回类型**: `boolean` - 是否为 undefined
|
|
97
|
+
- **示例**:
|
|
98
|
+
```typescript
|
|
99
|
+
import { is_undefined } from '@qy_better_lib/core';
|
|
100
|
+
|
|
101
|
+
is_undefined(undefined); // true
|
|
102
|
+
is_undefined(null); // false
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
#### is_empty
|
|
106
|
+
- **功能**: 检查值是否为空
|
|
107
|
+
- **参数**: `val: any` - 要检查的值
|
|
108
|
+
- **返回类型**: `boolean` - 是否为空
|
|
109
|
+
- **示例**:
|
|
110
|
+
```typescript
|
|
111
|
+
import { is_empty } from '@qy_better_lib/core';
|
|
112
|
+
|
|
113
|
+
is_empty([]); // true
|
|
114
|
+
is_empty(''); // true
|
|
115
|
+
is_empty({}); // true
|
|
116
|
+
is_empty(null); // true
|
|
117
|
+
is_empty(undefined); // true
|
|
118
|
+
is_empty(0); // false
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
#### is_date
|
|
122
|
+
- **功能**: 检查值是否为日期对象
|
|
123
|
+
- **参数**: `val: any` - 要检查的值
|
|
124
|
+
- **返回类型**: `boolean` - 是否为日期对象
|
|
125
|
+
- **示例**:
|
|
126
|
+
```typescript
|
|
127
|
+
import { is_date } from '@qy_better_lib/core';
|
|
128
|
+
|
|
129
|
+
is_date(new Date()); // true
|
|
130
|
+
is_date('2023-01-01'); // false
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
#### is_reg_exp
|
|
134
|
+
- **功能**: 检查值是否为正则表达式
|
|
135
|
+
- **参数**: `val: any` - 要检查的值
|
|
136
|
+
- **返回类型**: `boolean` - 是否为正则表达式
|
|
137
|
+
- **示例**:
|
|
138
|
+
```typescript
|
|
139
|
+
import { is_reg_exp } from '@qy_better_lib/core';
|
|
140
|
+
|
|
141
|
+
is_reg_exp(/test/); // true
|
|
142
|
+
is_reg_exp('test'); // false
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### is_error
|
|
146
|
+
- **功能**: 检查值是否为错误对象
|
|
147
|
+
- **参数**: `val: any` - 要检查的值
|
|
148
|
+
- **返回类型**: `boolean` - 是否为错误对象
|
|
149
|
+
- **示例**:
|
|
150
|
+
```typescript
|
|
151
|
+
import { is_error } from '@qy_better_lib/core';
|
|
152
|
+
|
|
153
|
+
is_error(new Error('error')); // true
|
|
154
|
+
is_error('error'); // false
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
#### is_promise
|
|
158
|
+
- **功能**: 检查值是否为 Promise 对象
|
|
159
|
+
- **参数**: `val: any` - 要检查的值
|
|
160
|
+
- **返回类型**: `boolean` - 是否为 Promise 对象
|
|
161
|
+
- **示例**:
|
|
162
|
+
```typescript
|
|
163
|
+
import { is_promise } from '@qy_better_lib/core';
|
|
164
|
+
|
|
165
|
+
is_promise(new Promise(() => {})); // true
|
|
166
|
+
is_promise({}); // false
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
#### is_element
|
|
170
|
+
- **功能**: 检查值是否为 DOM 元素
|
|
171
|
+
- **参数**: `val: any` - 要检查的值
|
|
172
|
+
- **返回类型**: `boolean` - 是否为 DOM 元素
|
|
173
|
+
- **示例**:
|
|
174
|
+
```typescript
|
|
175
|
+
import { is_element } from '@qy_better_lib/core';
|
|
176
|
+
|
|
177
|
+
is_element(document.createElement('div')); // true
|
|
178
|
+
is_element({}); // false
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
#### is_plain_object
|
|
182
|
+
- **功能**: 是否为纯粹的对象(排除数组、null等)
|
|
183
|
+
- **参数**: `val: any` - 要检查的值
|
|
184
|
+
- **返回类型**: `boolean` - 是否为纯粹的对象
|
|
185
|
+
- **示例**:
|
|
186
|
+
```typescript
|
|
187
|
+
import { is_plain_object } from '@qy_better_lib/core';
|
|
188
|
+
|
|
189
|
+
is_plain_object({}); // true
|
|
190
|
+
is_plain_object([]); // false
|
|
191
|
+
is_plain_object(null); // false
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
#### is_valid_date
|
|
195
|
+
- **功能**: 是否为有效的日期
|
|
196
|
+
- **参数**: `val: any` - 要检查的值
|
|
197
|
+
- **返回类型**: `boolean` - 是否为有效的日期
|
|
198
|
+
- **示例**:
|
|
199
|
+
```typescript
|
|
200
|
+
import { is_valid_date } from '@qy_better_lib/core';
|
|
201
|
+
|
|
202
|
+
is_valid_date(new Date()); // true
|
|
203
|
+
is_valid_date(new Date('invalid')); // false
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
#### is_truthy
|
|
207
|
+
- **功能**: 是否为真值
|
|
208
|
+
- **参数**: `val: any` - 要检查的值
|
|
209
|
+
- **返回类型**: `boolean` - 是否为真值
|
|
210
|
+
- **示例**:
|
|
211
|
+
```typescript
|
|
212
|
+
import { is_truthy } from '@qy_better_lib/core';
|
|
213
|
+
|
|
214
|
+
is_truthy(1); // true
|
|
215
|
+
is_truthy('hello'); // true
|
|
216
|
+
is_truthy(false); // false
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
#### is_falsy
|
|
220
|
+
- **功能**: 是否为假值
|
|
221
|
+
- **参数**: `val: any` - 要检查的值
|
|
222
|
+
- **返回类型**: `boolean` - 是否为假值
|
|
223
|
+
- **示例**:
|
|
224
|
+
```typescript
|
|
225
|
+
import { is_falsy } from '@qy_better_lib/core';
|
|
226
|
+
|
|
227
|
+
is_falsy(false); // true
|
|
228
|
+
is_falsy(0); // true
|
|
229
|
+
is_falsy(''); // true
|
|
230
|
+
is_falsy('hello'); // false
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
#### is_string_number
|
|
234
|
+
- **功能**: 是否为字符串数值
|
|
235
|
+
- **参数**: `val: any` - 要检查的值
|
|
236
|
+
- **返回类型**: `boolean` - 是否为字符串数值
|
|
237
|
+
- **示例**:
|
|
238
|
+
```typescript
|
|
239
|
+
import { is_string_number } from '@qy_better_lib/core';
|
|
240
|
+
|
|
241
|
+
is_string_number('123'); // true
|
|
242
|
+
is_string_number('abc'); // false
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
#### is_mobile
|
|
246
|
+
- **功能**: 是否为手机号(国内手机号)
|
|
247
|
+
- **参数**: `str: string` - 要检查的字符串
|
|
248
|
+
- **返回类型**: `boolean` - 是否为手机号
|
|
249
|
+
- **示例**:
|
|
250
|
+
```typescript
|
|
251
|
+
import { is_mobile } from '@qy_better_lib/core';
|
|
252
|
+
|
|
253
|
+
is_mobile('13800138000'); // true
|
|
254
|
+
is_mobile('12345678901'); // false
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
#### is_email
|
|
258
|
+
- **功能**: 是否为邮箱地址
|
|
259
|
+
- **参数**: `str: string` - 要检查的字符串
|
|
260
|
+
- **返回类型**: `boolean` - 是否为邮箱地址
|
|
261
|
+
- **示例**:
|
|
262
|
+
```typescript
|
|
263
|
+
import { is_email } from '@qy_better_lib/core';
|
|
264
|
+
|
|
265
|
+
is_email('test@example.com'); // true
|
|
266
|
+
is_email('invalid'); // false
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
#### is_url
|
|
270
|
+
- **功能**: 是否为URL地址
|
|
271
|
+
- **参数**: `str: string` - 要检查的字符串
|
|
272
|
+
- **返回类型**: `boolean` - 是否为URL地址
|
|
273
|
+
- **示例**:
|
|
274
|
+
```typescript
|
|
275
|
+
import { is_url } from '@qy_better_lib/core';
|
|
276
|
+
|
|
277
|
+
is_url('https://example.com'); // true
|
|
278
|
+
is_url('invalid'); // false
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
#### is_hex_color
|
|
282
|
+
- **功能**: 是否为十六进制颜色值
|
|
283
|
+
- **参数**: `str: string` - 要检查的字符串
|
|
284
|
+
- **返回类型**: `boolean` - 是否为十六进制颜色值
|
|
285
|
+
- **示例**:
|
|
286
|
+
```typescript
|
|
287
|
+
import { is_hex_color } from '@qy_better_lib/core';
|
|
288
|
+
|
|
289
|
+
is_hex_color('#ff0000'); // true
|
|
290
|
+
is_hex_color('#fff'); // true
|
|
291
|
+
is_hex_color('red'); // false
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
#### is_ip
|
|
295
|
+
- **功能**: 是否为有效IP地址
|
|
296
|
+
- **参数**: `val: string` - 要检查的字符串
|
|
297
|
+
- **返回类型**: `boolean` - 是否为有效IP地址
|
|
298
|
+
- **示例**:
|
|
299
|
+
```typescript
|
|
300
|
+
import { is_ip } from '@qy_better_lib/core';
|
|
301
|
+
|
|
302
|
+
is_ip('192.168.1.1'); // true
|
|
303
|
+
is_ip('256.0.0.1'); // false
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
#### is_port
|
|
307
|
+
- **功能**: 是否为有效端口
|
|
308
|
+
- **参数**: `val: string` - 要检查的字符串
|
|
309
|
+
- **返回类型**: `boolean` - 是否为有效端口
|
|
310
|
+
- **示例**:
|
|
311
|
+
```typescript
|
|
312
|
+
import { is_port } from '@qy_better_lib/core';
|
|
313
|
+
|
|
314
|
+
is_port('8080'); // true
|
|
315
|
+
is_port('65536'); // false
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
#### is_client
|
|
319
|
+
- **功能**: 检查是否在客户端环境
|
|
320
|
+
- **返回类型**: `boolean` - 是否在客户端环境
|
|
321
|
+
- **示例**:
|
|
322
|
+
```typescript
|
|
323
|
+
import { is_client } from '@qy_better_lib/core';
|
|
324
|
+
|
|
325
|
+
if (is_client) {
|
|
326
|
+
// 客户端环境代码
|
|
327
|
+
}
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
#### is_server
|
|
331
|
+
- **功能**: 检查是否在服务端环境
|
|
332
|
+
- **返回类型**: `boolean` - 是否在服务端环境
|
|
333
|
+
- **示例**:
|
|
334
|
+
```typescript
|
|
335
|
+
import { is_server } from '@qy_better_lib/core';
|
|
336
|
+
|
|
337
|
+
if (is_server) {
|
|
338
|
+
// 服务端环境代码
|
|
339
|
+
}
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
### 2. 数字处理工具 (number.ts)
|
|
343
|
+
|
|
344
|
+
#### format_currency
|
|
345
|
+
- **功能**: 格式化货币
|
|
346
|
+
- **参数**:
|
|
347
|
+
- `value: number` - 要格式化的数值
|
|
348
|
+
- `currency: string` - 货币符号,默认 '¥'
|
|
349
|
+
- `decimals: number` - 小数位数,默认 2
|
|
350
|
+
- `decimalSeparator: string` - 小数分隔符,默认 '.'
|
|
351
|
+
- `thousandSeparator: string` - 千位分隔符,默认 ','
|
|
352
|
+
- **返回类型**: `string` - 格式化后的货币字符串
|
|
353
|
+
- **示例**:
|
|
354
|
+
```typescript
|
|
355
|
+
import { format_currency } from '@qy_better_lib/core';
|
|
356
|
+
|
|
357
|
+
format_currency(1234.56); // ¥1,234.56
|
|
358
|
+
format_currency(1234.56, '$'); // $1,234.56
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
#### format_number
|
|
362
|
+
- **功能**: 格式化数字
|
|
363
|
+
- **参数**:
|
|
364
|
+
- `value: number` - 要格式化的数值
|
|
365
|
+
- `decimals: number` - 小数位数,默认 0
|
|
366
|
+
- `decimalSeparator: string` - 小数分隔符,默认 '.'
|
|
367
|
+
- `thousandSeparator: string` - 千位分隔符,默认 ','
|
|
368
|
+
- **返回类型**: `string` - 格式化后的数字字符串
|
|
369
|
+
- **示例**:
|
|
370
|
+
```typescript
|
|
371
|
+
import { format_number } from '@qy_better_lib/core';
|
|
372
|
+
|
|
373
|
+
format_number(1234.56, 2); // 1,234.56
|
|
374
|
+
format_number(1234, 0); // 1,234
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
#### to_percentage
|
|
378
|
+
- **功能**: 将数字转换为百分比
|
|
379
|
+
- **参数**:
|
|
380
|
+
- `value: number` - 要转换的数值
|
|
381
|
+
- `decimals: number` - 小数位数,默认 2
|
|
382
|
+
- `multiply: boolean` - 是否乘以 100,默认 true
|
|
383
|
+
- **返回类型**: `string` - 格式化后的百分比字符串
|
|
384
|
+
- **示例**:
|
|
385
|
+
```typescript
|
|
386
|
+
import { to_percentage } from '@qy_better_lib/core';
|
|
387
|
+
|
|
388
|
+
to_percentage(0.1234); // 12.34%
|
|
389
|
+
to_percentage(12.34, 1, false); // 12.3%
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
#### clamp
|
|
393
|
+
- **功能**: 限制数字在指定范围内
|
|
394
|
+
- **参数**:
|
|
395
|
+
- `value: number` - 要限制的值
|
|
396
|
+
- `min: number` - 最小值
|
|
397
|
+
- `max: number` - 最大值
|
|
398
|
+
- **返回类型**: `number` - 限制后的值
|
|
399
|
+
- **示例**:
|
|
400
|
+
```typescript
|
|
401
|
+
import { clamp } from '@qy_better_lib/core';
|
|
402
|
+
|
|
403
|
+
clamp(5, 0, 10); // 5
|
|
404
|
+
clamp(-5, 0, 10); // 0
|
|
405
|
+
clamp(15, 0, 10); // 10
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
#### is_even
|
|
409
|
+
- **功能**: 检查数字是否为偶数
|
|
410
|
+
- **参数**: `value: number` - 要检查的数字
|
|
411
|
+
- **返回类型**: `boolean` - 是否为偶数
|
|
412
|
+
- **示例**:
|
|
413
|
+
```typescript
|
|
414
|
+
import { is_even } from '@qy_better_lib/core';
|
|
415
|
+
|
|
416
|
+
is_even(2); // true
|
|
417
|
+
is_even(3); // false
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
#### is_odd
|
|
421
|
+
- **功能**: 检查数字是否为奇数
|
|
422
|
+
- **参数**: `value: number` - 要检查的数字
|
|
423
|
+
- **返回类型**: `boolean` - 是否为奇数
|
|
424
|
+
- **示例**:
|
|
425
|
+
```typescript
|
|
426
|
+
import { is_odd } from '@qy_better_lib/core';
|
|
427
|
+
|
|
428
|
+
is_odd(3); // true
|
|
429
|
+
is_odd(2); // false
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
#### get_integer_part
|
|
433
|
+
- **功能**: 获取数字的整数部分
|
|
434
|
+
- **参数**: `value: number` - 要处理的数字
|
|
435
|
+
- **返回类型**: `number` - 整数部分
|
|
436
|
+
- **示例**:
|
|
437
|
+
```typescript
|
|
438
|
+
import { get_integer_part } from '@qy_better_lib/core';
|
|
439
|
+
|
|
440
|
+
get_integer_part(123.45); // 123
|
|
441
|
+
get_integer_part(-123.45); // -123
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
#### get_decimal_part
|
|
445
|
+
- **功能**: 获取数字的小数部分
|
|
446
|
+
- **参数**: `value: number` - 要处理的数字
|
|
447
|
+
- **返回类型**: `number` - 小数部分
|
|
448
|
+
- **示例**:
|
|
449
|
+
```typescript
|
|
450
|
+
import { get_decimal_part } from '@qy_better_lib/core';
|
|
451
|
+
|
|
452
|
+
get_decimal_part(123.45); // 0.45
|
|
453
|
+
get_decimal_part(-123.45); // 0.45
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
#### round
|
|
457
|
+
- **功能**: 四舍五入数字
|
|
458
|
+
- **参数**:
|
|
459
|
+
- `value: number` - 要四舍五入的数字
|
|
460
|
+
- `decimals: number` - 小数位数,默认 0
|
|
461
|
+
- **返回类型**: `number` - 四舍五入后的数字
|
|
462
|
+
- **示例**:
|
|
463
|
+
```typescript
|
|
464
|
+
import { round } from '@qy_better_lib/core';
|
|
465
|
+
|
|
466
|
+
round(123.45); // 123
|
|
467
|
+
round(123.45, 1); // 123.5
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
#### ceil
|
|
471
|
+
- **功能**: 向上取整数字
|
|
472
|
+
- **参数**: `value: number` - 要向上取整的数字
|
|
473
|
+
- **返回类型**: `number` - 向上取整后的数字
|
|
474
|
+
- **示例**:
|
|
475
|
+
```typescript
|
|
476
|
+
import { ceil } from '@qy_better_lib/core';
|
|
477
|
+
|
|
478
|
+
ceil(123.1); // 124
|
|
479
|
+
ceil(-123.9); // -123
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
#### floor
|
|
483
|
+
- **功能**: 向下取整数字
|
|
484
|
+
- **参数**: `value: number` - 要向下取整的数字
|
|
485
|
+
- **返回类型**: `number` - 向下取整后的数字
|
|
486
|
+
- **示例**:
|
|
487
|
+
```typescript
|
|
488
|
+
import { floor } from '@qy_better_lib/core';
|
|
489
|
+
|
|
490
|
+
floor(123.9); // 123
|
|
491
|
+
floor(-123.1); // -124
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
#### thousand_separator
|
|
495
|
+
- **功能**: 数字转成千分位
|
|
496
|
+
- **参数**: `num: any` - 数字或数字字符串
|
|
497
|
+
- **返回类型**: `string` - 千分位格式的字符串
|
|
498
|
+
- **示例**:
|
|
499
|
+
```typescript
|
|
500
|
+
import { thousand_separator } from '@qy_better_lib/core';
|
|
501
|
+
|
|
502
|
+
thousand_separator(1234567); // "1,234,567"
|
|
503
|
+
thousand_separator('1234567'); // "1,234,567"
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
#### to_number
|
|
507
|
+
- **功能**: 转换成数值
|
|
508
|
+
- **参数**: `num: any` - 源
|
|
509
|
+
- **返回类型**: `number` - 转换后的数值
|
|
510
|
+
- **示例**:
|
|
511
|
+
```typescript
|
|
512
|
+
import { to_number } from '@qy_better_lib/core';
|
|
513
|
+
|
|
514
|
+
to_number(123); // 123
|
|
515
|
+
to_number('123'); // 123
|
|
516
|
+
to_number('invalid'); // NaN
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
#### to_fixed
|
|
520
|
+
- **功能**: 保留小数点
|
|
521
|
+
- **参数**:
|
|
522
|
+
- `num: any` - 数字或数字字符串
|
|
523
|
+
- `fixed: number` - 小数位数
|
|
524
|
+
- **返回类型**: `number` - 保留小数点后的数值
|
|
525
|
+
- **示例**:
|
|
526
|
+
```typescript
|
|
527
|
+
import { to_fixed } from '@qy_better_lib/core';
|
|
528
|
+
|
|
529
|
+
to_fixed(123.456, 2); // 123.46
|
|
530
|
+
to_fixed('123.456', 1); // 123.5
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
#### calculate_percentage
|
|
534
|
+
- **功能**: 计算百分比
|
|
535
|
+
- **参数**:
|
|
536
|
+
- `current: number` - 当前值
|
|
537
|
+
- `total: number` - 总值
|
|
538
|
+
- `fixed: number` - 小数位数,默认 2
|
|
539
|
+
- **返回类型**: `number` - 百分比值
|
|
540
|
+
- **示例**:
|
|
541
|
+
```typescript
|
|
542
|
+
import { calculate_percentage } from '@qy_better_lib/core';
|
|
543
|
+
|
|
544
|
+
calculate_percentage(50, 200); // 25
|
|
545
|
+
calculate_percentage(25, 100, 1); // 25
|
|
546
|
+
```
|
|
547
|
+
|
|
548
|
+
### 3. 日期时间工具 (date.ts)
|
|
549
|
+
|
|
550
|
+
> 注意:日期时间工具现在是可选依赖,需要单独导入
|
|
551
|
+
|
|
552
|
+
#### format_date
|
|
553
|
+
- **功能**: 格式化日期时间
|
|
554
|
+
- **参数**:
|
|
555
|
+
- `date: Date | string | number` - 日期时间对象或字符串
|
|
556
|
+
- `format: string` - 格式化模板,默认 "YYYY-MM-DD HH:mm:ss"
|
|
557
|
+
- **返回类型**: `string` - 格式化后的日期时间字符串
|
|
558
|
+
- **示例**:
|
|
559
|
+
```typescript
|
|
560
|
+
import { format_date } from '@qy_better_lib/core/date';
|
|
561
|
+
|
|
562
|
+
format_date(new Date()); // 2023-12-31 23:59:59
|
|
563
|
+
format_date(new Date(), 'YYYY-MM-DD'); // 2023-12-31
|
|
564
|
+
```
|
|
565
|
+
|
|
566
|
+
#### get_date_time_range
|
|
567
|
+
- **功能**: 根据当前时间获取指定类型的时间范围
|
|
568
|
+
- **参数**:
|
|
569
|
+
- `type: DateRangeType` - 范围类型 ('d' | 'w' | 'M' | 'Q' | 'y' | 'h' | 'm' | 's')
|
|
570
|
+
- `increment: number` - 增量,默认 -1
|
|
571
|
+
- `format: string` - 日期时间显示格式,默认 "YYYY-MM-DD HH:mm:ss"
|
|
572
|
+
- `span: boolean` - 是否跨越范围类型,默认 false
|
|
573
|
+
- **返回类型**: `[string, string]` - [开始日期时间, 截止日期时间]
|
|
574
|
+
- **示例**:
|
|
575
|
+
```typescript
|
|
576
|
+
import { get_date_time_range } from '@qy_better_lib/core/date';
|
|
577
|
+
|
|
578
|
+
// 获取昨天的时间范围
|
|
579
|
+
const [start, end] = get_date_time_range('d', -1);
|
|
580
|
+
console.log(start, end); // 2023-12-30 00:00:00 2023-12-31 23:59:59
|
|
581
|
+
```
|
|
582
|
+
|
|
583
|
+
#### get_date_diff
|
|
584
|
+
- **功能**: 计算两个日期之间的时间差
|
|
585
|
+
- **参数**:
|
|
586
|
+
- `date1: Date | string | number` - 第一个日期
|
|
587
|
+
- `date2: Date | string | number` - 第二个日期
|
|
588
|
+
- `unit: string` - 时间单位,默认 "day"
|
|
589
|
+
- **返回类型**: `number` - 时间差
|
|
590
|
+
- **示例**:
|
|
591
|
+
```typescript
|
|
592
|
+
import { get_date_diff } from '@qy_better_lib/core/date';
|
|
593
|
+
|
|
594
|
+
const date1 = '2023-12-31';
|
|
595
|
+
const date2 = '2023-12-25';
|
|
596
|
+
get_date_diff(date1, date2); // 6
|
|
597
|
+
```
|
|
598
|
+
|
|
599
|
+
#### add_date
|
|
600
|
+
- **功能**: 添加时间
|
|
601
|
+
- **参数**:
|
|
602
|
+
- `date: Date | string | number` - 基础日期
|
|
603
|
+
- `value: number` - 增加的值
|
|
604
|
+
- `unit: string` - 时间单位
|
|
605
|
+
- `format: string` - 格式化模板,默认不格式化
|
|
606
|
+
- **返回类型**: `Date | string` - 增加后的日期或格式化后的字符串
|
|
607
|
+
- **示例**:
|
|
608
|
+
```typescript
|
|
609
|
+
import { add_date } from '@qy_better_lib/core/date';
|
|
610
|
+
|
|
611
|
+
// 添加7天
|
|
612
|
+
add_date(new Date(), 7, 'day'); // 7天后的日期对象
|
|
613
|
+
add_date(new Date(), 7, 'day', 'YYYY-MM-DD'); // 格式化后的日期字符串
|
|
614
|
+
```
|
|
615
|
+
|
|
616
|
+
#### subtract_date
|
|
617
|
+
- **功能**: 减去时间
|
|
618
|
+
- **参数**:
|
|
619
|
+
- `date: Date | string | number` - 基础日期
|
|
620
|
+
- `value: number` - 减少的值
|
|
621
|
+
- `unit: string` - 时间单位
|
|
622
|
+
- `format: string` - 格式化模板,默认不格式化
|
|
623
|
+
- **返回类型**: `Date | string` - 减少后的日期或格式化后的字符串
|
|
624
|
+
- **示例**:
|
|
625
|
+
```typescript
|
|
626
|
+
import { subtract_date } from '@qy_better_lib/core/date';
|
|
627
|
+
|
|
628
|
+
// 减去7天
|
|
629
|
+
subtract_date(new Date(), 7, 'day'); // 7天前的日期对象
|
|
630
|
+
subtract_date(new Date(), 7, 'day', 'YYYY-MM-DD'); // 格式化后的日期字符串
|
|
631
|
+
```
|
|
632
|
+
|
|
633
|
+
#### is_weekday
|
|
634
|
+
- **功能**: 判断是否为工作日
|
|
635
|
+
- **参数**: `date: Date | string | number` - 日期
|
|
636
|
+
- **返回类型**: `boolean` - 是否为工作日
|
|
637
|
+
- **示例**:
|
|
638
|
+
```typescript
|
|
639
|
+
import { is_weekday } from '@qy_better_lib/core/date';
|
|
640
|
+
|
|
641
|
+
is_weekday('2023-12-31'); // true (周日)
|
|
642
|
+
is_weekday('2024-01-01'); // false (周一)
|
|
643
|
+
```
|
|
644
|
+
|
|
645
|
+
#### is_weekend
|
|
646
|
+
- **功能**: 判断是否为周末
|
|
647
|
+
- **参数**: `date: Date | string | number` - 日期
|
|
648
|
+
- **返回类型**: `boolean` - 是否为周末
|
|
649
|
+
- **示例**:
|
|
650
|
+
```typescript
|
|
651
|
+
import { is_weekend } from '@qy_better_lib/core/date';
|
|
652
|
+
|
|
653
|
+
is_weekend('2023-12-30'); // true (周六)
|
|
654
|
+
is_weekend('2023-12-29'); // false (周五)
|
|
655
|
+
```
|
|
656
|
+
|
|
657
|
+
#### get_relative_time
|
|
658
|
+
- **功能**: 获取相对时间描述
|
|
659
|
+
- **参数**: `date: Date | string | number` - 日期
|
|
660
|
+
- **返回类型**: `string` - 相对时间描述
|
|
661
|
+
- **示例**:
|
|
662
|
+
```typescript
|
|
663
|
+
import { get_relative_time } from '@qy_better_lib/core/date';
|
|
664
|
+
|
|
665
|
+
// 假设当前时间是2023-12-31
|
|
666
|
+
get_relative_time('2023-12-31 23:50:00'); // 刚刚
|
|
667
|
+
get_relative_time('2023-12-31 22:00:00'); // 1小时前
|
|
668
|
+
```
|
|
669
|
+
|
|
670
|
+
#### format_timestamp_diff
|
|
671
|
+
- **功能**: 将时间戳差值转换成可读格式
|
|
672
|
+
- **参数**:
|
|
673
|
+
- `timestamp_diff: number` - 时间戳差值(毫秒)
|
|
674
|
+
- `format: TimeDiffFormatType` - 格式类型,默认 'zh'
|
|
675
|
+
- **返回类型**: `string` - 格式化后的时间差字符串
|
|
676
|
+
- **示例**:
|
|
677
|
+
```typescript
|
|
678
|
+
import { format_timestamp_diff } from '@qy_better_lib/core/date';
|
|
679
|
+
|
|
680
|
+
// 1天2小时3分钟4秒
|
|
681
|
+
const diff = 1 * 24 * 60 * 60 * 1000 + 2 * 60 * 60 * 1000 + 3 * 60 * 1000 + 4 * 1000;
|
|
682
|
+
format_timestamp_diff(diff); // 1天2时3分4秒
|
|
683
|
+
format_timestamp_diff(diff, 'en'); // 1D2H3M4S
|
|
684
|
+
```
|
|
685
|
+
|
|
686
|
+
### 4. 对象操作工具 (object.ts)
|
|
687
|
+
|
|
688
|
+
#### deep_clone
|
|
689
|
+
- **功能**: 深拷贝对象
|
|
690
|
+
- **参数**: `obj: any` - 要拷贝的对象
|
|
691
|
+
- **返回类型**: `any` - 拷贝后的新对象
|
|
692
|
+
- **示例**:
|
|
693
|
+
```typescript
|
|
694
|
+
import { deep_clone } from '@qy_better_lib/core';
|
|
695
|
+
|
|
696
|
+
const obj = { a: 1, b: { c: 2 } };
|
|
697
|
+
const cloned = deep_clone(obj);
|
|
698
|
+
cloned.b.c = 3;
|
|
699
|
+
console.log(obj.b.c); // 2
|
|
700
|
+
```
|
|
701
|
+
|
|
702
|
+
#### deep_assign
|
|
703
|
+
- **功能**: 深度合并多个对象
|
|
704
|
+
- **参数**: `...args: any[]` - 要合并的对象列表
|
|
705
|
+
- **返回类型**: `any` - 合并后的对象
|
|
706
|
+
- **示例**:
|
|
707
|
+
```typescript
|
|
708
|
+
import { deep_assign } from '@qy_better_lib/core';
|
|
709
|
+
|
|
710
|
+
const obj1 = { a: 1, b: { c: 2 } };
|
|
711
|
+
const obj2 = { b: { d: 3 }, e: 4 };
|
|
712
|
+
const merged = deep_assign(obj1, obj2);
|
|
713
|
+
console.log(merged); // { a: 1, b: { c: 2, d: 3 }, e: 4 }
|
|
714
|
+
```
|
|
715
|
+
|
|
716
|
+
#### deep_equal
|
|
717
|
+
- **功能**: 深度比较两个对象值是否一致
|
|
718
|
+
- **参数**:
|
|
719
|
+
- `a: any` - 第一个对象
|
|
720
|
+
- `b: any` - 第二个对象
|
|
721
|
+
- **返回类型**: `boolean` - 是否相等
|
|
722
|
+
- **示例**:
|
|
723
|
+
```typescript
|
|
724
|
+
import { deep_equal } from '@qy_better_lib/core';
|
|
725
|
+
|
|
726
|
+
const obj1 = { a: 1, b: { c: 2 } };
|
|
727
|
+
const obj2 = { a: 1, b: { c: 2 } };
|
|
728
|
+
const obj3 = { a: 1, b: { c: 3 } };
|
|
729
|
+
|
|
730
|
+
deep_equal(obj1, obj2); // true
|
|
731
|
+
deep_equal(obj1, obj3); // false
|
|
732
|
+
```
|
|
733
|
+
|
|
734
|
+
#### is_empty_object
|
|
735
|
+
- **功能**: 检查对象是否为空
|
|
736
|
+
- **参数**: `obj: any` - 要检查的对象
|
|
737
|
+
- **返回类型**: `boolean` - 是否为空对象
|
|
738
|
+
- **示例**:
|
|
739
|
+
```typescript
|
|
740
|
+
import { is_empty_object } from '@qy_better_lib/core';
|
|
741
|
+
|
|
742
|
+
is_empty_object({}); // true
|
|
743
|
+
is_empty_object({ a: 1 }); // false
|
|
744
|
+
```
|
|
745
|
+
|
|
746
|
+
#### pick
|
|
747
|
+
- **功能**: 从对象中提取指定属性
|
|
748
|
+
- **参数**:
|
|
749
|
+
- `obj: any` - 源对象
|
|
750
|
+
- `keys: string[]` - 要提取的属性名数组
|
|
751
|
+
- **返回类型**: `Record<string, any>` - 提取后的新对象
|
|
752
|
+
- **示例**:
|
|
753
|
+
```typescript
|
|
754
|
+
import { pick } from '@qy_better_lib/core';
|
|
755
|
+
|
|
756
|
+
const obj = { a: 1, b: 2, c: 3 };
|
|
757
|
+
const picked = pick(obj, ['a', 'c']);
|
|
758
|
+
console.log(picked); // { a: 1, c: 3 }
|
|
759
|
+
```
|
|
760
|
+
|
|
761
|
+
#### omit
|
|
762
|
+
- **功能**: 从对象中排除指定属性
|
|
763
|
+
- **参数**:
|
|
764
|
+
- `obj: any` - 源对象
|
|
765
|
+
- `keys: string[]` - 要排除的属性名数组
|
|
766
|
+
- **返回类型**: `Record<string, any>` - 排除后的新对象
|
|
767
|
+
- **示例**:
|
|
768
|
+
```typescript
|
|
769
|
+
import { omit } from '@qy_better_lib/core';
|
|
770
|
+
|
|
771
|
+
const obj = { a: 1, b: 2, c: 3 };
|
|
772
|
+
const omitted = omit(obj, ['b']);
|
|
773
|
+
console.log(omitted); // { a: 1, c: 3 }
|
|
774
|
+
```
|
|
775
|
+
|
|
776
|
+
#### flatten_object
|
|
777
|
+
- **功能**: 将嵌套对象扁平化为一维对象
|
|
778
|
+
- **参数**:
|
|
779
|
+
- `obj: any` - 源对象
|
|
780
|
+
- `prefix: string` - 前缀,默认 ''
|
|
781
|
+
- **返回类型**: `Record<string, any>` - 扁平化后的对象
|
|
782
|
+
- **示例**:
|
|
783
|
+
```typescript
|
|
784
|
+
import { flatten_object } from '@qy_better_lib/core';
|
|
785
|
+
|
|
786
|
+
const obj = { a: 1, b: { c: 2, d: { e: 3 } } };
|
|
787
|
+
const flattened = flatten_object(obj);
|
|
788
|
+
console.log(flattened); // { a: 1, 'b.c': 2, 'b.d.e': 3 }
|
|
789
|
+
```
|
|
790
|
+
|
|
791
|
+
#### unflatten_object
|
|
792
|
+
- **功能**: 将扁平对象转换为嵌套对象
|
|
793
|
+
- **参数**: `obj: any` - 扁平对象
|
|
794
|
+
- **返回类型**: `Record<string, any>` - 嵌套对象
|
|
795
|
+
- **示例**:
|
|
796
|
+
```typescript
|
|
797
|
+
import { unflatten_object } from '@qy_better_lib/core';
|
|
798
|
+
|
|
799
|
+
const obj = { a: 1, 'b.c': 2, 'b.d.e': 3 };
|
|
800
|
+
const unflattened = unflatten_object(obj);
|
|
801
|
+
console.log(unflattened); // { a: 1, b: { c: 2, d: { e: 3 } } }
|
|
802
|
+
```
|
|
803
|
+
|
|
804
|
+
#### get_object_value
|
|
805
|
+
- **功能**: 根据路径获取对象值
|
|
806
|
+
- **参数**:
|
|
807
|
+
- `obj: any` - 源对象
|
|
808
|
+
- `path: string` - 路径字符串,如 "a.b.c"
|
|
809
|
+
- `defaultValue: any` - 默认值,默认 undefined
|
|
810
|
+
- **返回类型**: `any` - 获取的值或默认值
|
|
811
|
+
- **示例**:
|
|
812
|
+
```typescript
|
|
813
|
+
import { get_object_value } from '@qy_better_lib/core';
|
|
814
|
+
|
|
815
|
+
const obj = { a: 1, b: { c: 2, d: { e: 3 } } };
|
|
816
|
+
get_object_value(obj, 'a'); // 1
|
|
817
|
+
get_object_value(obj, 'b.c'); // 2
|
|
818
|
+
get_object_value(obj, 'b.d.e'); // 3
|
|
819
|
+
get_object_value(obj, 'b.f', 'default'); // 'default'
|
|
820
|
+
```
|
|
821
|
+
|
|
822
|
+
#### set_object_value
|
|
823
|
+
- **功能**: 根据路径设置对象值
|
|
824
|
+
- **参数**:
|
|
825
|
+
- `obj: any` - 源对象
|
|
826
|
+
- `path: string` - 路径字符串,如 "a.b.c"
|
|
827
|
+
- `value: any` - 要设置的值
|
|
828
|
+
- **返回类型**: `Record<string, any>` - 修改后的对象
|
|
829
|
+
- **示例**:
|
|
830
|
+
```typescript
|
|
831
|
+
import { set_object_value } from '@qy_better_lib/core';
|
|
832
|
+
|
|
833
|
+
const obj = { a: 1, b: { c: 2 } };
|
|
834
|
+
set_object_value(obj, 'b.d.e', 3);
|
|
835
|
+
console.log(obj); // { a: 1, b: { c: 2, d: { e: 3 } } }
|
|
836
|
+
```
|
|
837
|
+
|
|
838
|
+
#### merge_objects
|
|
839
|
+
- **功能**: 浅合并多个对象
|
|
840
|
+
- **参数**: `...args: any[]` - 要合并的对象列表
|
|
841
|
+
- **返回类型**: `Record<string, any>` - 合并后的对象
|
|
842
|
+
- **示例**:
|
|
843
|
+
```typescript
|
|
844
|
+
import { merge_objects } from '@qy_better_lib/core';
|
|
845
|
+
|
|
846
|
+
const obj1 = { a: 1, b: 2 };
|
|
847
|
+
const obj2 = { b: 3, c: 4 };
|
|
848
|
+
const merged = merge_objects(obj1, obj2);
|
|
849
|
+
console.log(merged); // { a: 1, b: 3, c: 4 }
|
|
850
|
+
```
|
|
851
|
+
|
|
852
|
+
#### diff_objects
|
|
853
|
+
- **功能**: 比较两个对象的差异
|
|
854
|
+
- **参数**:
|
|
855
|
+
- `obj1: any` - 第一个对象
|
|
856
|
+
- `obj2: any` - 第二个对象
|
|
857
|
+
- **返回类型**: `Record<string, any>` - 差异对象
|
|
858
|
+
- **示例**:
|
|
859
|
+
```typescript
|
|
860
|
+
import { diff_objects } from '@qy_better_lib/core';
|
|
861
|
+
|
|
862
|
+
const obj1 = { a: 1, b: 2, c: 3 };
|
|
863
|
+
const obj2 = { a: 1, b: 4, d: 5 };
|
|
864
|
+
const diff = diff_objects(obj1, obj2);
|
|
865
|
+
console.log(diff); // { b: { oldValue: 2, newValue: 4 }, c: { oldValue: 3, newValue: undefined }, d: { oldValue: undefined, newValue: 5 } }
|
|
866
|
+
```
|
|
867
|
+
|
|
868
|
+
### 5. 随机值生成工具 (random.ts)
|
|
869
|
+
|
|
870
|
+
#### random_id
|
|
871
|
+
- **功能**: 生成[0-10000]的随机数,针对数据小量使用
|
|
872
|
+
- **返回类型**: `number` - 随机数
|
|
873
|
+
- **示例**:
|
|
874
|
+
```typescript
|
|
875
|
+
import { random_id } from '@qy_better_lib/core';
|
|
876
|
+
|
|
877
|
+
random_id(); // 生成0-10000之间的随机数
|
|
878
|
+
```
|
|
879
|
+
|
|
880
|
+
#### random_int
|
|
881
|
+
- **功能**: 生成[0-max]的随机数
|
|
882
|
+
- **参数**: `max: number` - 最大值
|
|
883
|
+
- **返回类型**: `number` - 随机数
|
|
884
|
+
- **示例**:
|
|
885
|
+
```typescript
|
|
886
|
+
import { random_int } from '@qy_better_lib/core';
|
|
887
|
+
|
|
888
|
+
random_int(10); // 生成0-10之间的随机整数
|
|
889
|
+
```
|
|
890
|
+
|
|
891
|
+
#### random_int_range
|
|
892
|
+
- **功能**: 生成指定范围的随机整数
|
|
893
|
+
- **参数**:
|
|
894
|
+
- `min: number` - 最小值
|
|
895
|
+
- `max: number` - 最大值
|
|
896
|
+
- **返回类型**: `number` - 随机整数
|
|
897
|
+
- **示例**:
|
|
898
|
+
```typescript
|
|
899
|
+
import { random_int_range } from '@qy_better_lib/core';
|
|
900
|
+
|
|
901
|
+
random_int_range(1, 10); // 生成1-10之间的随机整数
|
|
902
|
+
```
|
|
903
|
+
|
|
904
|
+
#### random_float
|
|
905
|
+
- **功能**: 生成随机浮点数
|
|
906
|
+
- **参数**:
|
|
907
|
+
- `min: number` - 最小值
|
|
908
|
+
- `max: number` - 最大值
|
|
909
|
+
- `decimalPlaces: number` - 小数位数,默认 2
|
|
910
|
+
- **返回类型**: `number` - 随机浮点数
|
|
911
|
+
- **示例**:
|
|
912
|
+
```typescript
|
|
913
|
+
import { random_float } from '@qy_better_lib/core';
|
|
914
|
+
|
|
915
|
+
random_float(1, 10, 2); // 生成1-10之间的随机小数,保留2位小数
|
|
916
|
+
```
|
|
917
|
+
|
|
918
|
+
#### guid
|
|
919
|
+
- **功能**: 生成GUID/UUID
|
|
920
|
+
- **返回类型**: `string` - GUID/UUID字符串
|
|
921
|
+
- **示例**:
|
|
922
|
+
```typescript
|
|
923
|
+
import { guid } from '@qy_better_lib/core';
|
|
924
|
+
|
|
925
|
+
guid(); // 生成一个随机GUID/UUID
|
|
926
|
+
```
|
|
927
|
+
|
|
928
|
+
#### random_string
|
|
929
|
+
- **功能**: 生成随机字符串
|
|
930
|
+
- **返回类型**: `string` - 随机字符串
|
|
931
|
+
- **示例**:
|
|
932
|
+
```typescript
|
|
933
|
+
import { random_string } from '@qy_better_lib/core';
|
|
934
|
+
|
|
935
|
+
random_string(); // 生成一个随机字符串
|
|
936
|
+
```
|
|
937
|
+
|
|
938
|
+
#### random_len_string
|
|
939
|
+
- **功能**: 生成指定长度的随机字符串
|
|
940
|
+
- **参数**:
|
|
941
|
+
- `length: number` - 字符串长度
|
|
942
|
+
- `chars: string` - 可选字符集,默认包含大小写字母和数字
|
|
943
|
+
- **返回类型**: `string` - 随机字符串
|
|
944
|
+
- **示例**:
|
|
945
|
+
```typescript
|
|
946
|
+
import { random_len_string } from '@qy_better_lib/core';
|
|
947
|
+
|
|
948
|
+
random_len_string(10); // 生成10位随机字符串
|
|
949
|
+
random_len_string(8, 'ABCDEFG'); // 生成8位只包含指定字符的随机字符串
|
|
950
|
+
```
|
|
951
|
+
|
|
952
|
+
#### random_boolean
|
|
953
|
+
- **功能**: 生成随机布尔值
|
|
954
|
+
- **返回类型**: `boolean` - 随机布尔值
|
|
955
|
+
- **示例**:
|
|
956
|
+
```typescript
|
|
957
|
+
import { random_boolean } from '@qy_better_lib/core';
|
|
958
|
+
|
|
959
|
+
random_boolean(); // 50%概率为true
|
|
960
|
+
```
|
|
961
|
+
|
|
962
|
+
#### random_element
|
|
963
|
+
- **功能**: 从数组中随机选择一个元素
|
|
964
|
+
- **参数**: `array: T[]` - 源数组
|
|
965
|
+
- **返回类型**: `T | undefined` - 随机选中的元素
|
|
966
|
+
- **示例**:
|
|
967
|
+
```typescript
|
|
968
|
+
import { random_element } from '@qy_better_lib/core';
|
|
969
|
+
|
|
970
|
+
const array = ['a', 'b', 'c', 'd'];
|
|
971
|
+
random_element(array); // 随机返回数组中的一个元素
|
|
972
|
+
```
|
|
973
|
+
|
|
974
|
+
#### random_elements
|
|
975
|
+
- **功能**: 从数组中随机选择多个元素
|
|
976
|
+
- **参数**:
|
|
977
|
+
- `array: T[]` - 源数组
|
|
978
|
+
- `count: number` - 选择数量
|
|
979
|
+
- **返回类型**: `T[]` - 随机选中的元素数组
|
|
980
|
+
- **示例**:
|
|
981
|
+
```typescript
|
|
982
|
+
import { random_elements } from '@qy_better_lib/core';
|
|
983
|
+
|
|
984
|
+
const array = ['a', 'b', 'c', 'd'];
|
|
985
|
+
random_elements(array, 2); // 随机返回数组中的2个元素
|
|
986
|
+
```
|
|
987
|
+
|
|
988
|
+
#### random_color
|
|
989
|
+
- **功能**: 生成随机颜色
|
|
990
|
+
- **返回类型**: `string` - 随机颜色的十六进制值
|
|
991
|
+
- **示例**:
|
|
992
|
+
```typescript
|
|
993
|
+
import { random_color } from '@qy_better_lib/core';
|
|
994
|
+
|
|
995
|
+
random_color(); // 生成一个随机十六进制颜色
|
|
996
|
+
```
|
|
997
|
+
|
|
998
|
+
#### random_number_string
|
|
999
|
+
- **功能**: 生成指定长度的随机数字字符串
|
|
1000
|
+
- **参数**: `length: number` - 字符串长度
|
|
1001
|
+
- **返回类型**: `string` - 随机数字字符串
|
|
1002
|
+
- **示例**:
|
|
1003
|
+
```typescript
|
|
1004
|
+
import { random_number_string } from '@qy_better_lib/core';
|
|
1005
|
+
|
|
1006
|
+
random_number_string(6); // 生成6位随机数字字符串
|
|
1007
|
+
```
|
|
1008
|
+
|
|
1009
|
+
#### random_password
|
|
1010
|
+
- **功能**: 生成随机密码
|
|
1011
|
+
- **参数**: `length: number` - 密码长度,默认 8
|
|
1012
|
+
- **返回类型**: `string` - 随机密码
|
|
1013
|
+
- **示例**:
|
|
1014
|
+
```typescript
|
|
1015
|
+
import { random_password } from '@qy_better_lib/core';
|
|
1016
|
+
|
|
1017
|
+
random_password(); // 生成8位随机密码
|
|
1018
|
+
random_password(12); // 生成12位随机密码
|
|
1019
|
+
```
|
|
1020
|
+
|
|
1021
|
+
#### random_phone
|
|
1022
|
+
- **功能**: 生成随机手机号
|
|
1023
|
+
- **返回类型**: `string` - 随机手机号
|
|
1024
|
+
- **示例**:
|
|
1025
|
+
```typescript
|
|
1026
|
+
import { random_phone } from '@qy_better_lib/core';
|
|
1027
|
+
|
|
1028
|
+
random_phone(); // 生成一个随机手机号
|
|
1029
|
+
```
|
|
1030
|
+
|
|
1031
|
+
#### random_email
|
|
1032
|
+
- **功能**: 生成随机邮箱
|
|
1033
|
+
- **返回类型**: `string` - 随机邮箱
|
|
1034
|
+
- **示例**:
|
|
1035
|
+
```typescript
|
|
1036
|
+
import { random_email } from '@qy_better_lib/core';
|
|
1037
|
+
|
|
1038
|
+
random_email(); // 生成一个随机邮箱
|
|
1039
|
+
```
|
|
1040
|
+
|
|
1041
|
+
#### random_url
|
|
1042
|
+
- **功能**: 生成随机URL
|
|
1043
|
+
- **返回类型**: `string` - 随机URL
|
|
1044
|
+
- **示例**:
|
|
1045
|
+
```typescript
|
|
1046
|
+
import { random_url } from '@qy_better_lib/core';
|
|
1047
|
+
|
|
1048
|
+
random_url(); // 生成一个随机URL
|
|
1049
|
+
```
|
|
1050
|
+
|
|
1051
|
+
#### shuffle_array
|
|
1052
|
+
- **功能**: 打乱数组顺序
|
|
1053
|
+
- **参数**: `array: T[]` - 源数组
|
|
1054
|
+
- **返回类型**: `T[]` - 打乱后的新数组
|
|
1055
|
+
- **示例**:
|
|
1056
|
+
```typescript
|
|
1057
|
+
import { shuffle_array } from '@qy_better_lib/core';
|
|
1058
|
+
|
|
1059
|
+
const array = [1, 2, 3, 4, 5];
|
|
1060
|
+
const shuffled = shuffle_array(array); // 打乱数组顺序
|
|
1061
|
+
```
|
|
1062
|
+
|
|
1063
|
+
### 6. 函数工具 (share.ts)
|
|
1064
|
+
|
|
1065
|
+
#### debounce
|
|
1066
|
+
- **功能**: 防抖函数
|
|
1067
|
+
- **参数**:
|
|
1068
|
+
- `fn: Function` - 要执行的函数
|
|
1069
|
+
- `delay: number` - 延迟时间,默认 200ms
|
|
1070
|
+
- `immediate: boolean` - 是否立即执行,默认 false
|
|
1071
|
+
- **返回类型**: `Function` - 防抖处理后的函数
|
|
1072
|
+
- **示例**:
|
|
1073
|
+
```typescript
|
|
1074
|
+
import { debounce } from '@qy_better_lib/core';
|
|
1075
|
+
|
|
1076
|
+
// 防抖处理的搜索函数
|
|
1077
|
+
const debouncedSearch = debounce((keyword) => {
|
|
1078
|
+
console.log('搜索:', keyword);
|
|
1079
|
+
}, 300);
|
|
1080
|
+
|
|
1081
|
+
// 频繁调用时,只会在最后一次调用后300ms执行
|
|
1082
|
+
debouncedSearch('关键词1');
|
|
1083
|
+
debouncedSearch('关键词2');
|
|
1084
|
+
debouncedSearch('关键词3'); // 只有这次会执行
|
|
1085
|
+
```
|
|
1086
|
+
|
|
1087
|
+
#### throttle
|
|
1088
|
+
- **功能**: 节流函数
|
|
1089
|
+
- **参数**:
|
|
1090
|
+
- `fn: Function` - 要执行的函数
|
|
1091
|
+
- `interval: number` - 时间间隔,默认 200ms
|
|
1092
|
+
- `options: object` - 配置选项
|
|
1093
|
+
- **返回类型**: `Function` - 节流处理后的函数
|
|
1094
|
+
- **示例**:
|
|
1095
|
+
```typescript
|
|
1096
|
+
import { throttle } from '@qy_better_lib/core';
|
|
1097
|
+
|
|
1098
|
+
// 节流处理的滚动函数
|
|
1099
|
+
const throttledScroll = throttle(() => {
|
|
1100
|
+
console.log('滚动事件');
|
|
1101
|
+
}, 100);
|
|
1102
|
+
|
|
1103
|
+
// 滚动时,每100ms执行一次
|
|
1104
|
+
window.addEventListener('scroll', throttledScroll);
|
|
1105
|
+
```
|
|
1106
|
+
|
|
1107
|
+
#### curry
|
|
1108
|
+
- **功能**: 柯里化函数
|
|
1109
|
+
- **参数**: `fn: Function` - 要柯里化的函数
|
|
1110
|
+
- **返回类型**: `Function` - 柯里化后的函数
|
|
1111
|
+
- **示例**:
|
|
1112
|
+
```typescript
|
|
1113
|
+
import { curry } from '@qy_better_lib/core';
|
|
1114
|
+
|
|
1115
|
+
const add = (a, b, c) => a + b + c;
|
|
1116
|
+
const curriedAdd = curry(add);
|
|
1117
|
+
|
|
1118
|
+
curriedAdd(1)(2)(3); // 6
|
|
1119
|
+
curriedAdd(1, 2)(3); // 6
|
|
1120
|
+
curriedAdd(1)(2, 3); // 6
|
|
1121
|
+
```
|
|
1122
|
+
|
|
1123
|
+
#### compose
|
|
1124
|
+
- **功能**: 函数组合
|
|
1125
|
+
- **参数**: `...fns: Function[]` - 要组合的函数列表
|
|
1126
|
+
- **返回类型**: `Function` - 组合后的函数
|
|
1127
|
+
- **示例**:
|
|
1128
|
+
```typescript
|
|
1129
|
+
import { compose } from '@qy_better_lib/core';
|
|
1130
|
+
|
|
1131
|
+
const add1 = x => x + 1;
|
|
1132
|
+
const multiply2 = x => x * 2;
|
|
1133
|
+
const subtract3 = x => x - 3;
|
|
1134
|
+
|
|
1135
|
+
const composed = compose(subtract3, multiply2, add1);
|
|
1136
|
+
composed(5); // ((5 + 1) * 2) - 3 = 9
|
|
1137
|
+
```
|
|
1138
|
+
|
|
1139
|
+
#### pipe
|
|
1140
|
+
- **功能**: 管道函数
|
|
1141
|
+
- **参数**: `...fns: Function[]` - 要管道的函数列表
|
|
1142
|
+
- **返回类型**: `Function` - 管道后的函数
|
|
1143
|
+
- **示例**:
|
|
1144
|
+
```typescript
|
|
1145
|
+
import { pipe } from '@qy_better_lib/core';
|
|
1146
|
+
|
|
1147
|
+
const add1 = x => x + 1;
|
|
1148
|
+
const multiply2 = x => x * 2;
|
|
1149
|
+
const subtract3 = x => x - 3;
|
|
1150
|
+
|
|
1151
|
+
const piped = pipe(add1, multiply2, subtract3);
|
|
1152
|
+
piped(5); // ((5 + 1) * 2) - 3 = 9
|
|
1153
|
+
```
|
|
1154
|
+
|
|
1155
|
+
#### delay_execution
|
|
1156
|
+
- **功能**: 延迟执行函数
|
|
1157
|
+
- **参数**:
|
|
1158
|
+
- `fn: Function` - 要执行的函数
|
|
1159
|
+
- `delay: number` - 延迟时间,默认 1000ms
|
|
1160
|
+
- **返回类型**: `Promise<any>` - 执行结果的Promise
|
|
1161
|
+
- **示例**:
|
|
1162
|
+
```typescript
|
|
1163
|
+
import { delay_execution } from '@qy_better_lib/core';
|
|
1164
|
+
|
|
1165
|
+
async function example() {
|
|
1166
|
+
console.log('开始');
|
|
1167
|
+
await delay_execution(() => {
|
|
1168
|
+
console.log('延迟执行');
|
|
1169
|
+
}, 2000);
|
|
1170
|
+
console.log('结束');
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
example(); // 开始 -> 2秒后 -> 延迟执行 -> 结束
|
|
1174
|
+
```
|
|
1175
|
+
|
|
1176
|
+
#### retry
|
|
1177
|
+
- **功能**: 重试函数
|
|
1178
|
+
- **参数**:
|
|
1179
|
+
- `fn: Function` - 要执行的函数
|
|
1180
|
+
- `options: object` - 配置选项
|
|
1181
|
+
- **返回类型**: `Promise<any>` - 执行结果的Promise
|
|
1182
|
+
- **示例**:
|
|
1183
|
+
```typescript
|
|
1184
|
+
import { retry } from '@qy_better_lib/core';
|
|
1185
|
+
|
|
1186
|
+
async function example() {
|
|
1187
|
+
const result = await retry(async () => {
|
|
1188
|
+
// 可能会失败的操作
|
|
1189
|
+
if (Math.random() > 0.5) {
|
|
1190
|
+
throw new Error('失败');
|
|
1191
|
+
}
|
|
1192
|
+
return '成功';
|
|
1193
|
+
}, {
|
|
1194
|
+
maxAttempts: 3,
|
|
1195
|
+
delay: 1000
|
|
1196
|
+
});
|
|
1197
|
+
console.log(result);
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
example();
|
|
1201
|
+
```
|
|
1202
|
+
|
|
1203
|
+
#### once
|
|
1204
|
+
- **功能**: 一次性函数
|
|
1205
|
+
- **参数**: `fn: Function` - 要执行的函数
|
|
1206
|
+
- **返回类型**: `Function` - 一次性函数
|
|
1207
|
+
- **示例**:
|
|
1208
|
+
```typescript
|
|
1209
|
+
import { once } from '@qy_better_lib/core';
|
|
1210
|
+
|
|
1211
|
+
const initialize = once(() => {
|
|
1212
|
+
console.log('初始化');
|
|
1213
|
+
return '初始化完成';
|
|
1214
|
+
});
|
|
1215
|
+
|
|
1216
|
+
initialize(); // 初始化
|
|
1217
|
+
initialize(); // 无输出
|
|
1218
|
+
initialize(); // 无输出
|
|
1219
|
+
```
|
|
1220
|
+
|
|
1221
|
+
#### memoize
|
|
1222
|
+
- **功能**: 记忆化函数
|
|
1223
|
+
- **参数**:
|
|
1224
|
+
- `fn: Function` - 要记忆化的函数
|
|
1225
|
+
- `keyGenerator: Function` - 缓存键生成函数
|
|
1226
|
+
- **返回类型**: `Function` - 记忆化后的函数
|
|
1227
|
+
- **示例**:
|
|
1228
|
+
```typescript
|
|
1229
|
+
import { memoize } from '@qy_better_lib/core';
|
|
1230
|
+
|
|
1231
|
+
// 记忆化的计算函数
|
|
1232
|
+
const memoizedCalculate = memoize((a, b) => {
|
|
1233
|
+
console.log('计算中...');
|
|
1234
|
+
return a + b;
|
|
1235
|
+
});
|
|
1236
|
+
|
|
1237
|
+
console.log(memoizedCalculate(1, 2)); // 计算中... 3
|
|
1238
|
+
console.log(memoizedCalculate(1, 2)); // 直接返回缓存结果 3
|
|
1239
|
+
console.log(memoizedCalculate(2, 3)); // 计算中... 5
|
|
1240
|
+
```
|
|
1241
|
+
|
|
1242
|
+
#### batch_execution
|
|
1243
|
+
- **功能**: 批量执行函数
|
|
1244
|
+
- **参数**:
|
|
1245
|
+
- `fns: Function[]` - 要执行的函数数组
|
|
1246
|
+
- `parallel: boolean` - 是否并行执行,默认 true
|
|
1247
|
+
- **返回类型**: `Promise<any[]>` - 执行结果的Promise数组
|
|
1248
|
+
- **示例**:
|
|
1249
|
+
```typescript
|
|
1250
|
+
import { batch_execution } from '@qy_better_lib/core';
|
|
1251
|
+
|
|
1252
|
+
async function example() {
|
|
1253
|
+
const fns = [
|
|
1254
|
+
() => Promise.resolve(1),
|
|
1255
|
+
() => Promise.resolve(2),
|
|
1256
|
+
() => Promise.resolve(3)
|
|
1257
|
+
];
|
|
1258
|
+
|
|
1259
|
+
const results = await batch_execution(fns);
|
|
1260
|
+
console.log(results); // [1, 2, 3]
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
example();
|
|
1264
|
+
```
|
|
1265
|
+
|
|
1266
|
+
#### timeout
|
|
1267
|
+
- **功能**: 超时控制函数
|
|
1268
|
+
- **参数**:
|
|
1269
|
+
- `fn: Function` - 要执行的函数
|
|
1270
|
+
- `timeout: number` - 超时时间,默认 3000ms
|
|
1271
|
+
- `message: string` - 超时消息,默认 '操作超时'
|
|
1272
|
+
- **返回类型**: `Promise<any>` - 执行结果的Promise
|
|
1273
|
+
- **示例**:
|
|
1274
|
+
```typescript
|
|
1275
|
+
import { timeout } from '@qy_better_lib/core';
|
|
1276
|
+
|
|
1277
|
+
async function example() {
|
|
1278
|
+
try {
|
|
1279
|
+
const result = await timeout(async () => {
|
|
1280
|
+
// 模拟长时间操作
|
|
1281
|
+
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
1282
|
+
return '成功';
|
|
1283
|
+
}, 3000);
|
|
1284
|
+
console.log(result);
|
|
1285
|
+
} catch (error) {
|
|
1286
|
+
console.log(error.message); // 操作超时
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
example();
|
|
1291
|
+
```
|
|
1292
|
+
|
|
1293
|
+
### 7. 存储工具 (storage.ts)
|
|
1294
|
+
|
|
1295
|
+
#### get_token
|
|
1296
|
+
- **功能**: 获取用户token,包含过期时间判断
|
|
1297
|
+
- **返回类型**: `any | null` - token值或null
|
|
1298
|
+
- **示例**:
|
|
1299
|
+
```typescript
|
|
1300
|
+
import { get_token } from '@qy_better_lib/core';
|
|
1301
|
+
|
|
1302
|
+
const token = get_token();
|
|
1303
|
+
console.log(token);
|
|
1304
|
+
```
|
|
1305
|
+
|
|
1306
|
+
#### set_token
|
|
1307
|
+
- **功能**: 设置用户token,有效期默认七天
|
|
1308
|
+
- **参数**:
|
|
1309
|
+
- `value: any` - token值
|
|
1310
|
+
- `expires: number` - 过期时间(秒),默认7天
|
|
1311
|
+
- **示例**:
|
|
1312
|
+
```typescript
|
|
1313
|
+
import { set_token } from '@qy_better_lib/core';
|
|
1314
|
+
|
|
1315
|
+
set_token('abc123'); // 默认7天过期
|
|
1316
|
+
set_token('abc123', 3600); // 1小时过期
|
|
1317
|
+
```
|
|
1318
|
+
|
|
1319
|
+
#### remove_token
|
|
1320
|
+
- **功能**: 移除token
|
|
1321
|
+
- **示例**:
|
|
1322
|
+
```typescript
|
|
1323
|
+
import { remove_token } from '@qy_better_lib/core';
|
|
1324
|
+
|
|
1325
|
+
remove_token();
|
|
1326
|
+
```
|
|
1327
|
+
|
|
1328
|
+
#### set_storage
|
|
1329
|
+
- **功能**: 设置缓存
|
|
1330
|
+
- **参数**:
|
|
1331
|
+
- `key: string` - 缓存键
|
|
1332
|
+
- `value: any` - 缓存值
|
|
1333
|
+
- `type: 'local' | 'session'` - 存储类型,默认'local'
|
|
1334
|
+
- **示例**:
|
|
1335
|
+
```typescript
|
|
1336
|
+
import { set_storage } from '@qy_better_lib/core';
|
|
1337
|
+
|
|
1338
|
+
// 设置localStorage
|
|
1339
|
+
set_storage('user', { name: '张三', age: 20 });
|
|
1340
|
+
|
|
1341
|
+
// 设置sessionStorage
|
|
1342
|
+
set_storage('token', 'abc123', 'session');
|
|
1343
|
+
```
|
|
1344
|
+
|
|
1345
|
+
#### get_storage
|
|
1346
|
+
- **功能**: 获取缓存
|
|
1347
|
+
- **参数**:
|
|
1348
|
+
- `key: string` - 缓存键
|
|
1349
|
+
- `type: 'local' | 'session'` - 存储类型,默认'local'
|
|
1350
|
+
- **返回类型**: `any` - 缓存值或null
|
|
1351
|
+
- **示例**:
|
|
1352
|
+
```typescript
|
|
1353
|
+
import { get_storage } from '@qy_better_lib/core';
|
|
1354
|
+
|
|
1355
|
+
// 获取localStorage
|
|
1356
|
+
const user = get_storage('user');
|
|
1357
|
+
console.log(user); // { name: '张三', age: 20 }
|
|
1358
|
+
|
|
1359
|
+
// 获取sessionStorage
|
|
1360
|
+
const token = get_storage('token', 'session');
|
|
1361
|
+
console.log(token); // 'abc123'
|
|
1362
|
+
```
|
|
1363
|
+
|
|
1364
|
+
#### remove_storage
|
|
1365
|
+
- **功能**: 删除指定key的缓存
|
|
1366
|
+
- **参数**:
|
|
1367
|
+
- `key: string` - 缓存键
|
|
1368
|
+
- `type: 'local' | 'session'` - 存储类型,默认'local'
|
|
1369
|
+
- **示例**:
|
|
1370
|
+
```typescript
|
|
1371
|
+
import { remove_storage } from '@qy_better_lib/core';
|
|
1372
|
+
|
|
1373
|
+
// 移除localStorage
|
|
1374
|
+
remove_storage('user');
|
|
1375
|
+
|
|
1376
|
+
// 移除sessionStorage
|
|
1377
|
+
remove_storage('token', 'session');
|
|
1378
|
+
```
|
|
1379
|
+
|
|
1380
|
+
#### clear_storage
|
|
1381
|
+
- **功能**: 清除所有缓存
|
|
1382
|
+
- **参数**: `type: 'local' | 'session'` - 存储类型,默认'local'
|
|
1383
|
+
- **示例**:
|
|
1384
|
+
```typescript
|
|
1385
|
+
import { clear_storage } from '@qy_better_lib/core';
|
|
1386
|
+
|
|
1387
|
+
// 清空localStorage
|
|
1388
|
+
clear_storage();
|
|
1389
|
+
|
|
1390
|
+
// 清空sessionStorage
|
|
1391
|
+
clear_storage('session');
|
|
1392
|
+
```
|
|
1393
|
+
|
|
1394
|
+
#### set_storage_with_expiry
|
|
1395
|
+
- **功能**: 设置带过期时间的缓存
|
|
1396
|
+
- **参数**:
|
|
1397
|
+
- `key: string` - 缓存键
|
|
1398
|
+
- `value: any` - 缓存值
|
|
1399
|
+
- `expiry: number` - 过期时间(秒)
|
|
1400
|
+
- `type: 'local' | 'session'` - 存储类型,默认'local'
|
|
1401
|
+
- **示例**:
|
|
1402
|
+
```typescript
|
|
1403
|
+
import { set_storage_with_expiry } from '@qy_better_lib/core';
|
|
1404
|
+
|
|
1405
|
+
// 设置1小时过期的缓存
|
|
1406
|
+
set_storage_with_expiry('user', { name: '张三', age: 20 }, 3600);
|
|
1407
|
+
```
|
|
1408
|
+
|
|
1409
|
+
#### get_storage_with_expiry
|
|
1410
|
+
- **功能**: 获取带过期时间的缓存
|
|
1411
|
+
- **参数**:
|
|
1412
|
+
- `key: string` - 缓存键
|
|
1413
|
+
- `type: 'local' | 'session'` - 存储类型,默认'local'
|
|
1414
|
+
- **返回类型**: `any` - 缓存值或null
|
|
1415
|
+
- **示例**:
|
|
1416
|
+
```typescript
|
|
1417
|
+
import { get_storage_with_expiry } from '@qy_better_lib/core';
|
|
1418
|
+
|
|
1419
|
+
const user = get_storage_with_expiry('user');
|
|
1420
|
+
console.log(user);
|
|
1421
|
+
```
|
|
1422
|
+
|
|
1423
|
+
#### has_storage
|
|
1424
|
+
- **功能**: 检查缓存是否存在
|
|
1425
|
+
- **参数**:
|
|
1426
|
+
- `key: string` - 缓存键
|
|
1427
|
+
- `type: 'local' | 'session'` - 存储类型,默认'local'
|
|
1428
|
+
- **返回类型**: `boolean` - 是否存在
|
|
1429
|
+
- **示例**:
|
|
1430
|
+
```typescript
|
|
1431
|
+
import { has_storage } from '@qy_better_lib/core';
|
|
1432
|
+
|
|
1433
|
+
const hasUser = has_storage('user');
|
|
1434
|
+
console.log(hasUser);
|
|
1435
|
+
```
|
|
1436
|
+
|
|
1437
|
+
#### get_all_storage_keys
|
|
1438
|
+
- **功能**: 获取所有缓存键
|
|
1439
|
+
- **参数**: `type: 'local' | 'session'` - 存储类型,默认'local'
|
|
1440
|
+
- **返回类型**: `string[]` - 缓存键数组
|
|
1441
|
+
- **示例**:
|
|
1442
|
+
```typescript
|
|
1443
|
+
import { get_all_storage_keys } from '@qy_better_lib/core';
|
|
1444
|
+
|
|
1445
|
+
const keys = get_all_storage_keys();
|
|
1446
|
+
console.log(keys);
|
|
1447
|
+
```
|
|
1448
|
+
|
|
1449
|
+
#### clear_expired_storage
|
|
1450
|
+
- **功能**: 清除过期的缓存
|
|
1451
|
+
- **参数**: `type: 'local' | 'session'` - 存储类型,默认'local'
|
|
1452
|
+
- **返回类型**: `number` - 清除的缓存数量
|
|
1453
|
+
- **示例**:
|
|
1454
|
+
```typescript
|
|
1455
|
+
import { clear_expired_storage } from '@qy_better_lib/core';
|
|
1456
|
+
|
|
1457
|
+
const count = clear_expired_storage();
|
|
1458
|
+
console.log(`清除了${count}个过期缓存`);
|
|
1459
|
+
```
|
|
1460
|
+
|
|
1461
|
+
#### encrypt_storage
|
|
1462
|
+
- **功能**: 加密存储敏感数据
|
|
1463
|
+
- **参数**:
|
|
1464
|
+
- `key: string` - 缓存键
|
|
1465
|
+
- `value: any` - 缓存值
|
|
1466
|
+
- `type: 'local' | 'session'` - 存储类型,默认'local'
|
|
1467
|
+
- **示例**:
|
|
1468
|
+
```typescript
|
|
1469
|
+
import { encrypt_storage } from '@qy_better_lib/core';
|
|
1470
|
+
|
|
1471
|
+
// 加密存储用户信息
|
|
1472
|
+
encrypt_storage('user', { name: '张三', age: 20 });
|
|
1473
|
+
```
|
|
1474
|
+
|
|
1475
|
+
#### decrypt_storage
|
|
1476
|
+
- **功能**: 解密获取敏感数据
|
|
1477
|
+
- **参数**:
|
|
1478
|
+
- `key: string` - 缓存键
|
|
1479
|
+
- `type: 'local' | 'session'` - 存储类型,默认'local'
|
|
1480
|
+
- **返回类型**: `any` - 解密后的数据或null
|
|
1481
|
+
- **示例**:
|
|
1482
|
+
```typescript
|
|
1483
|
+
import { decrypt_storage } from '@qy_better_lib/core';
|
|
1484
|
+
|
|
1485
|
+
// 解密获取用户信息
|
|
1486
|
+
const user = decrypt_storage('user');
|
|
1487
|
+
console.log(user); // { name: '张三', age: 20 }
|
|
1488
|
+
```
|
|
1489
|
+
|
|
1490
|
+
#### set_batch_storage
|
|
1491
|
+
- **功能**: 批量设置缓存
|
|
1492
|
+
- **参数**:
|
|
1493
|
+
- `items: Array<{ key: string; value: any }>` - 缓存项数组
|
|
1494
|
+
- `type: 'local' | 'session'` - 存储类型,默认'local'
|
|
1495
|
+
- **示例**:
|
|
1496
|
+
```typescript
|
|
1497
|
+
import { set_batch_storage } from '@qy_better_lib/core';
|
|
1498
|
+
|
|
1499
|
+
set_batch_storage([
|
|
1500
|
+
{ key: 'user', value: { name: '张三', age: 20 } },
|
|
1501
|
+
{ key: 'token', value: 'abc123' }
|
|
1502
|
+
]);
|
|
1503
|
+
```
|
|
1504
|
+
|
|
1505
|
+
#### get_batch_storage
|
|
1506
|
+
- **功能**: 批量获取缓存
|
|
1507
|
+
- **参数**:
|
|
1508
|
+
- `keys: string[]` - 缓存键数组
|
|
1509
|
+
- `type: 'local' | 'session'` - 存储类型,默认'local'
|
|
1510
|
+
- **返回类型**: `Record<string, any>` - 缓存值对象
|
|
1511
|
+
- **示例**:
|
|
1512
|
+
```typescript
|
|
1513
|
+
import { get_batch_storage } from '@qy_better_lib/core';
|
|
1514
|
+
|
|
1515
|
+
const data = get_batch_storage(['user', 'token']);
|
|
1516
|
+
console.log(data.user);
|
|
1517
|
+
console.log(data.token);
|
|
1518
|
+
```
|
|
1519
|
+
|
|
1520
|
+
#### remove_batch_storage
|
|
1521
|
+
- **功能**: 批量删除缓存
|
|
1522
|
+
- **参数**:
|
|
1523
|
+
- `keys: string[]` - 缓存键数组
|
|
1524
|
+
- `type: 'local' | 'session'` - 存储类型,默认'local'
|
|
1525
|
+
- **示例**:
|
|
1526
|
+
```typescript
|
|
1527
|
+
import { remove_batch_storage } from '@qy_better_lib/core';
|
|
1528
|
+
|
|
1529
|
+
remove_batch_storage(['user', 'token']);
|
|
1530
|
+
```
|
|
1531
|
+
|
|
1532
|
+
### 8. DOM 操作工具 (dom.ts)
|
|
1533
|
+
|
|
1534
|
+
#### get_parent_by_class
|
|
1535
|
+
- **功能**: 根据类名获取父元素
|
|
1536
|
+
- **参数**:
|
|
1537
|
+
- `dom: Element | null` - DOM元素
|
|
1538
|
+
- `class_name: string` - CSS类名
|
|
1539
|
+
- **返回类型**: `Element | null` - DOM元素或null
|
|
1540
|
+
- **示例**:
|
|
1541
|
+
```typescript
|
|
1542
|
+
import { get_parent_by_class } from '@qy_better_lib/core';
|
|
1543
|
+
|
|
1544
|
+
const childElement = document.getElementById('child');
|
|
1545
|
+
const parentElement = get_parent_by_class(childElement, 'parent-class');
|
|
1546
|
+
console.log(parentElement);
|
|
1547
|
+
```
|
|
1548
|
+
|
|
1549
|
+
#### scroll_footer
|
|
1550
|
+
- **功能**: 滚动到底部
|
|
1551
|
+
- **参数**:
|
|
1552
|
+
- `dom_id: string` - 元素ID
|
|
1553
|
+
- `behavior: boolean` - 开启滚动动画,默认true
|
|
1554
|
+
- **示例**:
|
|
1555
|
+
```typescript
|
|
1556
|
+
import { scroll_footer } from '@qy_better_lib/core';
|
|
1557
|
+
|
|
1558
|
+
// 滚动到ID为container的元素底部
|
|
1559
|
+
scroll_footer('container');
|
|
1560
|
+
```
|
|
1561
|
+
|
|
1562
|
+
#### get_element
|
|
1563
|
+
- **功能**: 根据选择器获取元素
|
|
1564
|
+
- **参数**:
|
|
1565
|
+
- `selector: string` - 选择器
|
|
1566
|
+
- `context: Element | Document` - 上下文元素,默认为document
|
|
1567
|
+
- **返回类型**: `Element | null` - DOM元素或null
|
|
1568
|
+
- **示例**:
|
|
1569
|
+
```typescript
|
|
1570
|
+
import { get_element } from '@qy_better_lib/core';
|
|
1571
|
+
|
|
1572
|
+
// 通过选择器获取元素
|
|
1573
|
+
const element = get_element('#container');
|
|
1574
|
+
|
|
1575
|
+
// 在指定上下文中获取元素
|
|
1576
|
+
const parentElement = get_element('.parent');
|
|
1577
|
+
const childElement = get_element('.child', parentElement);
|
|
1578
|
+
```
|
|
1579
|
+
|
|
1580
|
+
#### get_elements
|
|
1581
|
+
- **功能**: 根据选择器获取多个元素
|
|
1582
|
+
- **参数**:
|
|
1583
|
+
- `selector: string` - 选择器
|
|
1584
|
+
- `context: Element | Document` - 上下文元素,默认为document
|
|
1585
|
+
- **返回类型**: `Element[]` - DOM元素数组
|
|
1586
|
+
- **示例**:
|
|
1587
|
+
```typescript
|
|
1588
|
+
import { get_elements } from '@qy_better_lib/core';
|
|
1589
|
+
|
|
1590
|
+
// 获取所有段落元素
|
|
1591
|
+
const paragraphs = get_elements('p');
|
|
1592
|
+
console.log(paragraphs.length);
|
|
1593
|
+
```
|
|
1594
|
+
|
|
1595
|
+
#### add_class
|
|
1596
|
+
- **功能**: 添加类名
|
|
1597
|
+
- **参数**:
|
|
1598
|
+
- `element: Element | null` - DOM元素
|
|
1599
|
+
- `class_name: string` - 类名
|
|
1600
|
+
- **示例**:
|
|
1601
|
+
```typescript
|
|
1602
|
+
import { add_class } from '@qy_better_lib/core';
|
|
1603
|
+
|
|
1604
|
+
const element = document.getElementById('container');
|
|
1605
|
+
add_class(element, 'active');
|
|
1606
|
+
```
|
|
1607
|
+
|
|
1608
|
+
#### remove_class
|
|
1609
|
+
- **功能**: 移除类名
|
|
1610
|
+
- **参数**:
|
|
1611
|
+
- `element: Element | null` - DOM元素
|
|
1612
|
+
- `class_name: string` - 类名
|
|
1613
|
+
- **示例**:
|
|
1614
|
+
```typescript
|
|
1615
|
+
import { remove_class } from '@qy_better_lib/core';
|
|
1616
|
+
|
|
1617
|
+
const element = document.getElementById('container');
|
|
1618
|
+
remove_class(element, 'active');
|
|
1619
|
+
```
|
|
1620
|
+
|
|
1621
|
+
#### toggle_class
|
|
1622
|
+
- **功能**: 切换类名
|
|
1623
|
+
- **参数**:
|
|
1624
|
+
- `element: Element | null` - DOM元素
|
|
1625
|
+
- `class_name: string` - 类名
|
|
1626
|
+
- **返回类型**: `boolean` - 是否添加了类名
|
|
1627
|
+
- **示例**:
|
|
1628
|
+
```typescript
|
|
1629
|
+
import { toggle_class } from '@qy_better_lib/core';
|
|
1630
|
+
|
|
1631
|
+
const element = document.getElementById('container');
|
|
1632
|
+
const isActive = toggle_class(element, 'active');
|
|
1633
|
+
console.log(isActive); // 切换后的状态
|
|
1634
|
+
```
|
|
1635
|
+
|
|
1636
|
+
#### has_class
|
|
1637
|
+
- **功能**: 检查是否包含类名
|
|
1638
|
+
- **参数**:
|
|
1639
|
+
- `element: Element | null` - DOM元素
|
|
1640
|
+
- `class_name: string` - 类名
|
|
1641
|
+
- **返回类型**: `boolean` - 是否包含类名
|
|
1642
|
+
- **示例**:
|
|
1643
|
+
```typescript
|
|
1644
|
+
import { has_class } from '@qy_better_lib/core';
|
|
1645
|
+
|
|
1646
|
+
const element = document.getElementById('container');
|
|
1647
|
+
const hasActive = has_class(element, 'active');
|
|
1648
|
+
console.log(hasActive);
|
|
1649
|
+
```
|
|
1650
|
+
|
|
1651
|
+
#### get_style
|
|
1652
|
+
- **功能**: 获取元素样式
|
|
1653
|
+
- **参数**:
|
|
1654
|
+
- `element: Element | null` - DOM元素
|
|
1655
|
+
- `property: string` - CSS属性名
|
|
1656
|
+
- **返回类型**: `string` - CSS属性值
|
|
1657
|
+
- **示例**:
|
|
1658
|
+
```typescript
|
|
1659
|
+
import { get_style } from '@qy_better_lib/core';
|
|
1660
|
+
|
|
1661
|
+
const element = document.getElementById('container');
|
|
1662
|
+
const color = get_style(element, 'color');
|
|
1663
|
+
console.log(color);
|
|
1664
|
+
```
|
|
1665
|
+
|
|
1666
|
+
#### set_style
|
|
1667
|
+
- **功能**: 设置元素样式
|
|
1668
|
+
- **参数**:
|
|
1669
|
+
- `element: Element | null` - DOM元素
|
|
1670
|
+
- `property: string` - CSS属性名
|
|
1671
|
+
- `value: string` - CSS属性值
|
|
1672
|
+
- **示例**:
|
|
1673
|
+
```typescript
|
|
1674
|
+
import { set_style } from '@qy_better_lib/core';
|
|
1675
|
+
|
|
1676
|
+
const element = document.getElementById('container');
|
|
1677
|
+
set_style(element, 'color', 'red');
|
|
1678
|
+
set_style(element, 'fontSize', '16px');
|
|
1679
|
+
```
|
|
1680
|
+
|
|
1681
|
+
#### set_styles
|
|
1682
|
+
- **功能**: 设置元素多个样式
|
|
1683
|
+
- **参数**:
|
|
1684
|
+
- `element: Element | null` - DOM元素
|
|
1685
|
+
- `styles: Record<string, string>` - 样式对象
|
|
1686
|
+
- **示例**:
|
|
1687
|
+
```typescript
|
|
1688
|
+
import { set_styles } from '@qy_better_lib/core';
|
|
1689
|
+
|
|
1690
|
+
const element = document.getElementById('container');
|
|
1691
|
+
set_styles(element, {
|
|
1692
|
+
color: 'red',
|
|
1693
|
+
fontSize: '16px',
|
|
1694
|
+
backgroundColor: '#f0f0f0'
|
|
1695
|
+
});
|
|
1696
|
+
```
|
|
1697
|
+
|
|
1698
|
+
#### auto_size
|
|
1699
|
+
- **功能**: 根据屏幕宽度自动调整尺寸
|
|
1700
|
+
- **参数**:
|
|
1701
|
+
- `size: number` - 原始尺寸值
|
|
1702
|
+
- `deflate_width: number` - 基准宽度,默认 1920
|
|
1703
|
+
- **返回类型**: `number` - 调整后的尺寸值
|
|
1704
|
+
- **示例**:
|
|
1705
|
+
```typescript
|
|
1706
|
+
import { auto_size } from '@qy_better_lib/core';
|
|
1707
|
+
|
|
1708
|
+
// 根据屏幕宽度自动调整字体大小
|
|
1709
|
+
const fontSize = auto_size(16);
|
|
1710
|
+
console.log(fontSize); // 根据当前屏幕宽度计算的值
|
|
1711
|
+
```
|
|
1712
|
+
|
|
1713
|
+
### 9. 颜色工具 (color.ts)
|
|
1714
|
+
|
|
1715
|
+
#### mix
|
|
1716
|
+
- **功能**: 过渡颜色生成
|
|
1717
|
+
- **参数**:
|
|
1718
|
+
- `color1: string` - 第一个颜色
|
|
1719
|
+
- `color2: string` - 第二个颜色
|
|
1720
|
+
- `weight: number` - 混合权重 (0-1)
|
|
1721
|
+
- **返回类型**: `string` - 混合后的颜色
|
|
1722
|
+
- **示例**:
|
|
1723
|
+
```typescript
|
|
1724
|
+
import { mix } from '@qy_better_lib/core';
|
|
1725
|
+
|
|
1726
|
+
// 混合红色和蓝色,权重0.5
|
|
1727
|
+
const mixedColor = mix('#ff0000', '#0000ff', 0.5);
|
|
1728
|
+
console.log(mixedColor); // #800080
|
|
1729
|
+
```
|
|
1730
|
+
|
|
1731
|
+
#### get_rgb
|
|
1732
|
+
- **功能**: HEX转成RGB字符串
|
|
1733
|
+
- **参数**: `color: string` - HEX颜色值
|
|
1734
|
+
- **返回类型**: `string` - RGB字符串 "r,g,b"
|
|
1735
|
+
- **示例**:
|
|
1736
|
+
```typescript
|
|
1737
|
+
import { get_rgb } from '@qy_better_lib/core';
|
|
1738
|
+
|
|
1739
|
+
const rgb = get_rgb('#ff0000');
|
|
1740
|
+
console.log(rgb); // "255,0,0"
|
|
1741
|
+
```
|
|
1742
|
+
|
|
1743
|
+
#### hex_to_rgb
|
|
1744
|
+
- **功能**: HEX转成RGB对象
|
|
1745
|
+
- **参数**: `hex: string` - HEX颜色值
|
|
1746
|
+
- **返回类型**: `{r, g, b} | null` - RGB对象或null
|
|
1747
|
+
- **示例**:
|
|
1748
|
+
```typescript
|
|
1749
|
+
import { hex_to_rgb } from '@qy_better_lib/core';
|
|
1750
|
+
|
|
1751
|
+
const rgb = hex_to_rgb('#ff0000');
|
|
1752
|
+
console.log(rgb); // { r: 255, g: 0, b: 0 }
|
|
1753
|
+
```
|
|
1754
|
+
|
|
1755
|
+
#### rgb_to_hex
|
|
1756
|
+
- **功能**: RGB转成HEX
|
|
1757
|
+
- **参数**:
|
|
1758
|
+
- `r: number` - 红色通道值 (0-255)
|
|
1759
|
+
- `g: number` - 绿色通道值 (0-255)
|
|
1760
|
+
- `b: number` - 蓝色通道值 (0-255)
|
|
1761
|
+
- **返回类型**: `string` - HEX颜色值
|
|
1762
|
+
- **示例**:
|
|
1763
|
+
```typescript
|
|
1764
|
+
import { rgb_to_hex } from '@qy_better_lib/core';
|
|
1765
|
+
|
|
1766
|
+
const hex = rgb_to_hex(255, 0, 0);
|
|
1767
|
+
console.log(hex); // #ff0000
|
|
1768
|
+
```
|
|
1769
|
+
|
|
1770
|
+
#### rgba_to_hex
|
|
1771
|
+
- **功能**: RGBA转成HEX
|
|
1772
|
+
- **参数**:
|
|
1773
|
+
- `r: number` - 红色通道值 (0-255)
|
|
1774
|
+
- `g: number` - 绿色通道值 (0-255)
|
|
1775
|
+
- `b: number` - 蓝色通道值 (0-255)
|
|
1776
|
+
- `a: number` - 透明度值 (0-1)
|
|
1777
|
+
- **返回类型**: `string` - HEX颜色值
|
|
1778
|
+
- **示例**:
|
|
1779
|
+
```typescript
|
|
1780
|
+
import { rgba_to_hex } from '@qy_better_lib/core';
|
|
1781
|
+
|
|
1782
|
+
const hex = rgba_to_hex(255, 0, 0, 0.5);
|
|
1783
|
+
console.log(hex); // #ff0000
|
|
1784
|
+
```
|
|
1785
|
+
|
|
1786
|
+
#### lighten
|
|
1787
|
+
- **功能**: 使颜色变亮
|
|
1788
|
+
- **参数**:
|
|
1789
|
+
- `color: string` - HEX颜色值
|
|
1790
|
+
- `amount: number` - 变亮程度 (0-1)
|
|
1791
|
+
- **返回类型**: `string` - 变亮后的HEX颜色值
|
|
1792
|
+
- **示例**:
|
|
1793
|
+
```typescript
|
|
1794
|
+
import { lighten } from '@qy_better_lib/core';
|
|
1795
|
+
|
|
1796
|
+
const lightColor = lighten('#ff0000', 0.5);
|
|
1797
|
+
console.log(lightColor); // #ffffff
|
|
1798
|
+
```
|
|
1799
|
+
|
|
1800
|
+
#### darken
|
|
1801
|
+
- **功能**: 使颜色变暗
|
|
1802
|
+
- **参数**:
|
|
1803
|
+
- `color: string` - HEX颜色值
|
|
1804
|
+
- `amount: number` - 变暗程度 (0-1)
|
|
1805
|
+
- **返回类型**: `string` - 变暗后的HEX颜色值
|
|
1806
|
+
- **示例**:
|
|
1807
|
+
```typescript
|
|
1808
|
+
import { darken } from '@qy_better_lib/core';
|
|
1809
|
+
|
|
1810
|
+
const darkColor = darken('#ff0000', 0.5);
|
|
1811
|
+
console.log(darkColor); // #800000
|
|
1812
|
+
```
|
|
1813
|
+
|
|
1814
|
+
#### is_light
|
|
1815
|
+
- **功能**: 判断颜色是否为亮色
|
|
1816
|
+
- **参数**: `color: string` - HEX颜色值
|
|
1817
|
+
- **返回类型**: `boolean` - 是否为亮色
|
|
1818
|
+
- **示例**:
|
|
1819
|
+
```typescript
|
|
1820
|
+
import { is_light } from '@qy_better_lib/core';
|
|
1821
|
+
|
|
1822
|
+
const isLight = is_light('#ffffff');
|
|
1823
|
+
console.log(isLight); // true
|
|
1824
|
+
```
|
|
1825
|
+
|
|
1826
|
+
#### is_dark
|
|
1827
|
+
- **功能**: 判断颜色是否为暗色
|
|
1828
|
+
- **参数**: `color: string` - HEX颜色值
|
|
1829
|
+
- **返回类型**: `boolean` - 是否为暗色
|
|
1830
|
+
- **示例**:
|
|
1831
|
+
```typescript
|
|
1832
|
+
import { is_dark } from '@qy_better_lib/core';
|
|
1833
|
+
|
|
1834
|
+
const isDark = is_dark('#000000');
|
|
1835
|
+
console.log(isDark); // true
|
|
1836
|
+
```
|
|
1837
|
+
|
|
1838
|
+
#### get_contrast_color
|
|
1839
|
+
- **功能**: 获取与给定颜色对比度高的颜色(黑或白)
|
|
1840
|
+
- **参数**: `color: string` - HEX颜色值
|
|
1841
|
+
- **返回类型**: `string` - 对比度高的颜色 (#000000 或 #FFFFFF)
|
|
1842
|
+
- **示例**:
|
|
1843
|
+
```typescript
|
|
1844
|
+
import { get_contrast_color } from '@qy_better_lib/core';
|
|
1845
|
+
|
|
1846
|
+
const contrastColor = get_contrast_color('#ff0000');
|
|
1847
|
+
console.log(contrastColor); // #FFFFFF
|
|
1848
|
+
```
|
|
1849
|
+
|
|
1850
|
+
### 10. ECharts 工具 (echarts.ts)
|
|
1851
|
+
|
|
1852
|
+
#### get_chart_gradient_color
|
|
1853
|
+
- **功能**: Echarts线性渐变颜色
|
|
1854
|
+
- **参数**:
|
|
1855
|
+
- `type: 'v' | 'h'` - 渐变类型,'v'为垂直渐变,'h'为水平渐变
|
|
1856
|
+
- `start_color: string` - 起始颜色
|
|
1857
|
+
- `end_color: string` - 结束颜色
|
|
1858
|
+
- **返回类型**: `object` - 渐变配置对象
|
|
1859
|
+
- **示例**:
|
|
1860
|
+
```typescript
|
|
1861
|
+
import { get_chart_gradient_color } from '@qy_better_lib/core';
|
|
1862
|
+
|
|
1863
|
+
const gradient = get_chart_gradient_color('v', '#ff0000', '#0000ff');
|
|
1864
|
+
console.log(gradient);
|
|
1865
|
+
```
|
|
1866
|
+
|
|
1867
|
+
#### init_chart
|
|
1868
|
+
- **功能**: 初始化Echarts图表
|
|
1869
|
+
- **参数**:
|
|
1870
|
+
- `container: HTMLElement | string` - 容器元素或容器ID
|
|
1871
|
+
- `options?: EChartsOption` - 图表配置项
|
|
1872
|
+
- `renderer: 'canvas' | 'svg'` - 渲染方式,默认 'canvas'
|
|
1873
|
+
- `theme?: string` - 主题
|
|
1874
|
+
- **返回类型**: `Promise<any>` - Echarts实例
|
|
1875
|
+
- **示例**:
|
|
1876
|
+
```typescript
|
|
1877
|
+
import { init_chart } from '@qy_better_lib/core';
|
|
1878
|
+
|
|
1879
|
+
async function init() {
|
|
1880
|
+
const chart = await init_chart('chart-container', {
|
|
1881
|
+
title: { text: '示例图表' },
|
|
1882
|
+
series: [{ type: 'bar', data: [1, 2, 3] }]
|
|
1883
|
+
});
|
|
1884
|
+
return chart;
|
|
1885
|
+
}
|
|
1886
|
+
```
|
|
1887
|
+
|
|
1888
|
+
#### destroy_chart
|
|
1889
|
+
- **功能**: 销毁Echarts图表
|
|
1890
|
+
- **参数**: `chart: any` - Echarts实例
|
|
1891
|
+
- **示例**:
|
|
1892
|
+
```typescript
|
|
1893
|
+
import { destroy_chart } from '@qy_better_lib/core';
|
|
1894
|
+
|
|
1895
|
+
destroy_chart(chart);
|
|
1896
|
+
```
|
|
1897
|
+
|
|
1898
|
+
#### update_chart
|
|
1899
|
+
- **功能**: 更新Echarts图表配置
|
|
1900
|
+
- **参数**:
|
|
1901
|
+
- `chart: any` - Echarts实例
|
|
1902
|
+
- `options: EChartsOption` - 图表配置项
|
|
1903
|
+
- **示例**:
|
|
1904
|
+
```typescript
|
|
1905
|
+
import { update_chart } from '@qy_better_lib/core';
|
|
1906
|
+
|
|
1907
|
+
update_chart(chart, {
|
|
1908
|
+
series: [{ type: 'bar', data: [4, 5, 6] }]
|
|
1909
|
+
});
|
|
1910
|
+
```
|
|
1911
|
+
|
|
1912
|
+
#### resize_chart
|
|
1913
|
+
- **功能**: 调整Echarts图表大小
|
|
1914
|
+
- **参数**: `chart: any` - Echarts实例
|
|
1915
|
+
- **示例**:
|
|
1916
|
+
```typescript
|
|
1917
|
+
import { resize_chart } from '@qy_better_lib/core';
|
|
1918
|
+
|
|
1919
|
+
resize_chart(chart);
|
|
1920
|
+
```
|
|
1921
|
+
|
|
1922
|
+
#### make_chart_responsive
|
|
1923
|
+
- **功能**: 为Echarts图表添加响应式处理
|
|
1924
|
+
- **参数**:
|
|
1925
|
+
- `chart: any` - Echarts实例
|
|
1926
|
+
- `debounce_time: number` - 防抖时间,默认200ms
|
|
1927
|
+
- **返回类型**: `Function` - 清理函数
|
|
1928
|
+
- **示例**:
|
|
1929
|
+
```typescript
|
|
1930
|
+
import { make_chart_responsive } from '@qy_better_lib/core';
|
|
1931
|
+
|
|
1932
|
+
const cleanup = make_chart_responsive(chart);
|
|
1933
|
+
// 当需要清理时调用
|
|
1934
|
+
// cleanup();
|
|
1935
|
+
```
|
|
1936
|
+
|
|
1937
|
+
#### generate_chart_colors
|
|
1938
|
+
- **功能**: 生成Echarts颜色数组
|
|
1939
|
+
- **参数**:
|
|
1940
|
+
- `count: number` - 颜色数量
|
|
1941
|
+
- `base_colors?: string[]` - 基础颜色数组,默认使用ECharts默认颜色
|
|
1942
|
+
- **返回类型**: `string[]` - 颜色数组
|
|
1943
|
+
- **示例**:
|
|
1944
|
+
```typescript
|
|
1945
|
+
import { generate_chart_colors } from '@qy_better_lib/core';
|
|
1946
|
+
|
|
1947
|
+
const colors = generate_chart_colors(5);
|
|
1948
|
+
console.log(colors);
|
|
1949
|
+
```
|
|
1950
|
+
|
|
1951
|
+
#### format_chart_label
|
|
1952
|
+
- **功能**: 格式化Echarts数据标签
|
|
1953
|
+
- **参数**:
|
|
1954
|
+
- `value: any` - 数值
|
|
1955
|
+
- `formatter?: ((value: any) => string) | string` - 格式化函数或格式化模板
|
|
1956
|
+
- **返回类型**: `string` - 格式化后的值
|
|
1957
|
+
- **示例**:
|
|
1958
|
+
```typescript
|
|
1959
|
+
import { format_chart_label } from '@qy_better_lib/core';
|
|
1960
|
+
|
|
1961
|
+
const label = format_chart_label(123, '{value}件');
|
|
1962
|
+
console.log(label); // 123件
|
|
1963
|
+
```
|
|
1964
|
+
|
|
1965
|
+
#### merge_chart_options
|
|
1966
|
+
- **功能**: 合并Echarts配置项
|
|
1967
|
+
- **参数**:
|
|
1968
|
+
- `defaultOption: EChartsOption` - 默认配置项
|
|
1969
|
+
- `customOption: EChartsOption` - 自定义配置项
|
|
1970
|
+
- **返回类型**: `EChartsOption` - 合并后的配置项
|
|
1971
|
+
- **示例**:
|
|
1972
|
+
```typescript
|
|
1973
|
+
import { merge_chart_options } from '@qy_better_lib/core';
|
|
1974
|
+
|
|
1975
|
+
const options = merge_chart_options(
|
|
1976
|
+
{ title: { text: '默认标题' } },
|
|
1977
|
+
{ title: { text: '自定义标题' } }
|
|
1978
|
+
);
|
|
1979
|
+
```
|
|
1980
|
+
|
|
1981
|
+
#### add_chart_listener
|
|
1982
|
+
- **功能**: 为Echarts图表添加事件监听器
|
|
1983
|
+
- **参数**:
|
|
1984
|
+
- `chart: any` - Echarts实例
|
|
1985
|
+
- `eventName: string` - 事件名称
|
|
1986
|
+
- `handler: (...args: any[]) => void` - 事件处理函数
|
|
1987
|
+
- **示例**:
|
|
1988
|
+
```typescript
|
|
1989
|
+
import { add_chart_listener } from '@qy_better_lib/core';
|
|
1990
|
+
|
|
1991
|
+
add_chart_listener(chart, 'click', (params) => {
|
|
1992
|
+
console.log('点击事件:', params);
|
|
1993
|
+
});
|
|
1994
|
+
```
|
|
1995
|
+
|
|
1996
|
+
#### remove_chart_listener
|
|
1997
|
+
- **功能**: 移除Echarts图表事件监听器
|
|
1998
|
+
- **参数**:
|
|
1999
|
+
- `chart: any` - Echarts实例
|
|
2000
|
+
- `eventName: string` - 事件名称
|
|
2001
|
+
- `handler: (...args: any[]) => void` - 事件处理函数
|
|
2002
|
+
- **示例**:
|
|
2003
|
+
```typescript
|
|
2004
|
+
import { remove_chart_listener } from '@qy_better_lib/core';
|
|
2005
|
+
|
|
2006
|
+
const handler = (params) => console.log('点击事件:', params);
|
|
2007
|
+
add_chart_listener(chart, 'click', handler);
|
|
2008
|
+
// 稍后移除
|
|
2009
|
+
remove_chart_listener(chart, 'click', handler);
|
|
2010
|
+
```
|
|
2011
|
+
|
|
2012
|
+
#### export_chart_image
|
|
2013
|
+
- **功能**: 导出Echarts图表为图片
|
|
2014
|
+
- **参数**:
|
|
2015
|
+
- `chart: any` - Echarts实例
|
|
2016
|
+
- `options?: object` - 导出选项
|
|
2017
|
+
- **返回类型**: `string | null` - 图片URL
|
|
2018
|
+
- **示例**:
|
|
2019
|
+
```typescript
|
|
2020
|
+
import { export_chart_image } from '@qy_better_lib/core';
|
|
2021
|
+
|
|
2022
|
+
const imageUrl = export_chart_image(chart, {
|
|
2023
|
+
type: 'png',
|
|
2024
|
+
file_name: 'chart-image'
|
|
2025
|
+
});
|
|
2026
|
+
```
|
|
2027
|
+
|
|
2028
|
+
#### is_echarts_loaded
|
|
2029
|
+
- **功能**: 检查Echarts是否已加载
|
|
2030
|
+
- **返回类型**: `boolean` - 是否已加载
|
|
2031
|
+
- **示例**:
|
|
2032
|
+
```typescript
|
|
2033
|
+
import { is_echarts_loaded } from '@qy_better_lib/core';
|
|
2034
|
+
|
|
2035
|
+
const loaded = is_echarts_loaded();
|
|
2036
|
+
console.log(loaded);
|
|
2037
|
+
```
|
|
2038
|
+
|
|
2039
|
+
#### resize_charts
|
|
2040
|
+
- **功能**: 批量调整Echarts图表大小
|
|
2041
|
+
- **参数**: `charts: any[]` - 图表实例数组
|
|
2042
|
+
- **示例**:
|
|
2043
|
+
```typescript
|
|
2044
|
+
import { resize_charts } from '@qy_better_lib/core';
|
|
2045
|
+
|
|
2046
|
+
resize_charts([chart1, chart2]);
|
|
2047
|
+
```
|
|
2048
|
+
|
|
2049
|
+
#### destroy_charts
|
|
2050
|
+
- **功能**: 批量销毁Echarts图表
|
|
2051
|
+
- **参数**: `charts: any[]` - 图表实例数组
|
|
2052
|
+
- **示例**:
|
|
2053
|
+
```typescript
|
|
2054
|
+
import { destroy_charts } from '@qy_better_lib/core';
|
|
2055
|
+
|
|
2056
|
+
destroy_charts([chart1, chart2]);
|
|
2057
|
+
```
|
|
2058
|
+
|
|
2059
|
+
#### sort_chart_data
|
|
2060
|
+
- **功能**: 数据排序函数
|
|
2061
|
+
- **参数**:
|
|
2062
|
+
- `data: any[]` - 原始数据数组
|
|
2063
|
+
- `sort_key: string` - 排序字段名
|
|
2064
|
+
- `order: 'asc' | 'desc'` - 排序顺序,默认'asc'
|
|
2065
|
+
- **返回类型**: `any[]` - 排序后的数据数组
|
|
2066
|
+
- **示例**:
|
|
2067
|
+
```typescript
|
|
2068
|
+
import { sort_chart_data } from '@qy_better_lib/core';
|
|
2069
|
+
|
|
2070
|
+
const sortedData = sort_chart_data([{ value: 3 }, { value: 1 }, { value: 2 }], 'value');
|
|
2071
|
+
console.log(sortedData);
|
|
2072
|
+
```
|
|
2073
|
+
|
|
2074
|
+
#### group_chart_data
|
|
2075
|
+
- **功能**: 数据分组函数
|
|
2076
|
+
- **参数**:
|
|
2077
|
+
- `data: any[]` - 原始数据数组
|
|
2078
|
+
- `group_key: string` - 分组字段名
|
|
2079
|
+
- **返回类型**: `Record<string, any[]>` - 分组后的数据对象
|
|
2080
|
+
- **示例**:
|
|
2081
|
+
```typescript
|
|
2082
|
+
import { group_chart_data } from '@qy_better_lib/core';
|
|
2083
|
+
|
|
2084
|
+
const groupedData = group_chart_data([
|
|
2085
|
+
{ category: 'A', value: 1 },
|
|
2086
|
+
{ category: 'B', value: 2 },
|
|
2087
|
+
{ category: 'A', value: 3 }
|
|
2088
|
+
], 'category');
|
|
2089
|
+
console.log(groupedData);
|
|
2090
|
+
```
|
|
2091
|
+
|
|
2092
|
+
#### aggregate_chart_data
|
|
2093
|
+
- **功能**: 数据聚合函数
|
|
2094
|
+
- **参数**:
|
|
2095
|
+
- `data: any[]` - 原始数据数组
|
|
2096
|
+
- `group_key: string` - 分组字段名
|
|
2097
|
+
- `value_key: string` - 数值字段名
|
|
2098
|
+
- `aggregator: (values: number[]) => number` - 聚合函数,默认求和
|
|
2099
|
+
- **返回类型**: `any[]` - 聚合后的数据数组
|
|
2100
|
+
- **示例**:
|
|
2101
|
+
```typescript
|
|
2102
|
+
import { aggregate_chart_data } from '@qy_better_lib/core';
|
|
2103
|
+
|
|
2104
|
+
const aggregatedData = aggregate_chart_data([
|
|
2105
|
+
{ category: 'A', value: 1 },
|
|
2106
|
+
{ category: 'A', value: 3 },
|
|
2107
|
+
{ category: 'B', value: 2 }
|
|
2108
|
+
], 'category', 'value');
|
|
2109
|
+
console.log(aggregatedData);
|
|
2110
|
+
```
|
|
2111
|
+
|
|
2112
|
+
### 11. 文件工具 (file.ts)
|
|
2113
|
+
|
|
2114
|
+
#### download_file
|
|
2115
|
+
- **功能**: 下载文件
|
|
2116
|
+
- **参数**:
|
|
2117
|
+
- `path: string` - 文件路径
|
|
2118
|
+
- `name?: string` - 文件名
|
|
2119
|
+
- **示例**:
|
|
2120
|
+
```typescript
|
|
2121
|
+
import { download_file } from '@qy_better_lib/core';
|
|
2122
|
+
|
|
2123
|
+
download_file({ path: 'https://example.com/file.pdf', name: 'example.pdf' });
|
|
2124
|
+
```
|
|
2125
|
+
|
|
2126
|
+
#### download_files
|
|
2127
|
+
- **功能**: 批量下载文件
|
|
2128
|
+
- **参数**: `file_list: any[]` - 文件列表
|
|
2129
|
+
- **示例**:
|
|
2130
|
+
```typescript
|
|
2131
|
+
import { download_files } from '@qy_better_lib/core';
|
|
2132
|
+
|
|
2133
|
+
download_files([
|
|
2134
|
+
{ url: 'https://example.com/file1.pdf', name: 'file1.pdf' },
|
|
2135
|
+
{ url: 'https://example.com/file2.pdf', name: 'file2.pdf' }
|
|
2136
|
+
]);
|
|
2137
|
+
```
|
|
2138
|
+
|
|
2139
|
+
#### to_base64
|
|
2140
|
+
- **功能**: 将file转换成base64
|
|
2141
|
+
- **参数**: `file: Blob` - 文件对象
|
|
2142
|
+
- **返回类型**: `Promise<string | undefined>` - base64字符串
|
|
2143
|
+
- **示例**:
|
|
2144
|
+
```typescript
|
|
2145
|
+
import { to_base64 } from '@qy_better_lib/core';
|
|
2146
|
+
|
|
2147
|
+
async function convert() {
|
|
2148
|
+
const input = document.querySelector('input[type="file"]');
|
|
2149
|
+
if (input.files[0]) {
|
|
2150
|
+
const base64 = await to_base64(input.files[0]);
|
|
2151
|
+
console.log(base64);
|
|
2152
|
+
}
|
|
2153
|
+
}
|
|
2154
|
+
```
|
|
2155
|
+
|
|
2156
|
+
#### read_file
|
|
2157
|
+
- **功能**: 读取文件内容
|
|
2158
|
+
- **参数**:
|
|
2159
|
+
- `file: Blob` - 文件对象
|
|
2160
|
+
- `encoding: string` - 编码方式,默认为 UTF-8
|
|
2161
|
+
- **返回类型**: `Promise<string | undefined>` - 文件内容
|
|
2162
|
+
- **示例**:
|
|
2163
|
+
```typescript
|
|
2164
|
+
import { read_file } from '@qy_better_lib/core';
|
|
2165
|
+
|
|
2166
|
+
async function read() {
|
|
2167
|
+
const input = document.querySelector('input[type="file"]');
|
|
2168
|
+
if (input.files[0]) {
|
|
2169
|
+
const content = await read_file(input.files[0]);
|
|
2170
|
+
console.log(content);
|
|
2171
|
+
}
|
|
2172
|
+
}
|
|
2173
|
+
```
|
|
2174
|
+
|
|
2175
|
+
#### get_file_extension
|
|
2176
|
+
- **功能**: 获取文件扩展名
|
|
2177
|
+
- **参数**: `file_name: string` - 文件名
|
|
2178
|
+
- **返回类型**: `string` - 文件扩展名
|
|
2179
|
+
- **示例**:
|
|
2180
|
+
```typescript
|
|
2181
|
+
import { get_file_extension } from '@qy_better_lib/core';
|
|
2182
|
+
|
|
2183
|
+
const extension = get_file_extension('example.pdf');
|
|
2184
|
+
console.log(extension); // pdf
|
|
2185
|
+
```
|
|
2186
|
+
|
|
2187
|
+
#### get_file_name_without_extension
|
|
2188
|
+
- **功能**: 获取不带扩展名的文件名
|
|
2189
|
+
- **参数**: `file_name: string` - 文件名
|
|
2190
|
+
- **返回类型**: `string` - 不带扩展名的文件名
|
|
2191
|
+
- **示例**:
|
|
2192
|
+
```typescript
|
|
2193
|
+
import { get_file_name_without_extension } from '@qy_better_lib/core';
|
|
2194
|
+
|
|
2195
|
+
const name = get_file_name_without_extension('example.pdf');
|
|
2196
|
+
console.log(name); // example
|
|
2197
|
+
```
|
|
2198
|
+
|
|
2199
|
+
#### validate_file_size
|
|
2200
|
+
- **功能**: 验证文件大小
|
|
2201
|
+
- **参数**:
|
|
2202
|
+
- `file: File` - 文件对象
|
|
2203
|
+
- `max_size: number` - 最大文件大小(字节)
|
|
2204
|
+
- **返回类型**: `boolean` - 是否符合大小要求
|
|
2205
|
+
- **示例**:
|
|
2206
|
+
```typescript
|
|
2207
|
+
import { validate_file_size } from '@qy_better_lib/core';
|
|
2208
|
+
|
|
2209
|
+
const input = document.querySelector('input[type="file"]');
|
|
2210
|
+
if (input.files[0]) {
|
|
2211
|
+
const isValid = validate_file_size(input.files[0], 1024 * 1024); // 1MB
|
|
2212
|
+
console.log(isValid);
|
|
2213
|
+
}
|
|
2214
|
+
```
|
|
2215
|
+
|
|
2216
|
+
#### validate_file_type
|
|
2217
|
+
- **功能**: 验证文件类型
|
|
2218
|
+
- **参数**:
|
|
2219
|
+
- `file: File` - 文件对象
|
|
2220
|
+
- `allowed_types: string[]` - 允许的文件类型数组
|
|
2221
|
+
- **返回类型**: `boolean` - 是否符合类型要求
|
|
2222
|
+
- **示例**:
|
|
2223
|
+
```typescript
|
|
2224
|
+
import { validate_file_type } from '@qy_better_lib/core';
|
|
2225
|
+
|
|
2226
|
+
const input = document.querySelector('input[type="file"]');
|
|
2227
|
+
if (input.files[0]) {
|
|
2228
|
+
const isValid = validate_file_type(input.files[0], ['pdf', 'doc', 'docx']);
|
|
2229
|
+
console.log(isValid);
|
|
2230
|
+
}
|
|
2231
|
+
```
|
|
2232
|
+
|
|
2233
|
+
#### create_file_from_blob
|
|
2234
|
+
- **功能**: 从 Blob 创建文件
|
|
2235
|
+
- **参数**:
|
|
2236
|
+
- `blob: Blob` - Blob 对象
|
|
2237
|
+
- `file_name: string` - 文件名
|
|
2238
|
+
- `mime_type: string` - MIME 类型
|
|
2239
|
+
- **返回类型**: `File` - 文件对象
|
|
2240
|
+
- **示例**:
|
|
2241
|
+
```typescript
|
|
2242
|
+
import { create_file_from_blob } from '@qy_better_lib/core';
|
|
2243
|
+
|
|
2244
|
+
const blob = new Blob(['Hello World'], { type: 'text/plain' });
|
|
2245
|
+
const file = create_file_from_blob(blob, 'example.txt', 'text/plain');
|
|
2246
|
+
console.log(file);
|
|
2247
|
+
```
|
|
2248
|
+
|
|
2249
|
+
#### download_text_file
|
|
2250
|
+
- **功能**: 下载文本文件
|
|
2251
|
+
- **参数**:
|
|
2252
|
+
- `content: string` - 文件内容
|
|
2253
|
+
- `file_name: string` - 文件名
|
|
2254
|
+
- `mime_type: string` - MIME 类型,默认为 text/plain
|
|
2255
|
+
- **示例**:
|
|
2256
|
+
```typescript
|
|
2257
|
+
import { download_text_file } from '@qy_better_lib/core';
|
|
2258
|
+
|
|
2259
|
+
download_text_file('Hello World', 'example.txt');
|
|
2260
|
+
```
|
|
2261
|
+
|
|
2262
|
+
#### compress_image
|
|
2263
|
+
- **功能**: 压缩图片文件
|
|
2264
|
+
- **参数**:
|
|
2265
|
+
- `file: File` - 图片文件
|
|
2266
|
+
- `max_width: number` - 最大宽度,默认 800
|
|
2267
|
+
- `max_height: number` - 最大高度,默认 800
|
|
2268
|
+
- `quality: number` - 压缩质量,0-1之间,默认 0.8
|
|
2269
|
+
- **返回类型**: `Promise<File | undefined>` - 压缩后的文件
|
|
2270
|
+
- **示例**:
|
|
2271
|
+
```typescript
|
|
2272
|
+
import { compress_image } from '@qy_better_lib/core';
|
|
2273
|
+
|
|
2274
|
+
async function compress() {
|
|
2275
|
+
const input = document.querySelector('input[type="file"]');
|
|
2276
|
+
if (input.files[0]) {
|
|
2277
|
+
const compressedFile = await compress_image(input.files[0]);
|
|
2278
|
+
console.log(compressedFile);
|
|
2279
|
+
}
|
|
2280
|
+
}
|
|
2281
|
+
```
|
|
2282
|
+
|
|
2283
|
+
#### get_file_info
|
|
2284
|
+
- **功能**: 获取文件信息
|
|
2285
|
+
- **参数**: `file: File` - 文件对象
|
|
2286
|
+
- **返回类型**: `object` - 文件信息对象
|
|
2287
|
+
- **示例**:
|
|
2288
|
+
```typescript
|
|
2289
|
+
import { get_file_info } from '@qy_better_lib/core';
|
|
2290
|
+
|
|
2291
|
+
const input = document.querySelector('input[type="file"]');
|
|
2292
|
+
if (input.files[0]) {
|
|
2293
|
+
const info = get_file_info(input.files[0]);
|
|
2294
|
+
console.log(info);
|
|
2295
|
+
}
|
|
2296
|
+
```
|
|
2297
|
+
|
|
2298
|
+
#### generate_unique_file_name
|
|
2299
|
+
- **功能**: 生成唯一的文件名
|
|
2300
|
+
- **参数**: `original_name: string` - 原始文件名
|
|
2301
|
+
- **返回类型**: `string` - 唯一的文件名
|
|
2302
|
+
- **示例**:
|
|
2303
|
+
```typescript
|
|
2304
|
+
import { generate_unique_file_name } from '@qy_better_lib/core';
|
|
2305
|
+
|
|
2306
|
+
const uniqueName = generate_unique_file_name('example.pdf');
|
|
2307
|
+
console.log(uniqueName);
|
|
2308
|
+
```
|
|
2309
|
+
|
|
2310
|
+
### 12. 树形数据工具 (tree.ts)
|
|
2311
|
+
|
|
2312
|
+
#### tree_flat
|
|
2313
|
+
- **功能**: 树形数据扁平化处理
|
|
2314
|
+
- **参数**:
|
|
2315
|
+
- `data: TreeNode<T>[]` - 树形数据数组
|
|
2316
|
+
- `key: string` - key属性,默认"key"
|
|
2317
|
+
- `children: string` - children属性,默认"children"
|
|
2318
|
+
- **返回类型**: `FlatTreeMap<T>` - 扁平化的树形数据映射
|
|
2319
|
+
- **示例**:
|
|
2320
|
+
```typescript
|
|
2321
|
+
import { tree_flat } from '@qy_better_lib/core';
|
|
2322
|
+
|
|
2323
|
+
const treeData = [
|
|
2324
|
+
{ key: '1', name: '节点1', children: [{ key: '1-1', name: '节点1-1' }] }
|
|
2325
|
+
];
|
|
2326
|
+
const flatMap = tree_flat(treeData);
|
|
2327
|
+
console.log(flatMap);
|
|
2328
|
+
```
|
|
2329
|
+
|
|
2330
|
+
#### build_tree
|
|
2331
|
+
- **功能**: 从扁平化数据构建树形结构
|
|
2332
|
+
- **参数**:
|
|
2333
|
+
- `data: TreeNode<T>[]` - 扁平化数据数组
|
|
2334
|
+
- `key: string` - key属性,默认"key"
|
|
2335
|
+
- `parent_key: string` - 父节点key属性,默认"parentId"
|
|
2336
|
+
- `children: string` - children属性,默认"children"
|
|
2337
|
+
- **返回类型**: `TreeNode<T>[]` - 树形数据数组
|
|
2338
|
+
- **示例**:
|
|
2339
|
+
```typescript
|
|
2340
|
+
import { build_tree } from '@qy_better_lib/core';
|
|
2341
|
+
|
|
2342
|
+
const flatData = [
|
|
2343
|
+
{ key: '1', name: '节点1' },
|
|
2344
|
+
{ key: '1-1', name: '节点1-1', parentId: '1' }
|
|
2345
|
+
];
|
|
2346
|
+
const treeData = build_tree(flatData);
|
|
2347
|
+
console.log(treeData);
|
|
2348
|
+
```
|
|
2349
|
+
|
|
2350
|
+
#### traverse_tree_depth_first
|
|
2351
|
+
- **功能**: 深度优先遍历树形数据
|
|
2352
|
+
- **参数**:
|
|
2353
|
+
- `tree: TreeNode<T> | TreeNode<T>[]` - 树形数据
|
|
2354
|
+
- `callback: (node: TreeNode<T>, depth: number) => boolean | void` - 回调函数,返回false可中断遍历
|
|
2355
|
+
- `children: string` - children属性,默认"children"
|
|
2356
|
+
- **返回类型**: `boolean` - 是否提前中断了遍历
|
|
2357
|
+
- **示例**:
|
|
2358
|
+
```typescript
|
|
2359
|
+
import { traverse_tree_depth_first } from '@qy_better_lib/core';
|
|
2360
|
+
|
|
2361
|
+
const treeData = [
|
|
2362
|
+
{ key: '1', name: '节点1', children: [{ key: '1-1', name: '节点1-1' }] }
|
|
2363
|
+
];
|
|
2364
|
+
traverse_tree_depth_first(treeData, (node, depth) => {
|
|
2365
|
+
console.log(`${' '.repeat(depth)}${node.name}`);
|
|
2366
|
+
});
|
|
2367
|
+
```
|
|
2368
|
+
|
|
2369
|
+
#### traverse_tree_breadth_first
|
|
2370
|
+
- **功能**: 广度优先遍历树形数据
|
|
2371
|
+
- **参数**:
|
|
2372
|
+
- `tree: TreeNode<T> | TreeNode<T>[]` - 树形数据
|
|
2373
|
+
- `callback: (node: TreeNode<T>, depth: number) => boolean | void` - 回调函数,返回false可中断遍历
|
|
2374
|
+
- `children: string` - children属性,默认"children"
|
|
2375
|
+
- **返回类型**: `boolean` - 是否提前中断了遍历
|
|
2376
|
+
- **示例**:
|
|
2377
|
+
```typescript
|
|
2378
|
+
import { traverse_tree_breadth_first } from '@qy_better_lib/core';
|
|
2379
|
+
|
|
2380
|
+
const treeData = [
|
|
2381
|
+
{ key: '1', name: '节点1', children: [{ key: '1-1', name: '节点1-1' }] }
|
|
2382
|
+
];
|
|
2383
|
+
traverse_tree_breadth_first(treeData, (node, depth) => {
|
|
2384
|
+
console.log(`${' '.repeat(depth)}${node.name}`);
|
|
2385
|
+
});
|
|
2386
|
+
```
|
|
2387
|
+
|
|
2388
|
+
#### find_tree_node
|
|
2389
|
+
- **功能**: 根据条件查找树形节点
|
|
2390
|
+
- **参数**:
|
|
2391
|
+
- `tree: TreeNode<T> | TreeNode<T>[]` - 树形数据
|
|
2392
|
+
- `predicate: (node: TreeNode<T>) => boolean` - 条件函数
|
|
2393
|
+
- `children: string` - children属性,默认"children"
|
|
2394
|
+
- **返回类型**: `TreeNode<T> | null` - 找到的节点或null
|
|
2395
|
+
- **示例**:
|
|
2396
|
+
```typescript
|
|
2397
|
+
import { find_tree_node } from '@qy_better_lib/core';
|
|
2398
|
+
|
|
2399
|
+
const treeData = [
|
|
2400
|
+
{ key: '1', name: '节点1', children: [{ key: '1-1', name: '节点1-1' }] }
|
|
2401
|
+
];
|
|
2402
|
+
const node = find_tree_node(treeData, node => node.key === '1-1');
|
|
2403
|
+
console.log(node);
|
|
2404
|
+
```
|
|
2405
|
+
|
|
2406
|
+
#### find_tree_node_by_key
|
|
2407
|
+
- **功能**: 根据key查找树形节点
|
|
2408
|
+
- **参数**:
|
|
2409
|
+
- `tree: TreeNode<T> | TreeNode<T>[]` - 树形数据
|
|
2410
|
+
- `key_value: any` - key值
|
|
2411
|
+
- `key: string` - key属性,默认"key"
|
|
2412
|
+
- `children: string` - children属性,默认"children"
|
|
2413
|
+
- **返回类型**: `TreeNode<T> | null` - 找到的节点或null
|
|
2414
|
+
- **示例**:
|
|
2415
|
+
```typescript
|
|
2416
|
+
import { find_tree_node_by_key } from '@qy_better_lib/core';
|
|
2417
|
+
|
|
2418
|
+
const treeData = [
|
|
2419
|
+
{ key: '1', name: '节点1', children: [{ key: '1-1', name: '节点1-1' }] }
|
|
2420
|
+
];
|
|
2421
|
+
const node = find_tree_node_by_key(treeData, '1-1');
|
|
2422
|
+
console.log(node);
|
|
2423
|
+
```
|
|
2424
|
+
|
|
2425
|
+
#### filter_tree
|
|
2426
|
+
- **功能**: 过滤树形数据
|
|
2427
|
+
- **参数**:
|
|
2428
|
+
- `tree: TreeNode<T> | TreeNode<T>[]` - 树形数据
|
|
2429
|
+
- `predicate: (node: TreeNode<T>) => boolean` - 条件函数
|
|
2430
|
+
- `children: string` - children属性,默认"children"
|
|
2431
|
+
- **返回类型**: `TreeNode<T>[]` - 过滤后的树形数据
|
|
2432
|
+
- **示例**:
|
|
2433
|
+
```typescript
|
|
2434
|
+
import { filter_tree } from '@qy_better_lib/core';
|
|
2435
|
+
|
|
2436
|
+
const treeData = [
|
|
2437
|
+
{ key: '1', name: '节点1', value: 10 },
|
|
2438
|
+
{ key: '2', name: '节点2', value: 20 }
|
|
2439
|
+
];
|
|
2440
|
+
const filteredData = filter_tree(treeData, node => node.value > 15);
|
|
2441
|
+
console.log(filteredData);
|
|
2442
|
+
```
|
|
2443
|
+
|
|
2444
|
+
#### transform_tree
|
|
2445
|
+
- **功能**: 转换树形数据结构
|
|
2446
|
+
- **参数**:
|
|
2447
|
+
- `tree: TreeNode<T> | TreeNode<T>[]` - 树形数据
|
|
2448
|
+
- `transform: (node: TreeNode<T>) => TreeNode<T>` - 转换函数
|
|
2449
|
+
- `children: string` - children属性,默认"children"
|
|
2450
|
+
- **返回类型**: `TreeNode<T>[]` - 转换后的树形数据
|
|
2451
|
+
- **示例**:
|
|
2452
|
+
```typescript
|
|
2453
|
+
import { transform_tree } from '@qy_better_lib/core';
|
|
2454
|
+
|
|
2455
|
+
const treeData = [
|
|
2456
|
+
{ key: '1', name: '节点1' }
|
|
2457
|
+
];
|
|
2458
|
+
const transformedData = transform_tree(treeData, node => ({
|
|
2459
|
+
...node,
|
|
2460
|
+
newProperty: 'value'
|
|
2461
|
+
}));
|
|
2462
|
+
console.log(transformedData);
|
|
2463
|
+
```
|
|
2464
|
+
|
|
2465
|
+
#### add_tree_node_path
|
|
2466
|
+
- **功能**: 为树形节点添加路径信息
|
|
2467
|
+
- **参数**:
|
|
2468
|
+
- `tree: TreeNode<T> | TreeNode<T>[]` - 树形数据
|
|
2469
|
+
- `key: string` - key属性,默认"key"
|
|
2470
|
+
- `path: string` - path属性,默认"path"
|
|
2471
|
+
- `children: string` - children属性,默认"children"
|
|
2472
|
+
- **返回类型**: `TreeNode<T>[]` - 添加了路径信息的树形数据
|
|
2473
|
+
- **示例**:
|
|
2474
|
+
```typescript
|
|
2475
|
+
import { add_tree_node_path } from '@qy_better_lib/core';
|
|
2476
|
+
|
|
2477
|
+
const treeData = [
|
|
2478
|
+
{ key: '1', name: '节点1', children: [{ key: '1-1', name: '节点1-1' }] }
|
|
2479
|
+
];
|
|
2480
|
+
const dataWithPath = add_tree_node_path(treeData);
|
|
2481
|
+
console.log(dataWithPath);
|
|
2482
|
+
```
|
|
2483
|
+
|
|
2484
|
+
#### get_tree_node_descendants
|
|
2485
|
+
- **功能**: 获取树形节点的所有后代
|
|
2486
|
+
- **参数**:
|
|
2487
|
+
- `node: TreeNode<T>` - 树形节点
|
|
2488
|
+
- `children: string` - children属性,默认"children"
|
|
2489
|
+
- **返回类型**: `TreeNode<T>[]` - 所有后代节点数组
|
|
2490
|
+
- **示例**:
|
|
2491
|
+
```typescript
|
|
2492
|
+
import { get_tree_node_descendants } from '@qy_better_lib/core';
|
|
2493
|
+
|
|
2494
|
+
const node = { key: '1', name: '节点1', children: [{ key: '1-1', name: '节点1-1' }] };
|
|
2495
|
+
const descendants = get_tree_node_descendants(node);
|
|
2496
|
+
console.log(descendants);
|
|
2497
|
+
```
|
|
2498
|
+
|
|
2499
|
+
#### get_tree_node_ancestors
|
|
2500
|
+
- **功能**: 获取树形节点的所有祖先
|
|
2501
|
+
- **参数**:
|
|
2502
|
+
- `node: TreeNode<T>` - 树形节点
|
|
2503
|
+
- `parent: string` - parent属性,默认"parent"
|
|
2504
|
+
- **返回类型**: `TreeNode<T>[]` - 所有祖先节点数组
|
|
2505
|
+
- **示例**:
|
|
2506
|
+
```typescript
|
|
2507
|
+
import { get_tree_node_ancestors } from '@qy_better_lib/core';
|
|
2508
|
+
|
|
2509
|
+
const parentNode = { key: '1', name: '节点1' };
|
|
2510
|
+
const childNode = { key: '1-1', name: '节点1-1', parent: parentNode };
|
|
2511
|
+
const ancestors = get_tree_node_ancestors(childNode);
|
|
2512
|
+
console.log(ancestors);
|
|
2513
|
+
```
|
|
2514
|
+
|
|
2515
|
+
#### get_tree_depth
|
|
2516
|
+
- **功能**: 计算树形数据的深度
|
|
2517
|
+
- **参数**:
|
|
2518
|
+
- `tree: TreeNode<T> | TreeNode<T>[]` - 树形数据
|
|
2519
|
+
- `children: string` - children属性,默认"children"
|
|
2520
|
+
- **返回类型**: `number` - 树形数据的深度
|
|
2521
|
+
- **示例**:
|
|
2522
|
+
```typescript
|
|
2523
|
+
import { get_tree_depth } from '@qy_better_lib/core';
|
|
2524
|
+
|
|
2525
|
+
const treeData = [
|
|
2526
|
+
{ key: '1', name: '节点1', children: [{ key: '1-1', name: '节点1-1' }] }
|
|
2527
|
+
];
|
|
2528
|
+
const depth = get_tree_depth(treeData);
|
|
2529
|
+
console.log(depth); // 2
|
|
2530
|
+
```
|
|
2531
|
+
|
|
2532
|
+
#### flatten_tree_to_array
|
|
2533
|
+
- **功能**: 扁平化树形数据为数组
|
|
2534
|
+
- **参数**:
|
|
2535
|
+
- `tree: TreeNode<T> | TreeNode<T>[]` - 树形数据
|
|
2536
|
+
- `children: string` - children属性,默认"children"
|
|
2537
|
+
- **返回类型**: `TreeNode<T>[]` - 扁平化的节点数组
|
|
2538
|
+
- **示例**:
|
|
2539
|
+
```typescript
|
|
2540
|
+
import { flatten_tree_to_array } from '@qy_better_lib/core';
|
|
2541
|
+
|
|
2542
|
+
const treeData = [
|
|
2543
|
+
{ key: '1', name: '节点1', children: [{ key: '1-1', name: '节点1-1' }] }
|
|
2544
|
+
];
|
|
2545
|
+
const flatArray = flatten_tree_to_array(treeData);
|
|
2546
|
+
console.log(flatArray);
|
|
2547
|
+
```
|
|
2548
|
+
|
|
2549
|
+
#### add_tree_node
|
|
2550
|
+
- **功能**: 向树形结构添加节点
|
|
2551
|
+
- **参数**:
|
|
2552
|
+
- `tree: TreeNode<T> | TreeNode<T>[]` - 树形数据
|
|
2553
|
+
- `parent_key: any` - 父节点key值
|
|
2554
|
+
- `new_node: TreeNode<T>` - 新节点
|
|
2555
|
+
- `key: string` - key属性,默认"key"
|
|
2556
|
+
- `children: string` - children属性,默认"children"
|
|
2557
|
+
- **返回类型**: `TreeNode<T>[]` - 更新后的树形数据
|
|
2558
|
+
- **示例**:
|
|
2559
|
+
```typescript
|
|
2560
|
+
import { add_tree_node } from '@qy_better_lib/core';
|
|
2561
|
+
|
|
2562
|
+
const treeData = [
|
|
2563
|
+
{ key: '1', name: '节点1' }
|
|
2564
|
+
];
|
|
2565
|
+
const newTreeData = add_tree_node(treeData, '1', { key: '1-1', name: '节点1-1' });
|
|
2566
|
+
console.log(newTreeData);
|
|
2567
|
+
```
|
|
2568
|
+
|
|
2569
|
+
#### remove_tree_node
|
|
2570
|
+
- **功能**: 从树形结构移除节点
|
|
2571
|
+
- **参数**:
|
|
2572
|
+
- `tree: TreeNode<T> | TreeNode<T>[]` - 树形数据
|
|
2573
|
+
- `node_key: any` - 要移除的节点key值
|
|
2574
|
+
- `key: string` - key属性,默认"key"
|
|
2575
|
+
- `children: string` - children属性,默认"children"
|
|
2576
|
+
- **返回类型**: `TreeNode<T>[]` - 更新后的树形数据
|
|
2577
|
+
- **示例**:
|
|
2578
|
+
```typescript
|
|
2579
|
+
import { remove_tree_node } from '@qy_better_lib/core';
|
|
2580
|
+
|
|
2581
|
+
const treeData = [
|
|
2582
|
+
{ key: '1', name: '节点1', children: [{ key: '1-1', name: '节点1-1' }] }
|
|
2583
|
+
];
|
|
2584
|
+
const newTreeData = remove_tree_node(treeData, '1-1');
|
|
2585
|
+
console.log(newTreeData);
|
|
2586
|
+
```
|
|
2587
|
+
|
|
2588
|
+
#### update_tree_node
|
|
2589
|
+
- **功能**: 更新树形结构中的节点
|
|
2590
|
+
- **参数**:
|
|
2591
|
+
- `tree: TreeNode<T> | TreeNode<T>[]` - 树形数据
|
|
2592
|
+
- `node_key: any` - 要更新的节点key值
|
|
2593
|
+
- `updates: Partial<TreeNode<T>>` - 更新内容
|
|
2594
|
+
- `key: string` - key属性,默认"key"
|
|
2595
|
+
- `children: string` - children属性,默认"children"
|
|
2596
|
+
- **返回类型**: `TreeNode<T>[]` - 更新后的树形数据
|
|
2597
|
+
- **示例**:
|
|
2598
|
+
```typescript
|
|
2599
|
+
import { update_tree_node } from '@qy_better_lib/core';
|
|
2600
|
+
|
|
2601
|
+
const treeData = [
|
|
2602
|
+
{ key: '1', name: '节点1' }
|
|
2603
|
+
];
|
|
2604
|
+
const newTreeData = update_tree_node(treeData, '1', { name: '更新后的节点1' });
|
|
2605
|
+
console.log(newTreeData);
|
|
2606
|
+
```
|
|
2607
|
+
|
|
2608
|
+
#### sort_tree
|
|
2609
|
+
- **功能**: 对树形结构进行排序
|
|
2610
|
+
- **参数**:
|
|
2611
|
+
- `tree: TreeNode<T> | TreeNode<T>[]` - 树形数据
|
|
2612
|
+
- `compare_fn: ((a: TreeNode<T>, b: TreeNode<T>) => number) | string` - 比较函数或属性名
|
|
2613
|
+
- `recursive: boolean` - 是否递归排序所有层级,默认true
|
|
2614
|
+
- `children: string` - children属性,默认"children"
|
|
2615
|
+
- **返回类型**: `TreeNode<T>[]` - 排序后的树形数据
|
|
2616
|
+
- **示例**:
|
|
2617
|
+
```typescript
|
|
2618
|
+
import { sort_tree } from '@qy_better_lib/core';
|
|
2619
|
+
|
|
2620
|
+
const treeData = [
|
|
2621
|
+
{ key: '2', name: '节点2' },
|
|
2622
|
+
{ key: '1', name: '节点1' }
|
|
2623
|
+
];
|
|
2624
|
+
const sortedData = sort_tree(treeData, 'key');
|
|
2625
|
+
console.log(sortedData);
|
|
2626
|
+
```
|
|
2627
|
+
|
|
2628
|
+
#### clone_tree
|
|
2629
|
+
- **功能**: 克隆树形结构
|
|
2630
|
+
- **参数**:
|
|
2631
|
+
- `tree: TreeNode<T> | TreeNode<T>[]` - 树形数据
|
|
2632
|
+
- `children: string` - children属性,默认"children"
|
|
2633
|
+
- **返回类型**: `TreeNode<T>[]` - 克隆后的树形数据
|
|
2634
|
+
- **示例**:
|
|
2635
|
+
```typescript
|
|
2636
|
+
import { clone_tree } from '@qy_better_lib/core';
|
|
2637
|
+
|
|
2638
|
+
const treeData = [
|
|
2639
|
+
{ key: '1', name: '节点1' }
|
|
2640
|
+
];
|
|
2641
|
+
const clonedData = clone_tree(treeData);
|
|
2642
|
+
console.log(clonedData);
|
|
2643
|
+
```
|
|
2644
|
+
|
|
2645
|
+
#### validate_tree
|
|
2646
|
+
- **功能**: 验证树形结构的有效性
|
|
2647
|
+
- **参数**:
|
|
2648
|
+
- `tree: TreeNode<T> | TreeNode<T>[]` - 树形数据
|
|
2649
|
+
- `key: string` - key属性,默认"key"
|
|
2650
|
+
- `children: string` - children属性,默认"children"
|
|
2651
|
+
- **返回类型**: `{ valid: boolean; errors: string[] }` - 验证结果
|
|
2652
|
+
- **示例**:
|
|
2653
|
+
```typescript
|
|
2654
|
+
import { validate_tree } from '@qy_better_lib/core';
|
|
2655
|
+
|
|
2656
|
+
const treeData = [
|
|
2657
|
+
{ key: '1', name: '节点1' }
|
|
2658
|
+
];
|
|
2659
|
+
const result = validate_tree(treeData);
|
|
2660
|
+
console.log(result);
|
|
2661
|
+
```
|
|
2662
|
+
|
|
2663
|
+
#### search_tree
|
|
2664
|
+
- **功能**: 在树形结构中搜索节点
|
|
2665
|
+
- **参数**:
|
|
2666
|
+
- `tree: TreeNode<T> | TreeNode<T>[]` - 树形数据
|
|
2667
|
+
- `predicate: ((node: TreeNode<T>) => boolean) | Record<string, any>` - 搜索条件函数或属性值对
|
|
2668
|
+
- `children: string` - children属性,默认"children"
|
|
2669
|
+
- **返回类型**: `TreeNode<T>[]` - 所有匹配的节点数组
|
|
2670
|
+
- **示例**:
|
|
2671
|
+
```typescript
|
|
2672
|
+
import { search_tree } from '@qy_better_lib/core';
|
|
2673
|
+
|
|
2674
|
+
const treeData = [
|
|
2675
|
+
{ key: '1', name: '节点1', value: 10 },
|
|
2676
|
+
{ key: '2', name: '节点2', value: 20 }
|
|
2677
|
+
];
|
|
2678
|
+
const results = search_tree(treeData, { value: 10 });
|
|
2679
|
+
console.log(results);
|
|
2680
|
+
```
|
|
2681
|
+
|