@minto-ai/tools 1.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.
Files changed (73) hide show
  1. package/README.md +1335 -0
  2. package/dist/array/chunk-array.d.ts +12 -0
  3. package/dist/array/index.d.ts +2 -0
  4. package/dist/browser/copy-text.d.ts +9 -0
  5. package/dist/browser/index.d.ts +6 -0
  6. package/dist/browser/is-android.d.ts +8 -0
  7. package/dist/browser/is-ios.d.ts +8 -0
  8. package/dist/browser/is-mobile.d.ts +9 -0
  9. package/dist/browser/is-pc.d.ts +8 -0
  10. package/dist/core/__test__/can-be-parsed-as-number.test.d.ts +1 -0
  11. package/dist/core/be-parsed-as-number.d.ts +8 -0
  12. package/dist/core/can-be-parsed-as-number.d.ts +8 -0
  13. package/dist/core/check-array-empty.d.ts +8 -0
  14. package/dist/core/check-empty-not-zero.d.ts +8 -0
  15. package/dist/core/check-empty.d.ts +9 -0
  16. package/dist/core/check-object-empty.d.ts +9 -0
  17. package/dist/core/index.d.ts +13 -0
  18. package/dist/core/is-array.d.ts +8 -0
  19. package/dist/core/is-null-or-undefined.d.ts +7 -0
  20. package/dist/core/is-number.d.ts +8 -0
  21. package/dist/core/is-object.d.ts +8 -0
  22. package/dist/core/is-string.d.ts +8 -0
  23. package/dist/core/is-undefined.d.ts +8 -0
  24. package/dist/file/batch-download-file.d.ts +11 -0
  25. package/dist/file/extends/enum.d.ts +81 -0
  26. package/dist/file/extends/regex.d.ts +3 -0
  27. package/dist/file/get-file-feed-type.d.ts +9 -0
  28. package/dist/file/get-file-icon.d.ts +8 -0
  29. package/dist/file/get-file-item-icon.d.ts +9 -0
  30. package/dist/file/get-file-item-suffix.d.ts +9 -0
  31. package/dist/file/get-file-name.d.ts +8 -0
  32. package/dist/file/get-file-suffix-icon.d.ts +9 -0
  33. package/dist/file/get-file-suffix.d.ts +9 -0
  34. package/dist/file/get-file-title.d.ts +8 -0
  35. package/dist/file/index.d.ts +19 -0
  36. package/dist/file/is-document-file-path.d.ts +8 -0
  37. package/dist/file/is-file-path.d.ts +8 -0
  38. package/dist/file/is-image-file-path.d.ts +8 -0
  39. package/dist/file/is-ppt-file-path.d.ts +8 -0
  40. package/dist/file/is-video-file-path.d.ts +8 -0
  41. package/dist/file/single-download-file.d.ts +8 -0
  42. package/dist/file/types/index.d.ts +18 -0
  43. package/dist/function/debounce.d.ts +23 -0
  44. package/dist/function/index.d.ts +3 -0
  45. package/dist/function/throttle.d.ts +23 -0
  46. package/dist/index.d.ts +11 -0
  47. package/dist/index.js +547 -0
  48. package/dist/math/__test__/add.test.d.ts +1 -0
  49. package/dist/math/__test__/divide.test.d.ts +1 -0
  50. package/dist/math/__test__/get-decimal-places.test.d.ts +1 -0
  51. package/dist/math/__test__/multiply.test.d.ts +1 -0
  52. package/dist/math/__test__/subtract.test.d.ts +1 -0
  53. package/dist/math/add.d.ts +9 -0
  54. package/dist/math/divide.d.ts +9 -0
  55. package/dist/math/get-decimal-places.d.ts +8 -0
  56. package/dist/math/index.d.ts +6 -0
  57. package/dist/math/multiply.d.ts +9 -0
  58. package/dist/math/subtract.d.ts +9 -0
  59. package/dist/object/deep-freeze.d.ts +9 -0
  60. package/dist/object/index.d.ts +3 -0
  61. package/dist/object/pick-object.d.ts +9 -0
  62. package/dist/string/__test__/chunk-string.test.d.ts +1 -0
  63. package/dist/string/chunk-string.d.ts +10 -0
  64. package/dist/string/index.d.ts +2 -0
  65. package/dist/supply/get-uuid.d.ts +9 -0
  66. package/dist/supply/index.d.ts +2 -0
  67. package/dist/web-socket/create-web-socket.d.ts +9 -0
  68. package/dist/web-socket/index.d.ts +3 -0
  69. package/dist/web-socket/is-web-socket-supported.d.ts +9 -0
  70. package/dist/wrker/close-worker.d.ts +9 -0
  71. package/dist/wrker/create-worker.d.ts +11 -0
  72. package/dist/wrker/index.d.ts +3 -0
  73. package/package.json +32 -0
package/README.md ADDED
@@ -0,0 +1,1335 @@
1
+ # @minto-ai/tools工具库
2
+
3
+ 欢迎使用@minto-ai/tools工具库,旨在提供一系列实用的函数,以简化日常开发任务。
4
+
5
+ # 安装
6
+
7
+ 使用 pnpm 安装最新版本的 @minto-ai/tools:
8
+
9
+ ```bash
10
+ pnpm install @minto-ai/tools
11
+ ```
12
+
13
+ # 功能概览
14
+
15
+ ## 主要
16
+
17
+ ### `isNumber`
18
+
19
+ > `function isNumber(value: unknown): value is number`
20
+
21
+ 判断给定的值是否是一个数字。
22
+
23
+ #### 参数
24
+
25
+ - `value (unknown)`: 要检查的值。
26
+
27
+ #### 返回
28
+
29
+ `(value is number)`: 如果 `value` 是一个数字,则返回 `true`,否则返回 `false`。
30
+
31
+ #### 例子
32
+
33
+ ```typescript
34
+ import { isNumber } from '@minto-ai/tools'
35
+
36
+ isNumber(123) // true
37
+ isNumber('123') // false
38
+ ```
39
+
40
+ ### `isString`
41
+
42
+ > `function isString(value: unknown): value is string`
43
+
44
+ 判断给定的值是否是一个字符串。
45
+
46
+ #### 参数
47
+
48
+ - `value (unknown)`: 要检查的值。
49
+
50
+ #### 返回
51
+
52
+ `( value is string)`: 如果 `value` 是一个字符串,则返回 `true`,否则返回 `false`。
53
+
54
+ #### 例子
55
+
56
+ ```typescript
57
+ import { isString } from '@minto-ai/tools'
58
+
59
+ isString('hello') // true
60
+ isString(123) // false
61
+ ```
62
+
63
+ ### `isUndefined`
64
+
65
+ > `function isUndefined(value: unknown): value is undefined`
66
+
67
+ 检查一个值是否为 undefined。
68
+
69
+ #### 参数
70
+
71
+ - `value (unknown)`: 要检查的值。
72
+
73
+ #### 返回
74
+
75
+ `(value is undefined)`: 如果 `value` 是 undefined,则返回 `true`,否则返回 `false`。
76
+
77
+ #### 例子
78
+
79
+ ```typescript
80
+ import { isUndefined } from '@minto-ai/tools'
81
+
82
+ console.log(isUndefined(undefined)) // true
83
+ console.log(isUndefined(123)) // false
84
+ ```
85
+
86
+ ### `isNullOrUndefined`
87
+
88
+ > `function isNullOrUndefined(value: unknown): value is null | undefined`
89
+
90
+ 如果值为 null 或 undefined,则返回 true;否则返回 false。
91
+
92
+ #### 参数
93
+
94
+ - `value (unknown)`: 要检查的值。
95
+
96
+ #### 返回
97
+
98
+ `(value is null | undefined)`: 如果 `value` 是 null 或 undefined,则返回 `true`,否则返回 `false`。
99
+
100
+ #### 例子
101
+
102
+ ```typescript
103
+ import { isNullOrUndefined } from '@minto-ai/tools'
104
+
105
+ console.log(isNullOrUndefined(null)) // true
106
+ console.log(isNullOrUndefined(123)) // false
107
+ ```
108
+
109
+ ### `isObject`
110
+
111
+ > `function isObject(value: unknown): value is Record<PropertyKey, unknown>`
112
+
113
+ 判断给定的值是否是一个对象。但对象的值包含 null 的情况,返回 false。
114
+
115
+ #### 参数
116
+
117
+ - `value (unknown)`: 要检查的值。
118
+
119
+ #### 返回
120
+
121
+ `(value is Record<PropertyKey, unknown>)`: 如果 `value` 是一个普通对象,则返回 `true`,否则返回 `false`。
122
+
123
+ #### 例子
124
+
125
+ ```typescript
126
+ import { isObject } from '@minto-ai/tools'
127
+
128
+ isObject({ a: 1 }) // true
129
+ isObject(null) // false
130
+ ```
131
+
132
+ ### `isArray`
133
+
134
+ > `function isArray(value: unknown): value is unknown[]`
135
+
136
+ 判断给定的值是否是一个数组。
137
+
138
+ #### 参数
139
+
140
+ - `value (unknown)`: 要检查的值。
141
+
142
+ #### 返回
143
+
144
+ `(value is unknown[])`: 如果 `value` 是一个数组,则返回 `true`,否则返回 `false`。
145
+
146
+ #### 例子
147
+
148
+ ```typescript
149
+ import { isArray } from '@minto-ai/tools'
150
+
151
+ isArray([1, 2, 3]) // true
152
+ isArray('123') // false
153
+ ```
154
+
155
+ ### `checkEmpty`
156
+
157
+ > `function checkEmpty(value: unknown): boolean`
158
+
159
+ 检查给定的值是否为空。空值包括:空字符串、空数组、空对象、null 或 undefined。
160
+
161
+ #### 参数
162
+
163
+ - `value (unknown)`: 要检查的值。
164
+
165
+ #### 返回
166
+
167
+ `(boolean)`: 如果 `value` 是空的,则返回 `true`,否则返回 `false`。
168
+
169
+ #### 例子
170
+
171
+ ```typescript
172
+ import { checkEmpty } from '@minto-ai/tools'
173
+
174
+ checkEmpty('') // true
175
+ checkEmpty(null) // true
176
+ checkEmpty({}) // true
177
+ checkEmpty([]) // true
178
+ checkEmpty('text') // false
179
+ ```
180
+
181
+ ### `checkObjectEmpty`
182
+
183
+ > `function checkObjectEmpty(value: unknown): boolean`
184
+
185
+ 检查对象是否为空。一个空对象是指没有任何自身属性的对象。
186
+
187
+ #### 参数
188
+
189
+ - `value (unknown)`: 要检查的值。
190
+
191
+ #### 返回
192
+
193
+ `(boolean)`: 如果 `value` 是一个空对象,则返回 `true`,否则返回 `false`。
194
+
195
+ #### 例子
196
+
197
+ ```typescript
198
+ import { checkObjectEmpty } from '@minto-ai/tools'
199
+
200
+ checkObjectEmpty({}) // true
201
+ checkObjectEmpty({ a: 1 }) // false
202
+ ```
203
+
204
+ ### `checkArrayEmpty`
205
+
206
+ > `function checkArrayEmpty(value: unknown): boolean`
207
+
208
+ 检查数组是否为空。
209
+
210
+ #### 参数
211
+
212
+ - `value (unknown)`: 要检查的值。
213
+
214
+ #### 返回
215
+
216
+ `(boolean)`: 如果 `value` 是一个数组并且长度为0,则返回 `true`,否则返回 `false`。
217
+
218
+ #### 例子
219
+
220
+ ```typescript
221
+ import { checkArrayEmpty } from '@minto-ai/tools'
222
+
223
+ checkArrayEmpty([]) // true
224
+ checkArrayEmpty([1, 2, 3]) // false
225
+ ```
226
+
227
+ ### `checkEmptyNotZero`
228
+
229
+ > `function checkEmptyNotZero(value: unknown): boolean`
230
+
231
+ 检查给定的值是否为空,但不包括数字 0。空值包括:空字符串、空数组、空对象、null 或 undefined。
232
+
233
+ #### 参数
234
+
235
+ - `value (unknown)`: 要检查的值。
236
+
237
+ #### 返回
238
+
239
+ `(boolean)`: 如果 `value` 是空的,且不是数字 0,则返回 `true`,否则返回 `false`。
240
+
241
+ #### 例子
242
+
243
+ ```typescript
244
+ import { checkEmptyNotZero } from '@minto-ai/tools'
245
+
246
+ checkEmptyNotZero('') // true
247
+ checkEmptyNotZero(0) // false
248
+ checkEmptyNotZero(null) // true
249
+ checkEmptyNotZero({}) // true
250
+ checkEmptyNotZero([]) // true
251
+ checkEmptyNotZero('text') // false
252
+ ```
253
+
254
+ ### `canBeParsedAsNumber`
255
+
256
+ > `function canBeParsedAsNumber(value: unknown): boolean`
257
+
258
+ 检查一个值是否可以被解析为有效的数字。
259
+
260
+ #### 参数
261
+
262
+ - `value (unknown)`: 要检查的值。
263
+
264
+ #### 返回
265
+
266
+ `(boolean)`: 如果 `value` 可以被解析为数字,则返回 `true`,否则返回 `false`。
267
+
268
+ #### 例子
269
+
270
+ ```typescript
271
+ import { canBeParsedAsNumber } from '@minto-ai/tools'
272
+
273
+ console.log(canBeParsedAsNumber('123')) // true
274
+ console.log(canBeParsedAsNumber('abc')) // false
275
+ console.log(canBeParsedAsNumber(null)) // false
276
+ console.log(canBeParsedAsNumber(undefined)) // false
277
+ console.log(canBeParsedAsNumber(123)) // true
278
+ ```
279
+
280
+ ### `beParsedAsNumber`
281
+
282
+ > `function beParsedAsNumber(value: never): number`
283
+
284
+ 将输入值转换为数字类型。
285
+
286
+ #### 参数
287
+
288
+ - `value (never)`: 要转换的值。
289
+
290
+ #### 返回
291
+
292
+ `(number)`: 如果 `value` 可以被解析为数字,则返回解析后的数字,否则返回 NaN。
293
+
294
+ #### 例子
295
+
296
+ ```typescript
297
+ import { beParsedAsNumber } from '@minto-ai/tools'
298
+
299
+ console.log(beParsedAsNumber('123')) // 123
300
+ console.log(beParsedAsNumber('abc')) // NaN
301
+ console.log(beParsedAsNumber(null)) // NaN
302
+ ```
303
+
304
+ ## 字符串
305
+
306
+ ### `chunkString`
307
+
308
+ > `function chunkString(str: string, chunkSize: number): string[]`
309
+
310
+ 将输入字符串按照指定的块大小分割成多个子字符串。
311
+
312
+ #### 参数
313
+
314
+ - `str (string)`: 要分割的字符串。
315
+ - `chunkSize (number)`: 每个子字符串的长度。
316
+
317
+ #### 返回
318
+
319
+ `(string[])`: 返回一个包含分割后的子字符串的数组。
320
+
321
+ #### 例子
322
+
323
+ ```typescript
324
+ import { chunkString } from '@minto-ai/tools'
325
+
326
+ console.log(chunkString('abcdefghijklmnopqrstuvwxyz', 3)) // ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz']
327
+ console.log(chunkString('abcdefghijklmnopqrstuvwxyz', 5)) // ['abcde', 'fghij', 'klmno', 'pqrst', 'uvwxy', 'z']
328
+ ```
329
+
330
+ ## 函数
331
+
332
+ ### `throttle`
333
+
334
+ > `function throttle<T extends (...args: any[]) => any>(callback: T, delay: number): ThrottledFunction<T>`
335
+
336
+ 节流函数用于限制一个函数的执行频率。它确保一个函数在指定的时间间隔内最多只执行一次。
337
+
338
+ #### 参数
339
+
340
+ - `callback (Function)`: 要节流的函数。
341
+ - `delay (number)`: 执行频率的延迟时间(以毫秒为单位)。
342
+
343
+ #### 返回
344
+
345
+ `(Function)`: 返回一个节流后的函数,包含原函数的参数类型和一个 cancel 方法,用于取消未执行的部分。
346
+
347
+ #### 例子
348
+
349
+ ```typescript
350
+ import { throttle } from '@minto-ai/tools'
351
+
352
+ // 定义一个需要节流的函数
353
+ function handleScroll() {
354
+ console.log('Scroll event triggered')
355
+ }
356
+
357
+ // 创建节流后的函数,每 500 毫秒最多执行一次
358
+ const throttledScroll = throttle(handleScroll, 500)
359
+
360
+ // 绑定到事件监听器
361
+ window.addEventListener('scroll', throttledScroll)
362
+
363
+ // 取消节流函数的未执行部分
364
+ throttledScroll.cancel()
365
+ ```
366
+
367
+ ### `debounce`
368
+
369
+ > `function debounce<T extends (...args: any[]) => any>(callback: T, delay: number): DebouncedFunction<T>`
370
+
371
+ 防抖函数用于延迟函数的执行,直到指定时间内不再触发事件。如果在延迟时间内再次触发事件,则会重新计时。
372
+
373
+ #### 参数
374
+
375
+ - `callback(Function)`: 要防抖的函数。
376
+ - `delay(number)`: 执行频率的延迟时间(以毫秒为单位)。
377
+
378
+ #### 返回
379
+
380
+ `(Function)`: 返回一个防抖后的函数,包含原函数的参数类型和一个 cancel 方法,用于取消未执行的部分。
381
+
382
+ #### 例子
383
+
384
+ ```typescript
385
+ import { debounce } from '@minto-ai/tools'
386
+
387
+ // 定义一个需要防抖的函数
388
+ function handleResize() {
389
+ console.log('Window resized')
390
+ }
391
+
392
+ // 创建防抖后的函数,延迟 300 毫秒执行
393
+ const debouncedResize = debounce(handleResize, 300)
394
+
395
+ // 绑定到事件监听器
396
+ window.addEventListener('resize', debouncedResize)
397
+
398
+ // 取消防抖函数的未执行部分
399
+ debouncedResize.cancel()
400
+ ```
401
+
402
+ ## 对象
403
+
404
+ ### `deepFreeze`
405
+
406
+ > `function deepFreeze<T extends Record<string, any>>(obj: T): Readonly<T>`
407
+
408
+ 深度冻结一个对象,防止其属性及嵌套属性被修改。递归地冻结对象的所有属性,包括嵌套对象,确保对象的状态不可变。
409
+
410
+ #### 参数
411
+
412
+ - `value (T)`: 需要被冻结的对象。
413
+
414
+ #### 返回
415
+
416
+ `(Readonly<T>)`: 返回深度冻结后的对象。
417
+
418
+ #### 例子
419
+
420
+ ```typescript
421
+ import { deepFreeze } from '@minto-ai/tools'
422
+
423
+ const obj = { name: 'Kimi', details: { age: 30 } }
424
+ const frozenObj = deepFreeze(obj)
425
+ console.log(Object.isFrozen(frozenObj)) // 输出:true
426
+ console.log(Object.isFrozen(frozenObj.details)) // 输出:true
427
+ ```
428
+
429
+ ### `pickObject`
430
+
431
+ > `function pickObject<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>`
432
+
433
+ 从给定对象中挑选指定的属性,并返回一个包含这些属性的新对象。
434
+
435
+ #### 参数
436
+
437
+ - `obj (T)`: 源对象,从中挑选属性。
438
+ - `keys (K[])`: 需要挑选的属性键名数组。
439
+
440
+ #### 返回
441
+
442
+ `(Readonly<T>)`: 返回深度冻结后的对象。
443
+
444
+ #### 例子
445
+
446
+ ```typescript
447
+ import { pickObject } from '@minto-ai/tools'
448
+
449
+ const user = {
450
+ name: 'Kimi',
451
+ age: 30,
452
+ email: 'kimi@example.com'
453
+ }
454
+
455
+ const pickedUser = pickObject(user, ['name', 'email'])
456
+ console.log(pickedUser) // 输出:{ name: 'Kimi', email: 'kimi@example.com' }
457
+ ```
458
+
459
+ ## 数组
460
+
461
+ ### `chunkArray`
462
+
463
+ > `function chunkArray<T>(array: T[], chunkSize?: number): T[][]`
464
+
465
+ 这个函数接受一个数组和一个指定的块大小,然后返回一个二维数组。每个子数组包含原数组中的一部分元素,大小由 `chunkSize` 参数决定。如果数组的长度不是 `chunkSize` 的整数倍,最后一个块可能包含的元素少于 `chunkSize`。
466
+
467
+ #### 参数
468
+
469
+ - `array (Array)`: 要分割的源数组。
470
+ - `[chunkSize=1] (number)`: 每个块的大小,默认值为 1。
471
+
472
+ #### 返回
473
+
474
+ `(Array)`: 返回一个二维数组,每个子数组是原数组的一部分元素。
475
+
476
+ #### 例子
477
+
478
+ ```typescript
479
+ import { chunkArray } from '@minto-ai/tools'
480
+
481
+ chunkArray(['a', 'b', 'c', 'd'], 2)
482
+ // [['a', 'b'], ['c', 'd']]
483
+
484
+ chunkArray(['a', 'b', 'c', 'd', 'e'], 3)
485
+ // [['a', 'b', 'c'], ['d', 'e']]
486
+
487
+ chunkArray([1, 2, 3, 4, 5], 4)
488
+ // [[1, 2, 3, 4], [5]]
489
+ ```
490
+
491
+ ## 数学
492
+
493
+ ### `getDecimalPlaces`
494
+
495
+ > `function getDecimalPlaces(num: number): number`
496
+
497
+ 获取一个数字的小数位数
498
+
499
+ #### 参数
500
+
501
+ - `num (number)`: 需要获取小数位数的数字。
502
+
503
+ #### 返回
504
+
505
+ `(number)`: 返回一个数字的小数位数。
506
+
507
+ #### 例子
508
+
509
+ ```typescript
510
+ import { getDecimalPlaces } from '@minto-ai/tools'
511
+
512
+ console.log(getDecimalPlaces(123.456)) // 3
513
+ console.log(getDecimalPlaces(123)) // 0
514
+ console.log(getDecimalPlaces(0.001)) // 3
515
+ console.log(getDecimalPlaces(-123.45)) // 2
516
+ ```
517
+
518
+ ### `add`
519
+
520
+ > `function add(augend: number, addend: number): number`
521
+
522
+ 精确地将两个数字相加,避免 JavaScript 中浮点数运算的精度问题。
523
+
524
+ #### 参数
525
+
526
+ - `augend (number)`: 第一个数字(被加数)。
527
+ - `addend (number)`: 第二个数字(加数)。
528
+
529
+ #### 返回
530
+
531
+ `(number)`: 返回两个数字的精确和。
532
+
533
+ #### 例子
534
+
535
+ ```typescript
536
+ import { add } from '@minto-ai/tools'
537
+
538
+ console.log(add(0.1, 0.2)) // 0.3
539
+ console.log(add(1.234, 5.678)) // 6.912
540
+ console.log(add(100, 0.001)) // 100.001
541
+ ```
542
+
543
+ ### `subtract`
544
+
545
+ > `function subtract(minuend: number, subtrahend: number): number`
546
+
547
+ 精确地减去两个数字,避免 JavaScript 中浮点数运算的精度问题。
548
+
549
+ #### 参数
550
+
551
+ - `minuend (number)`: 第一个数字(被减数)。
552
+ - `subtrahend (number)`: 第二个数字(减数)。
553
+
554
+ #### 返回
555
+
556
+ `(number)`: 返回两个数字的精确差。
557
+
558
+ #### 例子
559
+
560
+ ```typescript
561
+ import { subtract } from '@minto-ai/tools'
562
+
563
+ console.log(subtract(0.3, 0.1)) // 0.2
564
+ console.log(subtract(1.234, 0.567)) // 0.667
565
+ console.log(subtract(100.001, 0.001)) // 100
566
+ ```
567
+
568
+ ### `multiply`
569
+
570
+ > `function multiply(multiplier: number, multiplicand: number): number`
571
+
572
+ 精确地乘以两个数字,避免 JavaScript 中浮点数运算的精度问题。
573
+
574
+ #### 参数
575
+
576
+ - `multiplier (number)`: 第一个数字(乘数)。
577
+ - `multiplicand (number)`: 第二个数字(被乘数)。
578
+
579
+ #### 返回
580
+
581
+ `(number)`: 返回两个数字的精确积。
582
+
583
+ #### 例子
584
+
585
+ ```typescript
586
+ import { multiply } from '@minto-ai/tools'
587
+
588
+ console.log(multiply(0.1, 0.2)) // 0.02
589
+ console.log(multiply(1.234, 5.678)) // 7.006652
590
+ console.log(multiply(100.001, 0.001)) // 0.100001
591
+ ```
592
+
593
+ ### `divide`
594
+
595
+ > `function divide(dividend: number, divisor: number): number`
596
+
597
+ 精确地除以两个数字,避免 JavaScript 中浮点数运算的精度问题。
598
+
599
+ #### 参数
600
+
601
+ - `dividend (number)`: 第一个数字(被除数)。
602
+ - `divisor (number)`: 第二个数字(除数)。
603
+
604
+ #### 返回
605
+
606
+ `(number)`: 返回两个数字的精确商。
607
+
608
+ #### 例子
609
+
610
+ ```typescript
611
+ import { divide } from '@minto-ai/tools'
612
+
613
+ console.log(divide(10, 2)) // 5
614
+ console.log(divide(1.234, 0.567)) // 2.1763668430335097
615
+ console.log(divide(100.001, 0.001)) // 100001
616
+ ```
617
+
618
+ ## 浏览器
619
+
620
+ ### `copyText`
621
+
622
+ > `function copyText(text: string): Promise<string>`
623
+
624
+ 将指定的文本复制到剪贴板。
625
+
626
+ #### 参数
627
+
628
+ - `text (string)`: 需要复制到剪贴板的文本。
629
+
630
+ #### 返回
631
+
632
+ `Promise<string>`: 一个 Promise 对象,当文本成功复制到剪贴板时解析为被复制的文本,如果复制失败则拒绝 Promise。
633
+
634
+ #### 例子
635
+
636
+ ```typescript
637
+ import { copyText } from '@minto-ai/tools'
638
+
639
+ copyText('Hello, World!').then(
640
+ (text) => console.log('Text copied to clipboard:', text),
641
+ (error) => console.error('Failed to copy text:', error)
642
+ )
643
+ ```
644
+
645
+ ### `isIos`
646
+
647
+ > `function isIos(): boolean`
648
+
649
+ 判断当前环境是否为 iOS 设备。主要检测的设备类型包括 iPhone、iPad 和 iPod。
650
+
651
+ #### 返回
652
+
653
+ `(boolean)`: 如果是iOS设备,返回true;否则返回false。
654
+
655
+ #### 例子
656
+
657
+ ```typescript
658
+ import { isIos } from '@minto-ai/tools'
659
+ console.log(isIos()) // 输出 true 或 false
660
+ ```
661
+
662
+ ### `isAndroid`
663
+
664
+ > `function isAndroid(): boolean`
665
+
666
+ 判断当前环境是否为 Android 设备。主要检测设备类型为 Android。
667
+
668
+ #### 返回
669
+
670
+ `(boolean)`: 如果是Android设备,返回true;否则返回false。
671
+
672
+ #### 例子
673
+
674
+ ```typescript
675
+ import { isAndroid } from '@minto-ai/tools'
676
+ console.log(isAndroid()) // 输出 true 或 false
677
+ ```
678
+
679
+ ### `isMobile`
680
+
681
+ > `function isMobile(): boolean`
682
+
683
+ 判断当前环境是否为移动设备。主要检测设备类型为移动设备。
684
+
685
+ #### 返回
686
+
687
+ `(boolean)`: 如果是移动设备,返回true;否则返回false。
688
+
689
+ #### 例子
690
+
691
+ ```typescript
692
+ import { isMobile } from '@minto-ai/tools'
693
+ console.log(isMobile()) // 输出 true 或 false
694
+ ```
695
+
696
+ ### `isPc`
697
+
698
+ > `function isPc(): boolean`
699
+
700
+ 判断当前环境是否为PC设备。主要检测设备类型为PC设备。
701
+
702
+ #### 返回
703
+
704
+ `(boolean)`: 如果是PC设备,返回true;否则返回false。
705
+
706
+ #### 例子
707
+
708
+ ```typescript
709
+ import { isPc } from '@minto-ai/tools'
710
+ console.log(isPc()) // 输出 true 或 false
711
+ ```
712
+
713
+ ## 补充
714
+
715
+ ### `getUuid`
716
+
717
+ > `function getUuid(): string`
718
+
719
+ 利用 Web Crypto API,生成一个随机的 32 位无符号整数,并将其转换为一个基于 36 进制的字符串。这种格式的字符串通常用于标识,并且可以保证在很大范围内的唯一性。
720
+
721
+ #### 返回
722
+
723
+ `(string)`: 返回一个随机生成的 UUID 字符串。
724
+
725
+ #### 例子
726
+
727
+ ```typescript
728
+ import { getUuid } from '@minto-ai/tools'
729
+
730
+ const uuid = getUuid()
731
+ console.log(uuid) // 输出类似于 "1gann4cq9b6r"
732
+ ```
733
+
734
+ ## 文件
735
+
736
+ ### 枚举
737
+
738
+ #### `FileSuffixEnum`
739
+
740
+ 一个包含常见文件后缀的枚举类型,用于统一管理文件后缀的字符串值。
741
+
742
+ | 枚举键名 | 后缀值 | 说明 |
743
+ | -------- | ------ | ------------------------------ |
744
+ | `DOC` | `doc` | Microsoft Word 文档 |
745
+ | `DOCX` | `docx` | Microsoft Word 文档 (XML 格式) |
746
+ | `PDF` | `pdf` | Portable Document Format (PDF) |
747
+ | `PPT` | `ppt` | PowerPoint 演示文稿 |
748
+ | `PPTX` | `pptx` | PowerPoint 演示文稿 (XML 格式) |
749
+ | `XLS` | `xls` | Excel 工作簿 |
750
+ | `XLSX` | `xlsx` | Excel 工作簿 (XML 格式) |
751
+ | `JPG` | `jpg` | JPEG 图像 |
752
+ | `PNG` | `png` | PNG 图像 |
753
+ | `JPEG` | `jpeg` | JPEG 图像 (另一种扩展名) |
754
+ | `WEBP` | `webp` | WebP 图像 |
755
+ | `MP4` | `mp4` | MP4 视频文件 |
756
+ | `AVI` | `avi` | AVI 视频文件 |
757
+ | `FLV` | `flv` | Flash 视频文件 |
758
+ | `MKV` | `mkv` | Matroska 视频文件 |
759
+ | `MP3` | `mp3` | MP3 音频文件 |
760
+ | `WAV` | `wav` | WAV 音频文件 |
761
+ | `TXT` | `txt` | 文本文件 |
762
+ | `HTML` | `html` | HTML 文件 |
763
+ | `NULL` | `''` | 空字符串,用于表示无后缀 |
764
+
765
+ #### `ImageFileSuffixEnum`
766
+
767
+ 一个包含常见图片文件后缀的枚举类型,继承自 `FileSuffixEnum`。
768
+
769
+ | 枚举键名 | 后缀值 | 说明 |
770
+ | -------- | ------ | --------- |
771
+ | `JPG` | `jpg` | JPEG 图像 |
772
+ | `PNG` | `png` | PNG 图像 |
773
+ | `JPEG` | `jpeg` | JPEG 图像 |
774
+ | `WEBP` | `webp` | WebP 图像 |
775
+
776
+ #### `VideoFileSuffixEnum`
777
+
778
+ 一个包含常见视频文件后缀的枚举类型,继承自 `FileSuffixEnum`。
779
+
780
+ | 枚举键名 | 后缀值 | 说明 |
781
+ | -------- | ------ | ----------------- |
782
+ | `MP4` | `mp4` | MP4 视频文件 |
783
+ | `AVI` | `avi` | AVI 视频文件 |
784
+ | `FLV` | `flv` | Flash 视频文件 |
785
+ | `MKV` | `mkv` | Matroska 视频文件 |
786
+
787
+ #### `PptFileSuffixEnum`
788
+
789
+ 一个包含常见 PPT 文件后缀的枚举类型,继承自 `FileSuffixEnum`。
790
+
791
+ | 枚举键名 | 后缀值 | 说明 |
792
+ | -------- | ------ | ------------------------------ |
793
+ | `PPT` | `ppt` | PowerPoint 演示文稿 |
794
+ | `PPTX` | `pptx` | PowerPoint 演示文稿 (XML 格式) |
795
+
796
+ #### `DocumentFileSuffixEnum`
797
+
798
+ 一个包含常见文档文件后缀的枚举类型,继承自 `FileSuffixEnum`。
799
+
800
+ | 枚举键名 | 后缀值 | 说明 |
801
+ | -------- | ------ | ------------------------------ |
802
+ | `DOC` | `doc` | Microsoft Word 文档 |
803
+ | `DOCX` | `docx` | Microsoft Word 文档 (XML 格式) |
804
+ | `PDF` | `pdf` | Portable Document Format (PDF) |
805
+ | `TXT` | `txt` | 文本文件 |
806
+ | `XLS` | `xls` | Excel 工作簿 |
807
+ | `XLSX` | `xlsx` | Excel 工作簿 (XML 格式) |
808
+
809
+ #### `FileItemFeedTypeEnum`
810
+
811
+ 一个枚举类型,用于表示文件喂养类型
812
+
813
+ | 枚举键名 | 值 | 描述 |
814
+ | ------------- | ---- | ----------------------------- |
815
+ | `ALL` | ` ` | 所有类型内容 |
816
+ | `DOCUMENT` | `0` | 文档类型内容 |
817
+ | `TEXT` | `1` | 纯文本内容 |
818
+ | `IMG` | `2` | 图片类型内容 |
819
+ | `VIDEO` | `3` | 视频类型内容 |
820
+ | `PPT` | `4` | 幻灯片(PPT)类型内容 |
821
+ | `QA` | `5` | 问答(QA)类型内容 |
822
+ | `AUDIO` | `6` | 音频类型内容 |
823
+ | `MUSIC` | `7` | 音乐类型内容 |
824
+ | `AUTOHTML` | `8` | 自动生成的 HTML 内容 |
825
+ | `TWEET` | `9` | 社交媒体推文(Tweet)类型内容 |
826
+ | `COMIC_STRIP` | `10` | 漫画(Comic Strip)类型内容 |
827
+
828
+ ### 类型
829
+
830
+ #### `IFileItem`
831
+
832
+ `IFileItem` 是一个用于描述文件信息的接口,包含文件的基本信息和相关 URL。
833
+ | 属性名 | 类型 | 描述 |
834
+ |----------|-----------|---------------------|
835
+ | `title` | `string` | 文件的标题或名称,用于显示。 |
836
+ | `viewUrl` | `string` | 文件在Obs服务器上原始地址 |
837
+ |`createTime`|`string`|文件创建时间|
838
+ |`feedType`|`FileItemFeedType`|文件喂养类型|
839
+
840
+ #### `FileSuffix`
841
+
842
+ `FileSuffix` 是一个基于 `FileSuffixEnum` 枚举的类型别名,用于表示文件后缀。
843
+
844
+ #### `ImageFileSuffix`
845
+
846
+ `ImageFileSuffix` 是一个基于 `ImageFileSuffixEnum` 枚举的类型别名,用于表示图片文件后缀。
847
+
848
+ #### `VideoFileSuffix`
849
+
850
+ `VideoFileSuffix` 是一个基于 `VideoFileSuffixEnum` 枚举的类型别名,用于表示视频文件后缀。
851
+
852
+ #### `PptFileSuffix`
853
+
854
+ `PptFileSuffix` 是一个基于 `PptFileSuffixEnum` 枚举的类型别名,用于表示 PowerPoint 文件后缀。
855
+
856
+ #### `DocumentFileSuffix`
857
+
858
+ `DocumentFileSuffix` 是一个基于 `DocumentFileSuffixEnum` 枚举的类型别名,用于表示文档文件后缀。
859
+
860
+ #### `FileItemFeedType`
861
+
862
+ `FileItemFeedType` 是一个基于 `FileItemFeedTypeEnum` 枚举的类型别名,用于表示文件类型。
863
+
864
+ ### `getFileSuffix`
865
+
866
+ > `function getFileSuffix(filePath: string): FileSuffix`
867
+
868
+ 从文件路径或文件名中提取文件后缀。
869
+
870
+ #### 参数
871
+
872
+ - `filePath (string)`: 文件路径或文件名。
873
+
874
+ #### 返回
875
+
876
+ `(FileSuffix)`: 文件后缀,以小写形式返回。如果文件没有后缀或路径无效,返回空字符串。
877
+
878
+ #### 例子
879
+
880
+ ```typescript
881
+ import { getFileSuffix } from '@minto-ai/tools'
882
+
883
+ console.log(getFileSuffix('example/document.pdf')) // "pdf"
884
+ console.log(getFileSuffix('image.jpg')) // "jpg"
885
+ console.log(getFileSuffix('no_extension')) // ""
886
+ ```
887
+
888
+ ### `getFileTitle`
889
+
890
+ > `function getFileTitle(filePath: string): string`
891
+
892
+ 从文件路径中提取文件名(不包含后缀)。
893
+
894
+ #### 参数
895
+
896
+ - `filePath (string)`: 文件路径,例如 `/path/to/file.txt`。
897
+
898
+ #### 返回
899
+
900
+ `(string)`: 文件名(不包含后缀)
901
+
902
+ #### 示例
903
+
904
+ ```typescript
905
+ import { getFileTitle } from '@minto-ai/tools'
906
+
907
+ console.log(getFileTitle('/path/to/example/document.pdf')) // "document"
908
+ console.log(getFileTitle('/path/to/image.jpg')) // "image"
909
+ console.log(getFileTitle('/path/to/no_extension')) // "no_extension"
910
+ ```
911
+
912
+ ### `getFileName`
913
+
914
+ > `function getFileName(filePath: string): string`
915
+
916
+ 从文件路径中提取完整的文件名(包含后缀)。
917
+
918
+ #### 参数
919
+
920
+ - `filePath (string)`: 文件路径,例如 `/path/to/file.txt`。
921
+
922
+ #### 返回
923
+
924
+ `(string)`: 文件名(包含后缀)。
925
+
926
+ #### 示例
927
+
928
+ ```typescript
929
+ import { getFileName } from '@minto-ai/tools'
930
+
931
+ console.log(getFileName('/path/to/example/document.pdf')) // "document.pdf"
932
+ console.log(getFileName('/path/to/image.jpg')) // "image.jpg"
933
+ console.log(getFileName('/path/to/no_extension')) // "no_extension"
934
+ ```
935
+
936
+ ### `isFilePath`
937
+
938
+ > `function isFilePath(filePath: string): boolean`
939
+
940
+ 判断是否是有效的文件路径。有效文件路径指的是文件后缀匹配到 `FileSuffixEnum` 中的任何值。
941
+
942
+ #### 参数
943
+
944
+ - `filePath (string)`: 文件路径,例如 `/path/to/file.txt`。
945
+
946
+ #### 返回
947
+
948
+ `(boolean)`: 如果文件路径的后缀匹配到 `FileSuffixEnum` 中的任何值,则返回 `true`,否则返回 `false`。
949
+
950
+ #### 示例
951
+
952
+ ```typescript
953
+ import { isFilePath } from '@minto-ai/tools'
954
+
955
+ console.log(isFilePath('/path/to/example/document.pdf')) // true
956
+ console.log(isFilePath('/path/to/image.jpg')) // true
957
+ console.log(isFilePath('/path/to/no_extension')) // false
958
+ ```
959
+
960
+ ### `isImageFilePath`
961
+
962
+ > `function isImageFilePath(filePath: string): boolean`
963
+
964
+ 判断是否是有效的图片文件路径。有效图片文件路径指的是文件后缀匹配到 `ImageFileSuffixEnum` 中的任何值。
965
+
966
+ #### 参数
967
+
968
+ - `filePath (string)`: 文件路径,例如 `/path/to/image.jpg`。
969
+
970
+ #### 返回值
971
+
972
+ `(boolean)`: 如果文件路径的后缀匹配到 `ImageFileSuffixEnum` 中的任何值,则返回 `true`,否则返回 `false`。
973
+
974
+ #### 例子
975
+
976
+ ```typescript
977
+ import { isImageFilePath } from '@minto-ai/tools'
978
+
979
+ console.log(isImageFilePath('/path/to/image.jpg')) // true
980
+ console.log(isImageFilePath('/path/to/image.png')) // true
981
+ console.log(isImageFilePath('/path/to/image.gif')) // false
982
+ console.log(isImageFilePath('/path/to/image.bmp')) // false
983
+ ```
984
+
985
+ ### `isVideoFilePath`
986
+
987
+ > `function isVideoFilePath(filePath: string): boolean`
988
+
989
+ 判断是否是有效的视频文件路径。有效视频文件路径指的是文件后缀匹配到 `VideoFileSuffixEnum` 中的任何值。
990
+
991
+ #### 参数
992
+
993
+ - `filePath (string)`: 文件路径,例如 `/path/to/video.mp4`。
994
+
995
+ #### 返回值
996
+
997
+ `(boolean)`: 如果文件路径的后缀匹配到 `VideoFileSuffixEnum` 中的任何值,则返回 `true`,否则返回 `false`。
998
+
999
+ #### 例子
1000
+
1001
+ ```typescript
1002
+ import { isVideoFilePath } from '@minto-ai/tools'
1003
+
1004
+ console.log(isVideoFilePath('/path/to/video.mp4')) // true
1005
+ console.log(isVideoFilePath('/path/to/video.avi')) // true
1006
+ ```
1007
+
1008
+ ### `isPptFilePath`
1009
+
1010
+ > `function isPptFilePath(filePath: string): boolean`
1011
+
1012
+ 判断是否是有效的文件路径。有效文件路径指的是文件后缀匹配到 `PptFileSuffixEnum` 中的任何值。
1013
+
1014
+ #### 参数
1015
+
1016
+ - `filePath (string)`: 文件路径,例如 `/path/to/presentation.ppt`。
1017
+
1018
+ #### 返回值
1019
+
1020
+ `(boolean)`: 如果文件路径的后缀匹配到 `PptFileSuffixEnum` 中的任何值,则返回 `true`,否则返回 `false`。
1021
+
1022
+ #### 例子
1023
+
1024
+ ```typescript
1025
+ import { isPptFilePath } from '@minto-ai/tools'
1026
+
1027
+ console.log(isPptFilePath('/path/to/presentation.ppt')) // true
1028
+ console.log(isPptFilePath('/path/to/presentation.pptx')) // true
1029
+ ```
1030
+
1031
+ ### `isDocumentFilePath`
1032
+
1033
+ > `function isDocumentFilePath(filePath: string): boolean`
1034
+
1035
+ 判断是否是有效的文件路径。有效文件路径指的是文件后缀匹配到 `DocumentFileSuffixEnum` 中的任何值。
1036
+
1037
+ #### 参数
1038
+
1039
+ - `filePath (string)`: 文件路径,例如 `/path/to/document.pdf`。
1040
+
1041
+ #### 返回值
1042
+
1043
+ `(boolean)`: 如果文件路径的后缀匹配到 `DocumentFileSuffixEnum` 中的任何值,则返回 `true`,否则返回 `false`。
1044
+
1045
+ #### 例子
1046
+
1047
+ ```typescript
1048
+ import { isDocumentFilePath } from '@minto-ai/tools'
1049
+
1050
+ console.log(isDocumentFilePath('/path/to/document.pdf')) // true
1051
+ console.log(isDocumentFilePath('/path/to/document.docx')) // true
1052
+ console.log(isDocumentFilePath('/path/to/document.doc')) // true
1053
+ ```
1054
+
1055
+ ### `getFileSuffixIcon`
1056
+
1057
+ > `function getFileSuffixIcon(fileSuffix: FileSuffix): string`
1058
+
1059
+ 根据文件后缀获取对应的图标。
1060
+
1061
+ #### 参数
1062
+
1063
+ - `fileSuffix (FileSuffix)`: 文件后缀,需为 `FileSuffixEnum` 中的有效值。
1064
+
1065
+ #### 返回
1066
+
1067
+ `(string)`: 对应的图标路径。
1068
+
1069
+ #### 示例
1070
+
1071
+ ```typescript
1072
+ import { getFileSuffixIcon, FileSuffixEnum } from '@minto-ai/tools'
1073
+
1074
+ console.log(getFileSuffixIcon(FileSuffixEnum.PDF)) // 返回 pdfIcon 的路径
1075
+ console.log(getFileSuffixIcon(FileSuffixEnum.JPG)) // 返回 imageIcon 的路径
1076
+ console.log(getFileSuffixIcon(FileSuffixEnum.TXT)) // 返回 txtIcon 的路径
1077
+ ```
1078
+
1079
+ ### `getFileIcon`
1080
+
1081
+ > `function getFileIcon(filePath: string): string`
1082
+
1083
+ 根据文件路径获取对应的图标
1084
+
1085
+ #### 参数
1086
+
1087
+ - `filePath`: 文件路径
1088
+
1089
+ #### 返回
1090
+
1091
+ - `string`: 图标路径
1092
+
1093
+ #### 示例
1094
+
1095
+ ```typescript
1096
+ import { getFileIcon } from '@minto-ai/tools'
1097
+
1098
+ console.log(getFileIcon('/path/to/file.pdf')) // 返回 pdfIcon 的路径
1099
+ console.log(getFileIcon('/path/to/file.txt')) // 返回 txtIcon 的路径
1100
+ ```
1101
+
1102
+ ### `getFileItemSuffix`
1103
+
1104
+ > `function getFileItemSuffix(fileItem: IFileItem): FileSuffix`
1105
+
1106
+ 从文件实例中提取文件类型后缀。该函数内部包含多种逻辑,可以直接调用或根据需求进行修改。
1107
+
1108
+ #### 参数
1109
+
1110
+ - `fileItem(IFileItem)`: 文件实例,包含文件的基本信息和路径。
1111
+
1112
+ #### 返回
1113
+
1114
+ `(FileSuffix)`: 文件类型后缀。如果无法提取有效后缀,则返回空字符串。
1115
+
1116
+ #### 示例
1117
+
1118
+ ```typescript
1119
+ import { getFileItemSuffix } from '@minto-ai/tools'
1120
+
1121
+ console.log(
1122
+ getFileItemSuffix({
1123
+ title: 'pdf文件',
1124
+ viewUrl: '/path/to/file.pdf',
1125
+ createTime: '2024-03-19 20:37:30'
1126
+ })
1127
+ ) // 返回 "pdf"
1128
+ ```
1129
+
1130
+ ### `getFileItemIcon`
1131
+
1132
+ > `function getFileItemIcon(fileItem: IFileItem): string`
1133
+
1134
+ 从文件实例中提取文件类型图标。该函数通过文件实例的后缀获取对应的图标。
1135
+
1136
+ #### 参数
1137
+
1138
+ - `fileItem (IFileItem)`: 文件实例,包含文件的基本信息和路径。
1139
+
1140
+ #### 返回
1141
+
1142
+ `(string)`: 文件类型的图标路径。如果无法提取有效后缀,则返回默认图标路径。
1143
+
1144
+ #### 示例
1145
+
1146
+ ```typescript
1147
+ import { getFileItemIcon } from '@minto-ai/tools'
1148
+
1149
+ console.log(
1150
+ getFileItemIcon({
1151
+ title: 'image文件',
1152
+ viewUrl: '/path/to/image.jpg',
1153
+ createTime: '2024-03-19 20:37:30'
1154
+ })
1155
+ ) // image图标
1156
+ ```
1157
+
1158
+ ### `getFileFeedType`
1159
+
1160
+ > `function getFileFeedType(filePath: string): FileItemFeedType | undefined`
1161
+
1162
+ 根据文件路径获取`feedType`。该函数通过文件路径提取文件后缀,并根据预定义的映射关系返回对应的`feedType`(`FileItemFeedTypeEnum`)。
1163
+
1164
+ #### 参数
1165
+
1166
+ - `filePath (string)`: 文件路径,例如 `/path/to/file.txt`。
1167
+
1168
+ #### 返回
1169
+
1170
+ `(FileItemFeedType | undefined)`: 如果文件后缀无法识别,则返回 `undefined`。
1171
+
1172
+ #### 例子
1173
+
1174
+ ```typescript
1175
+ import { getFileFeedType } from '@minto-ai/tools'
1176
+
1177
+ const filePath1 = 'https://example.com/image.jpg'
1178
+ const filePath2 = 'https://example.com/document.pdf'
1179
+ const filePath3 = 'https://example.com/unknown.file'
1180
+
1181
+ console.log(getFileFeedType(filePath1)) // 输出: FileItemFeedTypeEnum.IMG
1182
+ console.log(getFileFeedType(filePath2)) // 输出: FileItemFeedTypeEnum.TEXT
1183
+ console.log(getFileFeedType(filePath3)) // 输出: undefined
1184
+ ```
1185
+
1186
+ ### `singleDownloadFile`
1187
+
1188
+ > `function singleDownloadFile(filePath: string, fileName?: string): Promise<void>`
1189
+
1190
+ 下载单个文件。该函数通过指定的文件路径下载文件,并允许自定义下载文件的名称。
1191
+
1192
+ #### 参数
1193
+
1194
+ - `fileUrl (string)`: 文件的完整路径,例如 `https://example.com/file.pdf`。
1195
+ - `fileName (string)`: 下载时保存的文件名(可选)。如果未提供,将自动从 `filePath` 提取文件名。
1196
+
1197
+ #### 返回
1198
+
1199
+ `(Promise<void>)`: 返回一个 Promise,在文件下载完成后。
1200
+
1201
+ #### 例子
1202
+
1203
+ ```typescript
1204
+ import { singleDownloadFile } from '@minto-ai/tools'
1205
+
1206
+ // 示例 1: 使用默认文件名下载
1207
+ singleDownloadFile('https://example.com/document.pdf')
1208
+
1209
+ // 示例 2: 自定义文件名下载
1210
+ singleDownloadFile('https://example.com/document.pdf', 'custom_name.pdf')
1211
+ ```
1212
+
1213
+ ### `batchDownloadFile`
1214
+
1215
+ > `function batchDownloadFile(fileList: Array<{filePath: string;fileName?: string;}>, zipName: string):Promise<void>`
1216
+
1217
+ 批量下载文件并打包为 ZIP。该函数接受一个文件列表,每个文件包含文件路径和可选的文件名,最终将所有文件打包为一个 ZIP 文件并触发下载。
1218
+
1219
+ #### 参数
1220
+
1221
+ - `fileList (Array<{filePath: string;fileName?: string;}>)`: 文件列表,每个文件包含文件路径和可选的文件名。如果未提供文件名,将自动从文件路径中提取。
1222
+ - `zipName (string)`: 打包后的 ZIP 文件名。如果文件名不以 `.zip` 结尾,将自动添加 `.zip` 后缀。
1223
+
1224
+ #### 返回
1225
+
1226
+ `(Promise<void>)`: 返回一个 Promise,在文件打包和下载完成后。
1227
+
1228
+ #### 例子
1229
+
1230
+ ```typescript
1231
+ import { batchDownloadFile } from '@minto-ai/tools'
1232
+
1233
+ const fileList = [
1234
+ { filePath: 'https://example.com/file1.pdf', fileName: 'document1.pdf' },
1235
+ { filePath: 'https://example.com/file2.jpg' },
1236
+ { filePath: 'https://example.com/file3.txt', fileName: 'notes.txt' }
1237
+ ]
1238
+
1239
+ batchDownloadFile(fileList, 'my-files.zip')
1240
+ ```
1241
+
1242
+ ## webSocket
1243
+
1244
+ ### `isWebSocketSupported`
1245
+
1246
+ > `function isWebSocketSupported(): boolean`
1247
+
1248
+ 检查当前浏览器是否支持 WebSocket。
1249
+
1250
+ #### 返回
1251
+
1252
+ `(boolean)`: 如果浏览器支持 WebSocket,则返回 `true`;否则返回 `false`。
1253
+
1254
+ #### 例子
1255
+
1256
+ ```typescript
1257
+ import { isWebSocketSupported } from '@minto-ai/tools'
1258
+
1259
+ if (isWebSocketSupported()) {
1260
+ console.log('WebSocket is supported!')
1261
+ } else {
1262
+ console.log('WebSocket is not supported!')
1263
+ }
1264
+ ```
1265
+
1266
+ ### `createWebSocket`
1267
+
1268
+ > `function createWebSocket(url: string): WebSocket`
1269
+
1270
+ 创建一个新的 WebSocket 实例。此函数接受一个 WebSocket 服务器的 URL 并创建一个新的 WebSocket 实例。
1271
+
1272
+ #### 参数
1273
+
1274
+ - `url (string)`: WebSocket 服务器的 URL。
1275
+
1276
+ #### 返回
1277
+
1278
+ `(WebSocket)`: 返回一个新的 WebSocket 实例。
1279
+
1280
+ #### 例子
1281
+
1282
+ ```typescript
1283
+ import { createWebSocket } from '@minto-ai/tools'
1284
+
1285
+ const socket = createWebSocket('ws://example.com')
1286
+ // 使用 socket 进行通信...
1287
+ ```
1288
+
1289
+ ## worker
1290
+
1291
+ ### `createWorker`
1292
+
1293
+ > `function createWorker(fun: (...args: any) => any): Worker`
1294
+
1295
+ 创建并返回一个新的 Worker 实例,`fun`指的的任意函数,也就是要在 Worker 中执行的 JavaScript 函数。
1296
+
1297
+ #### 参数
1298
+
1299
+ - `fun (Function)`: 要在 Worker 中执行的 JavaScript 函数。
1300
+
1301
+ #### 返回
1302
+
1303
+ `(Worker)`: 返回新创建的 Worker 实例。
1304
+
1305
+ #### 例子
1306
+
1307
+ ```typescript
1308
+ import { createWorker } from '@minto-ai/tools'
1309
+
1310
+ const myWorker = createWorker(function () {
1311
+ console.log('Hello from the Web Worker!')
1312
+ })
1313
+ ```
1314
+
1315
+ ### `closeWorker`
1316
+
1317
+ > `function closeWorker(worker: Worker | undefined | null): void`
1318
+
1319
+ 关闭并终止传入 Web Worker 实例的执行,通常配合`createWorker`一起使用
1320
+
1321
+ #### 参数
1322
+
1323
+ - `worker (Worker | undefined | null)`: 要关闭的 Worker 实例。
1324
+
1325
+ #### 例子
1326
+
1327
+ ```typescript
1328
+ import { createWorker, closeWorker } from '@minto-ai/tools'
1329
+
1330
+ const myWorker = createWorker(function () {
1331
+ console.log('Hello from the Web Worker!')
1332
+ })
1333
+ // 当不再需要 Worker 时
1334
+ closeWorker(myWorker)
1335
+ ```