utils-lib-js 2.0.5 → 2.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.en.md +579 -414
  2. package/README.md +566 -415
  3. package/dist/bundle/animate.d.ts +25 -0
  4. package/dist/bundle/array.d.ts +10 -0
  5. package/dist/bundle/base.d.ts +15 -0
  6. package/dist/bundle/element.d.ts +6 -0
  7. package/dist/bundle/event.d.ts +14 -0
  8. package/dist/bundle/function.d.ts +12 -0
  9. package/dist/bundle/index.d.ts +67 -0
  10. package/dist/bundle/index.js +7 -0
  11. package/dist/bundle/log.d.ts +8 -0
  12. package/dist/bundle/object.d.ts +32 -0
  13. package/dist/bundle/static.d.ts +16 -0
  14. package/dist/bundle/storage.d.ts +9 -0
  15. package/dist/bundle/types.d.ts +68 -0
  16. package/dist/cjs/animate.d.ts +25 -0
  17. package/dist/cjs/array.d.ts +10 -0
  18. package/dist/cjs/base.d.ts +15 -0
  19. package/dist/cjs/element.d.ts +6 -0
  20. package/dist/cjs/event.d.ts +14 -0
  21. package/dist/cjs/function.d.ts +12 -0
  22. package/dist/cjs/index.d.ts +67 -0
  23. package/dist/cjs/index.js +3021 -0
  24. package/dist/cjs/log.d.ts +8 -0
  25. package/dist/cjs/object.d.ts +32 -0
  26. package/dist/cjs/package.json +3 -0
  27. package/dist/cjs/static.d.ts +16 -0
  28. package/dist/cjs/storage.d.ts +9 -0
  29. package/dist/cjs/types.d.ts +68 -0
  30. package/dist/esm/animate.d.ts +25 -0
  31. package/dist/esm/array.d.ts +10 -0
  32. package/dist/esm/base.d.ts +15 -0
  33. package/dist/esm/element.d.ts +6 -0
  34. package/dist/esm/event.d.ts +14 -0
  35. package/dist/esm/function.d.ts +12 -0
  36. package/dist/esm/index.d.ts +67 -0
  37. package/dist/esm/index.js +2968 -0
  38. package/dist/esm/log.d.ts +8 -0
  39. package/dist/esm/object.d.ts +32 -0
  40. package/dist/esm/static.d.ts +16 -0
  41. package/dist/esm/storage.d.ts +9 -0
  42. package/dist/esm/types.d.ts +68 -0
  43. package/dist/umd/animate.d.ts +25 -0
  44. package/dist/umd/array.d.ts +10 -0
  45. package/dist/umd/base.d.ts +15 -0
  46. package/dist/umd/element.d.ts +6 -0
  47. package/dist/umd/event.d.ts +14 -0
  48. package/dist/umd/function.d.ts +12 -0
  49. package/dist/umd/index.d.ts +67 -0
  50. package/dist/umd/index.js +3023 -0
  51. package/dist/umd/log.d.ts +8 -0
  52. package/dist/umd/object.d.ts +32 -0
  53. package/dist/umd/static.d.ts +16 -0
  54. package/dist/umd/storage.d.ts +9 -0
  55. package/dist/umd/types.d.ts +68 -0
  56. package/package.json +49 -49
  57. package/pnpm-lock.yaml +0 -450
package/README.md CHANGED
@@ -1,540 +1,691 @@
1
1
  # utils-lib-js
2
2
 
3
- #### 介绍
4
- JavaScript工具函数,封装的一些常用的js函数
3
+ ## 介绍
5
4
 
6
- #### 软件架构
7
- 软件架构说明
5
+ JavaScript 工具函数,封装的一些常用的 js 函数
8
6
 
9
-
10
- #### 安装教程
11
-
12
- 1. pnpm i
13
- 2. pnpm build
14
-
15
- #### 使用说明
7
+ ## 调试说明
16
8
 
17
9
  1. pnpm build(构建)
18
10
  2. pnpm debug(调试源码)
11
+ 3. 在 esm,cjs,window 环境下导入该工具库
19
12
 
20
- #### 参与贡献
13
+ ## 参与贡献
21
14
 
22
15
  1. Fork 本仓库
23
- 2. 新建 Feat_xxx 分支
16
+ 2. Star 仓库
24
17
  3. 提交代码
25
18
  4. 新建 Pull Request
26
19
 
27
- #### 功能
28
- 接口:
20
+ ## 使用说明
29
21
 
30
- export type IKey = string | symbol | number
22
+ ### 安装
31
23
 
32
- ##### 对象类型
24
+ `npm install utils-lib-js`
25
+
26
+ `yarn add utils-lib-js`
27
+
28
+ `pnpm install utils-lib-js`
33
29
 
34
- export interface IObject<T> {
35
- [key: IKey]: T | IObject<any>
36
- }
30
+ ### 引入
37
31
 
38
- export interface IPromise extends IObject<any> {
39
- promise: Promise<void>
40
- resolve: (res: any) => unknown
41
- reject: (err: any) => unknown
42
- }
32
+ #### ESM
33
+
34
+ ```javascript
35
+ import { defer, messageCenter, TaskQueue, Request } from "utils-lib-js";
36
+ ```
37
+
38
+ #### CJS
39
+
40
+ ```javascript
41
+ const { defer, messageCenter, TaskQueue } = require("utils-lib-js");
42
+ ```
43
+
44
+ #### 浏览器中
45
+
46
+ ```html
47
+ <script src="./node_modules/utils-lib-js/dist/umd/index.js"></script>
48
+ <script>
49
+ console.log(UtilsLib);
50
+ </script>
51
+ ```
52
+
53
+ ### TS 接口注释
54
+
55
+ 参照 https://gitee.com/DieHunter/utils-lib-js/blob/master/src/types.ts
56
+
57
+ ### 代码演示
58
+
59
+ #### base 模块
43
60
 
44
- export type IInstance<T> = {
45
- _instance: Function
46
- } & T
61
+ ##### 1. `randomNum(min: number, max: number, bool?: boolean): number`
47
62
 
48
- export type IDemoteArray<T> = Array<IDemoteArray<T> | T>
63
+ 生成指定范围内的随机整数。
49
64
 
50
- ##### base
65
+ - `min`: 最小值(包含)。
66
+ - `max`: 最大值(包含)。
67
+ - `bool`: 是否包含最大值,默认为 `false`。
51
68
 
52
- /**产生区间随机数
53
- * @param {number} min 最小区间
54
- * @param {number} max 最大区间
55
- * @param {boolean} bool 包含最大值
56
- * @return {number} 随机数
57
- **/
69
+ ```javascript
70
+ const randomNumber = randomNum(1, 10);
71
+ console.log(randomNumber); // 生成一个介于 1 到 10 之间的随机整数
72
+ ```
58
73
 
59
- export type IRandomNum = (min: number, max: number, bool?: boolean) => number
74
+ ##### 2. `urlSplit(url: string): object`
60
75
 
61
- /**获取url的参数
62
- * @param {string} url 待截取的地址
63
- * @return {object} 参数对象
64
- **/
76
+ 将 URL 查询参数解析为对象。
65
77
 
66
- export type IUrlSplit = (url: string) => IObject<any>
78
+ - `url`: 包含查询参数的 URL。
67
79
 
68
- /**添加url的参数
69
- * @param {string} url 待添加参数的地址
70
- * @param {object} query 待添加的参数
71
- * @return {string} 添加参数后的url
72
- **/
80
+ ```javascript
81
+ const url = "https://example.com/path?param1=value1&param2=value2";
82
+ const params = urlSplit(url);
83
+ console.log(params);
84
+ // 输出: { param1: 'value1', param2: 'value2' }
85
+ ```
73
86
 
74
- export type IUrlJoin = (url: string, query: object) => string
87
+ ##### 3. `urlJoin(url: string, query: object): string`
75
88
 
76
- /**获取数据类型
77
- * @param {any} data 待检测数据
78
- * @return {string} 数据类型
79
- **/
89
+ 将对象转换为 URL 查询参数,并与原始 URL 连接。
80
90
 
81
- export type IGetType<T> = (data: any) => typeof data | T[keyof T] | "null";
91
+ - `url`: 原始 URL。
92
+ - `query`: 包含查询参数的对象。
82
93
 
83
- /**批量判断数据类型
84
- * @param {any} data 待检测数据
85
- * @param {any} whiteList 数据类型名单
86
- * @return {boolean} 是否在白名单中
87
- **/
94
+ ```javascript
95
+ const url = "https://example.com/path";
96
+ const query = { param1: "value1", param2: "value2" };
97
+ const newUrl = urlJoin(url, query);
98
+ console.log(newUrl);
99
+ // 输出: https://example.com/path?param1=value1&param2=value2
100
+ ```
88
101
 
89
- export type IGetTypeByList = (data: any, whiteList: string[]) => boolean;
102
+ ##### 4. `getType(data: any): string`
90
103
 
104
+ 获取数据的类型。
91
105
 
92
- ##### object
106
+ - `data`: 要检查的数据。
93
107
 
94
- /**lodash中的 _.get(),获取对象某级属性
95
- * @param {IObject} object 目标对象
96
- * @param {string} key 对象层级
97
- * @param {any} defaultValue 未取得时默认值
98
- * @return {IObject[IKey]} 对象某个属性
99
- **/
108
+ ```javascript
109
+ const dataType = getType("Hello, World!");
110
+ console.log(dataType); // 输出: string
111
+ ```
100
112
 
101
- export type IGetValue = <T, U = IObject<T> | IObject<T>[IKey]>(object: U, key: string, defaultValue?: any) => U
113
+ ##### 5. `getTypeByList(data: any, whiteList: string[]): boolean`
102
114
 
103
- /**lodash中的 _.set(),赋值对象某级属性
104
- * @param {IObject} object 目标对象
105
- * @param {string} key 对象层级
106
- * @param {any} value 需要赋的值
107
- * @return {IObject} 目标对象
108
- **/
115
+ 检查数据类型是否在白名单内。
109
116
 
110
- export type ISetValue = <T>(object: IObject<T>, key: string, value?: any) => IObject<T>
117
+ - `data`: 要检查的数据。
118
+ - `whiteList`: 允许的数据类型列表。
111
119
 
112
- /**对象混入
113
- * @param {IObject} target 目标对象
114
- * @param {string} source 需要混入的对象集合
115
- * @param {boolean} overwrite 是否重写覆盖原有属性
116
- * @return {IObject} 目标对象
117
- **/
120
+ ```javascript
121
+ const data = 42;
122
+ const allowedTypes = ["number", "string"];
123
+ const isTypeAllowed = getTypeByList(data, allowedTypes);
124
+ console.log(isTypeAllowed); // 输出: true
125
+ ```
118
126
 
119
- export type IMixIn = <U extends IObject<any>>(target?: U, source?: IObject<any>, overwrite?: boolean) => U
127
+ #### object 模块
120
128
 
121
- /**枚举值反向映射
122
- * @param {IObject<string>} target 目标对象
123
- * @return {IObject<string>} 目标对象
124
- **/
129
+ ##### 1. `getValue(object: object, key: string, defaultValue: any = ''): any`
125
130
 
126
- export type IEnumInversion = (target: IObject<string>) => IObject<string>
131
+ 获取对象中指定路径的值。
127
132
 
128
- /**对象复制
129
- * @param {IObject<any>} target 目标对象
130
- * @return {IObject} 目标对象
131
- **/
133
+ - `object`: 目标对象。
134
+ - `key`: 要获取的值的路径。
135
+ - `defaultValue`: 默认值,当路径不存在时返回。
132
136
 
133
- export type ICloneDeep = (target?: any) => any
137
+ ```javascript
138
+ const obj = { a: { b: { c: 42 } } };
139
+ const value = getValue(obj, "a.b.c", "default");
140
+ console.log(value); // 输出: 42
141
+ ```
134
142
 
135
- /**生成 对象 类型的初始值
136
- * @param {string} type 数据类型
137
- * @param {any} __init 初始值
138
- * @return {any} 目标对象
139
- **/
143
+ ##### 2. `setValue(object: object, key: string, value: any = {}): object`
140
144
 
141
- export type ICreateObjectVariable = (type: string, source?: any) => any
145
+ 设置对象中指定路径的值。
142
146
 
143
- /**Object.create 根据源对象产出新对象
144
- * @param {Function|Object} source 源对象
145
- * @return {Function|Object} 对象产物
146
- **/
147
+ - `object`: 目标对象。
148
+ - `key`: 要设置的值的路径。
149
+ - `value`: 要设置的值。
147
150
 
148
- export type ICreateObject = <T, U extends T>(source: T) => U
151
+ ```javascript
152
+ const obj = { a: { b: { c: 42 } } };
153
+ setValue(obj, "a.b.d", "new value");
154
+ console.log(obj); // 输出: { a: { b: { c: 42, d: 'new value' } } }
155
+ ```
149
156
 
150
- /**类的继承
151
- * @param {Function} source 源对象
152
- * @return {Function} 继承产物
153
- **/
157
+ ##### 3. `mixIn(target: object, source: object = {}, overwrite: boolean = false): object`
154
158
 
155
- export type IInherit = <T extends Function>(source: T, target?: Function) => Function
159
+ 将源对象的属性混入目标对象中。
156
160
 
157
- /**生成类的实例单例
158
- * @param {Function} classProto 类
159
- * @param {Boolean} overwrite 是否覆盖已有单例
160
- * @param {any[]} params 构造函数的参数
161
- * @return {IObject} 实例化的单例
162
- **/
161
+ - `target`: 目标对象。
162
+ - `source`: 源对象。
163
+ - `overwrite`: 是否覆盖已存在的属性,默认为 `false`。
163
164
 
164
- export type IGetInstance = (classProto: IInstance<FunctionConstructor>, overwrite?: boolean, ...params: any[]) => Function
165
+ ```javascript
166
+ const target = { a: 1 };
167
+ const source = { b: 2 };
168
+ const result = mixIn(target, source);
169
+ console.log(result); // 输出: { a: 1, b: 2 }
170
+ ```
165
171
 
166
- /**通过装饰器将属性混入类中
167
- * @param {IObject<any>} params 混入的属性
168
- * @return {ClassDecorator} 装饰器钩子函数
169
- **/
172
+ ##### 4. `enumInversion(target: object): object`
170
173
 
171
- export type IClassDecorator = (params: IObject<any>) => <TFunction extends Function>(target: TFunction) => void
174
+ 反转枚举对象的键值对。
172
175
 
173
- /**JSON.parse封装
174
- * @param {string} target 字符串
175
- * @return {IObject<any>} 对象
176
- **/
176
+ - `target`: 要反转的枚举对象。
177
177
 
178
- export type IStringToJson = (target: string) => IObject<any>
178
+ ```javascript
179
+ const enumObj = { key1: "value1", key2: "value2" };
180
+ const invertedEnum = enumInversion(enumObj);
181
+ console.log(invertedEnum); // 输出: { value1: 'key1', value2: 'key2' }
182
+ ```
179
183
 
180
- /**JSON.stringify封装
181
- * @param {IObject<any>} target 对象
182
- * @return {string} 字符串
183
- **/
184
+ ##### 5. `isNotObject(source: any, type: string): boolean`
184
185
 
185
- export type IJsonToString = (target: IObject<any>) => string
186
+ 检查值是否为非对象类型。
186
187
 
187
- ##### function
188
+ - `source`: 要检查的值。
189
+ - `type`: 数据类型。
188
190
 
189
- /**节流(throttle):高频事件触发,但在 n 秒内只会执行一次
190
- * @param {Function} fn 节流处理的函数
191
- * @param {number} time 执行间隔/毫秒
192
- * @return {Function} 处理后的函数
193
- **/
191
+ ```javascript
192
+ const result = isNotObject(42, "number");
193
+ console.log(result); // 输出: false
194
+ ```
194
195
 
195
- export type IThrottle = (fn: Function, time: number) => (...args: any[]) => void
196
+ ##### 6. `cloneDeep(target: any): any`
196
197
 
197
- /**防抖(debounce):触发高频事件后 n 秒内函数只会执行一次
198
- * @param {Function} fn 防抖处理的函数
199
- * @param {number} time 允许运行函数间隔/毫秒
200
- * @return {Function} 处理后的函数
201
- **/
198
+ 深度克隆对象。
202
199
 
203
- export type IDebounce = (fn: Function, time: number) => (...args: any[]) => void
200
+ - `target`: 要克隆的对象。
204
201
 
205
- /**
206
- * Promise扁平化,避免Promise嵌套
207
- * @param {number} timer 超时
208
- * @returns {Promise,resolve,reject}
209
- */
210
- export type IDefer = (timer: number) => IPromise
202
+ ```javascript
203
+ const obj = { a: { b: { c: 42 } } };
204
+ const clonedObj = cloneDeep(obj);
205
+ console.log(clonedObj); // 输出: { a: { b: { c: 42 } } }
206
+ ```
211
207
 
212
- /**await与try catch 捕获异常处理方法
213
- * @param {Promise<any>} defer 延迟函数
214
- * @returns {Promise<any>} [error, result]
215
- */
208
+ ##### 7. `createObjectVariable(type: string, source: object = {}): object`
216
209
 
217
- export type ICatchAwait<T extends Promise<any>> = (defer: T) => T
210
+ 根据类型创建特定类型的对象。
218
211
 
219
- ##### array
212
+ - `type`: 要创建的对象类型。
213
+ - `source`: 初始化对象的数据。
220
214
 
221
- /**数组乱序
222
- * @param {Array<any>} arr 目标数组
223
- * @returns {Array<any>} 乱序后的数组
224
- */
225
-
226
- export type IArrayRandom<T extends any[]> = (arr: T) => T
215
+ ```javascript
216
+ const array = createObjectVariable("array", [1, 2, 3]);
217
+ console.log(array); // 输出: [1, 2, 3]
218
+ ```
227
219
 
228
- /**数组数组去重
229
- * @param {Array<any>} arr 目标数组
230
- * @returns {Array<any>} 去重后的数组
231
- */
220
+ ##### 8. `createObject(source: object): object`
232
221
 
233
- export type IArrayUniq<T extends any[]> = (arr: T) => T
222
+ 使用原型链创建对象。
234
223
 
235
- /**数组扁平化
236
- * @param {Array<any>} arr 目标数组
237
- * @returns {Array<any>} 扁平化的数组
238
- */
224
+ - `source`: 原型对象。
239
225
 
240
- export type IArrayDemote<T extends IDemoteArray<any>> = (arr: T, result?: T) => T
226
+ ```javascript
227
+ const protoObj = { a: 1 };
228
+ const newObj = createObject(protoObj);
229
+ console.log(newObj.a); // 输出: 1
230
+ ```
241
231
 
242
- ##### element
232
+ ##### 9. `inherit(source: object, target: function): function`
243
233
 
244
- /**IElementParams
245
- * @param {string} ele 标签类型
246
- * @param {CSSStyleDeclaration} style 样式
247
- * @param {Attr} attr 属性
248
- * @param {object} parent 父元素
249
- */
234
+ 继承原型链。
250
235
 
251
- interface IElementParams<T> {
252
- ele: T | string
253
- style: CSSStyleDeclaration
254
- attr: Attr
255
- parent: T
236
+ - `source`: 父对象的原型链。
237
+ - `target`: 子对象的构造函数。
238
+
239
+ ```javascript
240
+ function Parent() {}
241
+ Parent.prototype.method = function () {
242
+ console.log("Hello from parent");
243
+ };
244
+
245
+ function Child() {}
246
+ inherit(Parent, Child);
247
+ const childInstance = new Child();
248
+ childInstance.method(); // 输出: Hello from parent
249
+ ```
250
+
251
+ ##### 10. `getInstance(classProto: function, overwrite: boolean = false, ...params: any[]): object`
252
+
253
+ 获取类的单例实例。
254
+
255
+ - `classProto`: 类的原型链。
256
+ - `overwrite`: 是否覆盖已存在的实例,默认为 `false`。
257
+ - `params`: 类构造函数的参数。
258
+
259
+ ```javascript
260
+ class Singleton {
261
+ constructor(name) {
262
+ this.name = name;
263
+ }
256
264
  }
257
265
 
258
- /**新增标签,设置属性及样式
259
- * @param {IElementParams} params 配置
260
- * @return {ElementObject} 生成的标签
261
- */
266
+ const instance1 = getInstance(Singleton, false, "Instance 1");
267
+ const instance2 = getInstance(Singleton, true, "Instance 2");
262
268
 
263
- export type ICreateElement<T = HTMLElement> = (params: IElementParams<T>) => T
269
+ console.log(instance1 === instance2); // 输出: false
270
+ ```
264
271
 
265
- ##### event
272
+ ##### 11. `classDecorator(params: object): ClassDecorator`
266
273
 
267
- /**浏览器事件
268
- * @param {Document} ele 标签
269
- * @param {string} type 事件类型
270
- * @param {(e: Event) => void} handler 事件回调
271
- * @return {void}
272
- */
274
+ 为类添加装饰器。
273
275
 
274
- export type IAddHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void
276
+ - `params`: 要添加的属性和方法。
275
277
 
276
- /**取消事件冒泡
277
- * @param {Event} e 浏览器事件对象
278
- * @return {void}
279
- */
278
+ ```javascript
279
+ @classDecorator({
280
+ additionalMethod: function () {
281
+ console.log("Additional method");
282
+ },
283
+ })
284
+ class DecoratedClass {}
280
285
 
281
- export type IStopBubble = (e: Event) => void
286
+ const instance = new DecoratedClass();
287
+ instance.additionalMethod(); // 输出: Additional method
288
+ ```
282
289
 
283
- /**取消默认事件
284
- * @param {Event} e 浏览器事件对象
285
- * @return {void}
286
- */
290
+ ##### 12. `stringToJson(target: string): object | null`
287
291
 
288
- export type IStopDefault = (e: Event) => void
292
+ JSON 字符串转换为对象。
289
293
 
290
- /**取消浏览器事件
291
- * @param {Document} ele 标签
292
- * @param {string} type 事件类型
293
- * @param {(e: Event) => void} handler 事件回调
294
- * @return {void}
295
- */
294
+ - `target`: 要转换的 JSON 字符串。
296
295
 
297
- export type IRemoveHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void
296
+ ```javascript
297
+ const jsonString = '{"key": "value"}';
298
+ const jsonObject = stringToJson(jsonString);
299
+ console.log(jsonObject); // 输出: { key: 'value' }
300
+ ```
298
301
 
299
- /**取消默认事件
300
- * @param {Event} e 浏览器事件对象
301
- * @return {void}
302
- */
302
+ ##### 13. `jsonToString(target: any): string`
303
303
 
304
- export type IDispatchEvent = <T extends Document>(ele: T, data: any) => void
304
+ 将对象转换为 JSON 字符串。
305
305
 
306
+ - `target`: 要转换的对象。
306
307
 
307
- ##### log
308
+ ```javascript
309
+ const obj = { key: "value" };
310
+ const jsonString = jsonToString(obj);
311
+ console.log(jsonString); // 输出: '{"key":"value"}'
312
+ ```
308
313
 
309
- /** 单行输出
310
- * @param {string} str 输出字符
311
- * @param {boolean} overwrite 覆盖前面的输出
312
- * @param {boolean} warp 最后一行换行
313
- * @return {void}
314
- */
315
- export type ILogOneLine = (str: string, overwrite?: boolean, warp?: boolean) => void
314
+ ##### 14. `isWindow(win: any): boolean`
316
315
 
317
- /** 循环输出某个队列
318
- * @param {string[]} loopList 输出的字符队列
319
- * @param {number} index 第几个字符
320
- * @param {boolean} isStop 控制暂停循环
321
- * @param {number} timer 两个相隔时间间隔
322
- */
323
- export type ILogLoopParams = {
324
- loopList?: string[]
325
- index?: number
326
- isStop?: boolean
327
- timer?: number
328
- }
329
- export type ILogLoop = (opts?: ILogLoopParams) => ILogLoopParams | void
316
+ 检查是否是浏览器窗口。
330
317
 
331
- ##### request 模块
332
- ###### https://gitee.com/DieHunter/js-request-lib
333
- export type IRequestParams<T> = T | IObject<any> | null
318
+ - `win`: 要检查的对象。
334
319
 
335
- // 请求路径
320
+ ```javascript
321
+ const isBrowserWindow = isWindow(window);
322
+ console.log(isBrowserWindow); // 输出: true
323
+ ```
336
324
 
337
- export type IUrl = string
325
+ #### array 模块
338
326
 
339
- // 环境判断
327
+ ##### 1. `arrayRandom(arr: any[]): any[]`
340
328
 
341
- export type IEnv = 'Window' | 'Node'
329
+ 对数组进行随机排序。
342
330
 
343
- // fetch返回取值方式
331
+ - `arr`: 要排序的数组。
344
332
 
345
- export type IDataType = "text" | "json" | "blob" | "formData" | "arrayBuffer"
333
+ ```javascript
334
+ const originalArray = [1, 2, 3, 4, 5];
335
+ const randomizedArray = arrayRandom(originalArray);
336
+ console.log(randomizedArray);
337
+ // 输出: 一个随机排序的数组
338
+ ```
346
339
 
347
- // 请求方式
340
+ ##### 2. `arrayUniq(arr: any[]): any[]`
348
341
 
349
- export type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION"
342
+ 从数组中移除重复的元素。
350
343
 
351
- // body结构
344
+ - `arr`: 要处理的数组。
352
345
 
353
- export type IRequestBody = IRequestParams<BodyInit>
346
+ ```javascript
347
+ const arrayWithDuplicates = [1, 2, 2, 3, 4, 4, 5];
348
+ const uniqueArray = arrayUniq(arrayWithDuplicates);
349
+ console.log(uniqueArray);
350
+ // 输出: 移除重复元素后的数组
351
+ ```
354
352
 
355
- // heads结构
353
+ ##### 3. `arrayDemote(arr: IDemoteArray<any>, result: any[] = []): any[]`
356
354
 
357
- export type IRequestHeaders = IRequestParams<HeadersInit>
355
+ 将多层嵌套的数组降维。
358
356
 
359
- // 请求基础函数
357
+ - `arr`: 要降维的数组。
358
+ - `result`: 用于存储结果的数组,默认为空数组。
360
359
 
361
- export type IRequestBaseFn = (url: IUrl, opts: IRequestOptions) => Promise<any>
360
+ ```javascript
361
+ const nestedArray = [1, [2, [3, [4]], 5]];
362
+ const demotedArray = arrayDemote(nestedArray);
363
+ console.log(demotedArray);
364
+ // 输出: 降维后的数组 [1, 2, 3, 4, 5]
365
+ ```
362
366
 
363
- // 请求函数体
367
+ #### function 模块
364
368
 
365
- export type IRequestFn = (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => Promise<any>
369
+ ##### 1. `throttle(fn: Function, time: number): Function`
366
370
 
367
- // 请求参数
371
+ 限制函数在指定时间内的调用频率。
368
372
 
369
- export type IRequestOptions = {
370
- method?: IRequestMethods
371
- query?: IRequestParams<IObject<any>>
372
- body?: IRequestBody
373
- headers?: IRequestHeaders
374
- controller?: AbortController
375
- timeout?: number
376
- timer?: number | unknown | null
377
- [key: string]: any
378
- }
373
+ - `fn`: 要执行的函数。
374
+ - `time`: 时间间隔(毫秒)。
379
375
 
380
- // 拦截器
376
+ ```javascript
377
+ const throttledFunction = throttle(
378
+ () => console.log("Throttled function called!"),
379
+ 1000
380
+ );
381
+ throttledFunction(); // 只有在 1 秒后才会执行
382
+ ```
381
383
 
382
- export type IInterceptors = {
383
- use(type: "request" | "response" | "error", fn: Function): void
384
- get reqFn(): Function
385
- get resFn(): Function
386
- get errFn(): Function
387
- }
384
+ ##### 2. `debounce(fn: Function, time: number): Function`
388
385
 
389
- // 公共函数
390
-
391
- export type IRequestBase = {
392
- readonly origin: string
393
- chackUrl: (url: IUrl) => boolean
394
- envDesc: () => IEnv
395
- errorFn: <Err = any, R = Function>(reject: R) => (err: Err) => R
396
- clearTimer: (opts: IRequestOptions) => void
397
- initAbort: <T = IRequestOptions>(opts: T) => T
398
- requestType: () => IRequestBaseFn
399
- fixOrigin: (fixStr: string) => string
400
- fetch: IRequestBaseFn
401
- http: IRequestBaseFn
402
- getDataByType: (type: IDataType, response: Response) => Promise<any>
403
- }
386
+ 限制函数在指定时间内的连续调用。
404
387
 
405
- // 初始化参数
388
+ - `fn`: 要执行的函数。
389
+ - `time`: 时间间隔(毫秒)。
406
390
 
407
- export type IRequestInit = {
408
- initDefaultParams: (url: IUrl, opts: IRequestOptions) => any
409
- initFetchParams: (url: IUrl, opts: IRequestOptions) => any
410
- initHttpParams: (url: IUrl, opts: IRequestOptions) => any
411
- }
391
+ ```javascript
392
+ const debouncedFunction = debounce(
393
+ () => console.log("Debounced function called!"),
394
+ 1000
395
+ );
396
+ debouncedFunction(); // 在 1 秒内多次调用,只有最后一次会执行
397
+ ```
412
398
 
413
- // 请求主体类
399
+ ##### 3. `defer(timer: number = 0): { promise: Promise<void>, resolve: Function, reject: Function }`
414
400
 
415
- export type IRequest = {
416
- GET: IRequestFn
417
- POST: IRequestFn
418
- DELETE: IRequestFn
419
- PUT: IRequestFn
420
- OPTIONS: IRequestFn
421
- HEAD: IRequestFn
422
- PATCH: IRequestFn
423
- } & IRequestBase
401
+ 创建一个扁平化的 Promise 延迟对象,可以设置在一定时间后超时。
424
402
 
403
+ - `timer`: 超时时间(毫秒),默认为 0。
425
404
 
426
- <!-- 消息中心 -->
427
- ##### 消息中心
428
- ###### https://gitee.com/DieHunter/message-center
429
- export declare interface Handlers {
430
- [key: string]: Array<Function>
431
- }
432
- export declare interface IMessageCenter {
433
- events: Handlers
434
- _instance?: IMessageCenter
435
- on: (type: string, handler: Function) => this
436
- emit: (type: string, data?: any) => this
437
- un: (type: string, handler?: Function) => this
438
- once: (type: string, handler: Function) => this
439
- clear: () => this
440
- has: (type: string) => boolean
441
- handlerLength: (type: string) => number
442
- watch: (type: string, handler: Function) => this
443
- invoke: (type: string, data?: any) => Promise<void>
444
- }
405
+ ```javascript
406
+ const deferFn = () => {
407
+ const { resolve, promise } = defer();
408
+ setTimeout(() => resolve("success"), 500);
409
+ return promise;
410
+ };
411
+ const delayFn = () => {
412
+ const { promise } = defer(1000);
413
+ return promise;
414
+ };
415
+ deferFn().then(console.log); // 延迟输出
416
+ delayFn().catch(console.error); // 超时输出
417
+ ```
445
418
 
419
+ ##### 4. `catchAwait(defer: Promise<any>): Promise<[Error | null, any]>`
446
420
 
421
+ 捕获异步操作的错误并返回 `[Error, null]` 或 `[null, result]`。
447
422
 
448
- <!-- 任务队列 -->
449
- ##### 任务队列
450
- ###### https://gitee.com/DieHunter/task-queue
451
- /**
452
- * 单条队列
453
- * defer: 待运行的异步函数
454
- * params?: defer的参数,也可以用bind直接传递
455
- *
456
- */
457
- export interface IQueue {
458
- defer: Function
459
- name?: string
460
- }
461
- /**
462
- * 队列参数
463
- * children: 队列列表
464
- * name: 队列唯一标识
465
- * result: 运行完成后的结果
466
- *
467
- */
468
- export interface IQueues {
469
- children: Array<Function>
470
- name: string
471
- result?: any[]
472
- }
473
- /**
474
- * 队列缓存
475
- */
476
- export type IQueueTemp = {
477
- [key: string]: IQueues
478
- }
479
- /**
480
- * 系统队列
481
- */
482
- export type IQueueList = Array<IQueue>
483
- /**
484
- * 队列状态 idle:空闲 pending:等待 fulfilled:完成 rejected:失败
485
- */
486
- export type IState = "idle" | "pending" | "fulfilled" | "rejected"
487
- /**
488
- * 任务队列参数
489
- */
490
- export type ITaskQueueProps = {
491
- maxLen: number
492
- }
493
- /**
494
- * 任务队列
495
- */
496
- export type ITaskQueue = {
497
- readonly fix: string
498
- props: ITaskQueueProps
499
- queueTemp: IQueueTemp
500
- queues: IQueueList
501
- state: IState
502
- push: (queue: IQueues) => Promise<void>
503
- unshift: (length: number) => IQueueList
504
- run: (reject: any) => unknown
505
- clear: () => void
506
- }
423
+ - `defer`: 异步操作的 Promise 对象。
507
424
 
425
+ ```javascript
426
+ const asyncOperation = new Promise((resolve, reject) => {
427
+ // 模拟异步操作
428
+ setTimeout(() => {
429
+ reject(new Error("Something went wrong!"));
430
+ }, 1000);
431
+ });
508
432
 
433
+ const [error, result] = await catchAwait(asyncOperation);
434
+ console.log(error); // 输出: Error: Something went wrong!
435
+ console.log(result); // 输出: null
436
+ ```
509
437
 
510
- /** 单行输出
511
- * @param {string} str 输出字符
512
- * @param {boolean} overwrite 覆盖前面的输出
513
- * @param {boolean} warp 最后一行换行
514
- * @return {void}
515
- */
516
- export function logOneLine(str: string): void
517
- export function logOneLine(str: string, overwrite?: boolean): void
518
- export function logOneLine(str: string, overwrite?: boolean, warp?: boolean): void
519
-
520
-
521
- // animate
522
-
523
- /**
524
- * 动画类
525
- * 参考:https://gitee.com/DieHunter/myCode/tree/master/%E5%89%8D%E7%AB%AF%E5%8A%A8%E7%94%BB/js
526
- *
527
- */
528
- export type IAnimateFrame = {
529
- id: number | null // 动画索引
530
- duration: number // 帧数控制
531
- isActive: boolean// 激活控制
532
- fn(timer: number): void
533
- start(duration: number): void
534
- stop(): void
535
- animate(timer: number): void
536
- }
537
- declare var IAnimateFrame: {
538
- new(fn: (timer: number) => void): IAnimateFrame
539
- prototype: IAnimateFrame
540
- }
438
+ #### element 模块
439
+
440
+ ##### 1. `createElement(options: { ele?: string | HTMLElement, style?: object, attr?: object, parent?: HTMLElement }): HTMLElement`
441
+
442
+ 创建并返回一个 HTML 元素。
443
+
444
+ - `ele`: 元素类型或已存在的 HTMLElement 对象,可选,默认为 'div'。
445
+ - `style`: 元素的样式对象,可选。
446
+ - `attr`: 元素的属性对象,可选。
447
+ - `parent`: 元素的父级元素,可选。
448
+
449
+ ```javascript
450
+ const options = {
451
+ ele: 'div',
452
+ style: { color: 'blue', fontSize: '16px' },
453
+ attr: { id: 'myElement', className: 'custom-class' },
454
+ parent: document.body
455
+ };
456
+
457
+ const createdElement = createElement(options);
458
+ // body 中创建一个带有样式和属性的 div 元素
459
+ ```
460
+ #### event 模块
461
+
462
+ ##### 1. `addHandler(ele: HTMLElement, type: string, handler: EventListener): void`
463
+
464
+ 为元素添加事件监听器。
465
+
466
+ - `ele`: 目标元素。
467
+ - `type`: 事件类型。
468
+ - `handler`: 事件处理函数。
469
+
470
+ ```javascript
471
+ const button = document.getElementById('myButton');
472
+ const handleClick = () => console.log('Button clicked!');
473
+ addHandler(button, 'click', handleClick);
474
+ ```
475
+
476
+ ##### 2. `stopBubble(event: Event): void`
477
+
478
+ 阻止事件冒泡。
479
+
480
+ - `event`: 事件对象。
481
+
482
+ ```javascript
483
+ const handleClick = (event) => {
484
+ console.log('Button clicked!');
485
+ stopBubble(event);
486
+ };
487
+ ```
488
+
489
+ ##### 3. `stopDefault(event: Event): void`
490
+
491
+ 阻止事件的默认行为。
492
+
493
+ - `event`: 事件对象。
494
+
495
+ ```javascript
496
+ const handleFormSubmit = (event) => {
497
+ console.log('Form submitted!');
498
+ stopDefault(event);
499
+ };
500
+ ```
501
+
502
+ ##### 4. `removeHandler(ele: HTMLElement, type: string, handler: EventListener): void`
503
+
504
+ 移除元素的事件监听器。
505
+
506
+ - `ele`: 目标元素。
507
+ - `type`: 事件类型。
508
+ - `handler`: 要移除的事件处理函数。
509
+
510
+ ```javascript
511
+ const button = document.getElementById('myButton');
512
+ const handleClick = () => console.log('Button clicked!');
513
+ addHandler(button, 'click', handleClick);
514
+ // 其他操作...
515
+ removeHandler(button, 'click', handleClick);
516
+ ```
517
+
518
+ ##### 5. `dispatchEvent(ele: HTMLElement, data: any): void`
519
+
520
+ 触发自定义事件。
521
+
522
+ - `ele`: 目标元素。
523
+ - `data`: 自定义事件的数据。
524
+
525
+ ```javascript
526
+ const customEvent = new CustomEvent('customEvent', { detail: { key: 'value' } });
527
+ const targetElement = document.getElementById('myElement');
528
+ dispatchEvent(targetElement, customEvent);
529
+ ```
530
+ #### storage 模块
531
+
532
+ ##### 1. `setStorage(key: string, val: any): void`
533
+
534
+ 将值存储到本地存储中。
535
+
536
+ - `key`: 存储的键名。
537
+ - `val`: 要存储的值。
538
+
539
+ ```javascript
540
+ const userData = { username: 'john_doe', email: 'john@example.com' };
541
+ setStorage('user_data', userData);
542
+ ```
543
+
544
+ ##### 2. `getStorage(key: string): any`
545
+
546
+ 从本地存储中获取值。
547
+
548
+ - `key`: 要获取的键名。
549
+
550
+ ```javascript
551
+ const storedUserData = getStorage('user_data');
552
+ console.log(storedUserData);
553
+ // 输出: { username: 'john_doe', email: 'john@example.com' }
554
+ ```
555
+
556
+ ##### 3. `clearStorage(key?: string): void`
557
+
558
+ 清除本地存储中的值。
559
+
560
+ - `key`: 可选,要清除的键名。如果未提供键名,则清除所有存储。
561
+
562
+ ```javascript
563
+ clearStorage('user_data'); // 清除特定键名的存储值
564
+ // 或者
565
+ clearStorage(); // 清除所有本地存储的值
566
+ ```
567
+
568
+ #### log 模块
569
+
570
+ ##### 1. `logOneLine(str: string, overwrite: boolean = false, wrap: boolean = true): void`
571
+
572
+ 在一行中输出日志。
573
+
574
+ - `str`: 要输出的字符串。
575
+ - `overwrite`: 是否覆盖当前行,默认为 `false`。
576
+ - `wrap`: 是否在输出后换行,默认为 `true`。
577
+
578
+ ```javascript
579
+ logOneLine('This is a single line log message.');
580
+ ```
581
+
582
+ ##### 2. `logLoop(opts?: { loopList?: string[], isStop?: boolean, timer?: number, index?: number }): { loopList?: string[], isStop?: boolean, timer?: number, index?: number }`
583
+
584
+ 在控制台中输出循环动画。
585
+
586
+ - `opts`: 可选参数对象,包含以下属性:
587
+ - `loopList`: 动画字符列表,默认为 `['\\', '|', '/', '—', '—']`。
588
+ - `isStop`: 是否停止循环,默认为 `false`。
589
+ - `timer`: 动画切换的时间间隔(毫秒),默认为 `100`。
590
+ - `index`: 当前动画字符的索引,默认为 `0`。
591
+
592
+ ```javascript
593
+ logLoop(); // 启动默认循环动画
594
+ // 或者
595
+ logLoop({ loopList: ['-', '+', '-', '+'], timer: 200 }); // 启动自定义循环动画
596
+ ```
597
+ #### animation 模块
598
+
599
+ ##### 1. `class AnimateFrame`
600
+
601
+ 提供帧动画的类。
602
+
603
+ - `start(duration?: number): void`: 启动帧动画,可选参数 `duration` 为帧数控制。
604
+ - `stop(): void`: 停止帧动画。
605
+
606
+ ```javascript
607
+ const animateFrame = new AnimateFrame((timestamp) => {
608
+ // 在此处更新动画状态
609
+ console.log('AnimationFrame callback:', timestamp);
610
+ });
611
+
612
+ animateFrame.start(60); // 启动帧动画,每秒 60 帧
613
+ // 其他操作...
614
+ animateFrame.stop(); // 停止帧动画
615
+ ```
616
+
617
+ ##### 2. `quadraticBezier(_x: number, _y: number, t: number): [number, number]`
618
+
619
+ 计算二次贝塞尔曲线上的点坐标。
620
+
621
+ - `_x`: 控制点1 x 坐标。
622
+ - `_y`: 控制点1 y 坐标。
623
+ - `t`: 时间参数,取值范围 [0, 1]。
624
+
625
+ ```javascript
626
+ const result = quadraticBezier(0, 0, 1, 1, 0.5);
627
+ console.log(result);
628
+ // 输出: [0.75, 0.75]
629
+ ```
630
+
631
+ ##### 3. `cubicBezier(_x1: number, _y1: number, _x2: number, _y2: number, t: number): [number, number]`
632
+
633
+ 计算三次贝塞尔曲线上的点坐标。
634
+
635
+ - `_x1`: 控制点1 x 坐标。
636
+ - `_y1`: 控制点1 y 坐标。
637
+ - `_x2`: 控制点2 x 坐标。
638
+ - `_y2`: 控制点2 y 坐标。
639
+ - `t`: 时间参数,取值范围 [0, 1]。
640
+
641
+ ```javascript
642
+ const result = cubicBezier(0, 0, 0.5, 1, 0.5);
643
+ console.log(result);
644
+ // 输出: [0.375, 0.625]
645
+ ```
646
+
647
+ ##### 4. `factorial(n: number): number`
648
+
649
+ 计算阶乘。
650
+
651
+ - `n`: 非负整数。
652
+
653
+ ```javascript
654
+ const result = factorial(5);
655
+ console.log(result);
656
+ // 输出: 120
657
+ ```
658
+
659
+ ##### 5. `combination(n: number, k: number): number`
660
+
661
+ 计算组合数。
662
+
663
+ - `n`: 总数。
664
+ - `k`: 选择的个数。
665
+
666
+ ```javascript
667
+ const result = combination(5, 2);
668
+ console.log(result);
669
+ // 输出: 10
670
+ ```
671
+
672
+ ##### 6. `NBezier(points: number[][], t: number): [number, number]`
673
+
674
+ 计算 N 次贝塞尔曲线上的点坐标。
675
+
676
+ - `points`: 控制点数组,每个点是一个二阶数组。
677
+ - `t`: 时间参数,取值范围 [0, 1]。
678
+
679
+ ```javascript
680
+ const points = [[0, 0], [1, 1], [2, 0]];
681
+ const result = NBezier(points, 0.5);
682
+ console.log(result);
683
+ // 输出: [1.5, 0.75]
684
+ ```
685
+
686
+ #### event-message-center
687
+ https://gitee.com/DieHunter/message-center
688
+ #### task-queue-lib
689
+ https://gitee.com/DieHunter/task-queue
690
+ #### js-request-lib
691
+ https://gitee.com/DieHunter/js-request-lib