@mingto/tools 1.0.675

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 (105) hide show
  1. package/README.md +1615 -0
  2. package/dist/array/chunk-array.d.ts +11 -0
  3. package/dist/array/index.d.ts +2 -0
  4. package/dist/browser/copy-text.d.ts +8 -0
  5. package/dist/browser/create-audio-permission.d.ts +186 -0
  6. package/dist/browser/get-absolute-url.d.ts +7 -0
  7. package/dist/browser/get-browser-fingerprint.d.ts +10 -0
  8. package/dist/browser/index.d.ts +9 -0
  9. package/dist/browser/is-android.d.ts +8 -0
  10. package/dist/browser/is-ios.d.ts +8 -0
  11. package/dist/browser/is-mobile.d.ts +8 -0
  12. package/dist/browser/is-pc.d.ts +8 -0
  13. package/dist/constant/image.d.ts +2 -0
  14. package/dist/constant/index.d.ts +1 -0
  15. package/dist/core/__test__/can-be-parsed-as-number.test.d.ts +1 -0
  16. package/dist/core/be-parsed-as-number.d.ts +8 -0
  17. package/dist/core/can-be-parsed-as-number.d.ts +8 -0
  18. package/dist/core/check-array-empty.d.ts +8 -0
  19. package/dist/core/check-empty-not-zero.d.ts +8 -0
  20. package/dist/core/check-empty.d.ts +8 -0
  21. package/dist/core/check-object-empty.d.ts +8 -0
  22. package/dist/core/index.d.ts +15 -0
  23. package/dist/core/is-array.d.ts +8 -0
  24. package/dist/core/is-boolean.d.ts +8 -0
  25. package/dist/core/is-function.d.ts +8 -0
  26. package/dist/core/is-null-or-undefined.d.ts +7 -0
  27. package/dist/core/is-number.d.ts +8 -0
  28. package/dist/core/is-object.d.ts +9 -0
  29. package/dist/core/is-string.d.ts +8 -0
  30. package/dist/core/is-undefined.d.ts +8 -0
  31. package/dist/file/__test__/get-file-suffix.test.d.ts +1 -0
  32. package/dist/file/__test__/is-file-path.test.d.ts +1 -0
  33. package/dist/file/batch-download-file.d.ts +12 -0
  34. package/dist/file/extends/enum.d.ts +81 -0
  35. package/dist/file/extends/regex.d.ts +2 -0
  36. package/dist/file/get-file-icon.d.ts +8 -0
  37. package/dist/file/get-file-name.d.ts +8 -0
  38. package/dist/file/get-file-path-not-query.d.ts +8 -0
  39. package/dist/file/get-file-query.d.ts +8 -0
  40. package/dist/file/get-file-suffix-icon.d.ts +9 -0
  41. package/dist/file/get-file-suffix.d.ts +9 -0
  42. package/dist/file/get-file-title.d.ts +8 -0
  43. package/dist/file/index.d.ts +22 -0
  44. package/dist/file/is-compress-file-path.d.ts +8 -0
  45. package/dist/file/is-document-file-path.d.ts +8 -0
  46. package/dist/file/is-file-path.d.ts +8 -0
  47. package/dist/file/is-image-file-path.d.ts +8 -0
  48. package/dist/file/is-music-file-path.d.ts +8 -0
  49. package/dist/file/is-ppt-file-path.d.ts +8 -0
  50. package/dist/file/is-video-file-path.d.ts +8 -0
  51. package/dist/file/single-download-file-base64.d.ts +8 -0
  52. package/dist/file/single-download-file.d.ts +12 -0
  53. package/dist/file/types/index.d.ts +9 -0
  54. package/dist/file/update-file-path-query.d.ts +8 -0
  55. package/dist/function/debounce.d.ts +23 -0
  56. package/dist/function/index.d.ts +3 -0
  57. package/dist/function/throttle.d.ts +23 -0
  58. package/dist/i18n/index.d.ts +32 -0
  59. package/dist/i18n/locales/ar.d.ts +18 -0
  60. package/dist/i18n/locales/en.d.ts +18 -0
  61. package/dist/i18n/locales/ms.d.ts +18 -0
  62. package/dist/i18n/locales/th.d.ts +18 -0
  63. package/dist/i18n/locales/vi.d.ts +13 -0
  64. package/dist/i18n/locales/zh-cn.d.ts +18 -0
  65. package/dist/i18n/locales/zh-tw.d.ts +18 -0
  66. package/dist/i18n/setup.d.ts +4 -0
  67. package/dist/index.d.ts +28 -0
  68. package/dist/index.js +16971 -0
  69. package/dist/math/__test__/add.test.d.ts +1 -0
  70. package/dist/math/__test__/divide.test.d.ts +1 -0
  71. package/dist/math/__test__/get-decimal-places.test.d.ts +1 -0
  72. package/dist/math/__test__/multiply.test.d.ts +1 -0
  73. package/dist/math/__test__/subtract.test.d.ts +1 -0
  74. package/dist/math/add.d.ts +9 -0
  75. package/dist/math/calc-aspect-ratio.d.ts +9 -0
  76. package/dist/math/divide.d.ts +9 -0
  77. package/dist/math/get-decimal-places.d.ts +8 -0
  78. package/dist/math/index.d.ts +7 -0
  79. package/dist/math/multiply.d.ts +9 -0
  80. package/dist/math/subtract.d.ts +9 -0
  81. package/dist/object/deep-freeze.d.ts +9 -0
  82. package/dist/object/index.d.ts +3 -0
  83. package/dist/object/pick-object.d.ts +9 -0
  84. package/dist/string/__test__/chunk-string.test.d.ts +1 -0
  85. package/dist/string/chunk-string.d.ts +10 -0
  86. package/dist/string/index.d.ts +4 -0
  87. package/dist/string/object-to-query-string.d.ts +10 -0
  88. package/dist/string/query-string-to-object.d.ts +8 -0
  89. package/dist/supply/get-uuid.d.ts +7 -0
  90. package/dist/supply/index.d.ts +3 -0
  91. package/dist/supply/time-delay.d.ts +7 -0
  92. package/dist/supply/type-writer.d.ts +18 -0
  93. package/dist/theme/index.d.ts +1 -0
  94. package/dist/theme/use-theme.d.ts +23 -0
  95. package/dist/validator/index.d.ts +3 -0
  96. package/dist/validator/is-valid-base64.d.ts +6 -0
  97. package/dist/validator/is-valid-url.d.ts +7 -0
  98. package/dist/validator/test/is-valid-url.test.d.ts +1 -0
  99. package/dist/web-socket/create-web-socket.d.ts +9 -0
  100. package/dist/web-socket/index.d.ts +3 -0
  101. package/dist/web-socket/is-web-socket-supported.d.ts +9 -0
  102. package/dist/wrker/close-worker.d.ts +9 -0
  103. package/dist/wrker/create-worker.d.ts +11 -0
  104. package/dist/wrker/index.d.ts +3 -0
  105. package/package.json +31 -0
package/README.md ADDED
@@ -0,0 +1,1615 @@
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
+ ### `objectToQueryString`
331
+
332
+ > `function objectToQueryString(obj: Record<string, any>): string`
333
+
334
+ 将对象转换为 URL 查询字符串,自动进行键值的 URL 编码并以 `key=value` 使用 `&` 连接。
335
+
336
+ #### 参数
337
+
338
+ - `obj (Record<string, any>)`: 需要转换的对象。
339
+
340
+ #### 返回
341
+
342
+ `(string)`: 转换后的查询字符串。
343
+
344
+ #### 例子
345
+
346
+ ```typescript
347
+ import { objectToQueryString } from '@minto-ai/tools'
348
+
349
+ objectToQueryString({ a: 1, b: 'x y' }) // 'a=1&b=x%20y'
350
+ ```
351
+
352
+ ### `queryStringToObject`
353
+
354
+ > `function queryStringToObject(queryString: string): Record<string, any>`
355
+
356
+ 将 URL 查询字符串转换为对象,自动对键值进行 URL 解码。
357
+
358
+ #### 参数
359
+
360
+ - `queryString (string)`: 需要解析的查询字符串,例如 `a=1&b=x%20y`。
361
+
362
+ #### 返回
363
+
364
+ `(Record<string, any>)`: 解析后的对象。
365
+
366
+ #### 例子
367
+
368
+ ```typescript
369
+ import { queryStringToObject } from '@minto-ai/tools'
370
+
371
+ queryStringToObject('a=1&b=x%20y') // { a: '1', b: 'x y' }
372
+ ```
373
+
374
+ ## 函数
375
+
376
+ ### `throttle`
377
+
378
+ > `function throttle<T extends (...args: any[]) => any>(callback: T, delay: number): ThrottledFunction<T>`
379
+
380
+ 节流函数用于限制一个函数的执行频率。它确保一个函数在指定的时间间隔内最多只执行一次。
381
+
382
+ #### 参数
383
+
384
+ - `callback (Function)`: 要节流的函数。
385
+ - `delay (number)`: 执行频率的延迟时间(以毫秒为单位)。
386
+
387
+ #### 返回
388
+
389
+ `(Function)`: 返回一个节流后的函数,包含原函数的参数类型和一个 cancel 方法,用于取消未执行的部分。
390
+
391
+ #### 例子
392
+
393
+ ```typescript
394
+ import { throttle } from '@minto-ai/tools'
395
+
396
+ // 定义一个需要节流的函数
397
+ function handleScroll() {
398
+ console.log('Scroll event triggered')
399
+ }
400
+
401
+ // 创建节流后的函数,每 500 毫秒最多执行一次
402
+ const throttledScroll = throttle(handleScroll, 500)
403
+
404
+ // 绑定到事件监听器
405
+ window.addEventListener('scroll', throttledScroll)
406
+
407
+ // 取消节流函数的未执行部分
408
+ throttledScroll.cancel()
409
+ ```
410
+
411
+ ### `debounce`
412
+
413
+ > `function debounce<T extends (...args: any[]) => any>(callback: T, delay: number): DebouncedFunction<T>`
414
+
415
+ 防抖函数用于延迟函数的执行,直到指定时间内不再触发事件。如果在延迟时间内再次触发事件,则会重新计时。
416
+
417
+ #### 参数
418
+
419
+ - `callback(Function)`: 要防抖的函数。
420
+ - `delay(number)`: 执行频率的延迟时间(以毫秒为单位)。
421
+
422
+ #### 返回
423
+
424
+ `(Function)`: 返回一个防抖后的函数,包含原函数的参数类型和一个 cancel 方法,用于取消未执行的部分。
425
+
426
+ #### 例子
427
+
428
+ ```typescript
429
+ import { debounce } from '@minto-ai/tools'
430
+
431
+ // 定义一个需要防抖的函数
432
+ function handleResize() {
433
+ console.log('Window resized')
434
+ }
435
+
436
+ // 创建防抖后的函数,延迟 300 毫秒执行
437
+ const debouncedResize = debounce(handleResize, 300)
438
+
439
+ // 绑定到事件监听器
440
+ window.addEventListener('resize', debouncedResize)
441
+
442
+ // 取消防抖函数的未执行部分
443
+ debouncedResize.cancel()
444
+ ```
445
+
446
+ ## 对象
447
+
448
+ ### `deepFreeze`
449
+
450
+ > `function deepFreeze<T extends Record<string, any>>(obj: T): Readonly<T>`
451
+
452
+ 深度冻结一个对象,防止其属性及嵌套属性被修改。递归地冻结对象的所有属性,包括嵌套对象,确保对象的状态不可变。
453
+
454
+ #### 参数
455
+
456
+ - `value (T)`: 需要被冻结的对象。
457
+
458
+ #### 返回
459
+
460
+ `(Readonly<T>)`: 返回深度冻结后的对象。
461
+
462
+ #### 例子
463
+
464
+ ```typescript
465
+ import { deepFreeze } from '@minto-ai/tools'
466
+
467
+ const obj = { name: 'Kimi', details: { age: 30 } }
468
+ const frozenObj = deepFreeze(obj)
469
+ console.log(Object.isFrozen(frozenObj)) // 输出:true
470
+ console.log(Object.isFrozen(frozenObj.details)) // 输出:true
471
+ ```
472
+
473
+ ### `pickObject`
474
+
475
+ > `function pickObject<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>`
476
+
477
+ 从给定对象中挑选指定的属性,并返回一个包含这些属性的新对象。
478
+
479
+ #### 参数
480
+
481
+ - `obj (T)`: 源对象,从中挑选属性。
482
+ - `keys (K[])`: 需要挑选的属性键名数组。
483
+
484
+ #### 返回
485
+
486
+ `(Readonly<T>)`: 返回深度冻结后的对象。
487
+
488
+ #### 例子
489
+
490
+ ```typescript
491
+ import { pickObject } from '@minto-ai/tools'
492
+
493
+ const user = {
494
+ name: 'Kimi',
495
+ age: 30,
496
+ email: 'kimi@example.com'
497
+ }
498
+
499
+ const pickedUser = pickObject(user, ['name', 'email'])
500
+ console.log(pickedUser) // 输出:{ name: 'Kimi', email: 'kimi@example.com' }
501
+ ```
502
+
503
+ ## 数组
504
+
505
+ ### `chunkArray`
506
+
507
+ > `function chunkArray<T>(array: T[], chunkSize?: number): T[][]`
508
+
509
+ 这个函数接受一个数组和一个指定的块大小,然后返回一个二维数组。每个子数组包含原数组中的一部分元素,大小由 `chunkSize` 参数决定。如果数组的长度不是 `chunkSize` 的整数倍,最后一个块可能包含的元素少于 `chunkSize`。
510
+
511
+ #### 参数
512
+
513
+ - `array (Array)`: 要分割的源数组。
514
+ - `[chunkSize=1] (number)`: 每个块的大小,默认值为 1。
515
+
516
+ #### 返回
517
+
518
+ `(Array)`: 返回一个二维数组,每个子数组是原数组的一部分元素。
519
+
520
+ #### 例子
521
+
522
+ ```typescript
523
+ import { chunkArray } from '@minto-ai/tools'
524
+
525
+ chunkArray(['a', 'b', 'c', 'd'], 2)
526
+ // [['a', 'b'], ['c', 'd']]
527
+
528
+ chunkArray(['a', 'b', 'c', 'd', 'e'], 3)
529
+ // [['a', 'b', 'c'], ['d', 'e']]
530
+
531
+ chunkArray([1, 2, 3, 4, 5], 4)
532
+ // [[1, 2, 3, 4], [5]]
533
+ ```
534
+
535
+ ## 数学
536
+
537
+ ### `getDecimalPlaces`
538
+
539
+ > `function getDecimalPlaces(num: number): number`
540
+
541
+ 获取一个数字的小数位数
542
+
543
+ #### 参数
544
+
545
+ - `num (number)`: 需要获取小数位数的数字。
546
+
547
+ #### 返回
548
+
549
+ `(number)`: 返回一个数字的小数位数。
550
+
551
+ #### 例子
552
+
553
+ ```typescript
554
+ import { getDecimalPlaces } from '@minto-ai/tools'
555
+
556
+ console.log(getDecimalPlaces(123.456)) // 3
557
+ console.log(getDecimalPlaces(123)) // 0
558
+ console.log(getDecimalPlaces(0.001)) // 3
559
+ console.log(getDecimalPlaces(-123.45)) // 2
560
+ ```
561
+
562
+ ### `add`
563
+
564
+ > `function add(augend: number, addend: number): number`
565
+
566
+ 精确地将两个数字相加,避免 JavaScript 中浮点数运算的精度问题。
567
+
568
+ #### 参数
569
+
570
+ - `augend (number)`: 第一个数字(被加数)。
571
+ - `addend (number)`: 第二个数字(加数)。
572
+
573
+ #### 返回
574
+
575
+ `(number)`: 返回两个数字的精确和。
576
+
577
+ #### 例子
578
+
579
+ ```typescript
580
+ import { add } from '@minto-ai/tools'
581
+
582
+ console.log(add(0.1, 0.2)) // 0.3
583
+ console.log(add(1.234, 5.678)) // 6.912
584
+ console.log(add(100, 0.001)) // 100.001
585
+ ```
586
+
587
+ ### `subtract`
588
+
589
+ > `function subtract(minuend: number, subtrahend: number): number`
590
+
591
+ 精确地减去两个数字,避免 JavaScript 中浮点数运算的精度问题。
592
+
593
+ #### 参数
594
+
595
+ - `minuend (number)`: 第一个数字(被减数)。
596
+ - `subtrahend (number)`: 第二个数字(减数)。
597
+
598
+ #### 返回
599
+
600
+ `(number)`: 返回两个数字的精确差。
601
+
602
+ #### 例子
603
+
604
+ ```typescript
605
+ import { subtract } from '@minto-ai/tools'
606
+
607
+ console.log(subtract(0.3, 0.1)) // 0.2
608
+ console.log(subtract(1.234, 0.567)) // 0.667
609
+ console.log(subtract(100.001, 0.001)) // 100
610
+ ```
611
+
612
+ ### `multiply`
613
+
614
+ > `function multiply(multiplier: number, multiplicand: number): number`
615
+
616
+ 精确地乘以两个数字,避免 JavaScript 中浮点数运算的精度问题。
617
+
618
+ #### 参数
619
+
620
+ - `multiplier (number)`: 第一个数字(乘数)。
621
+ - `multiplicand (number)`: 第二个数字(被乘数)。
622
+
623
+ #### 返回
624
+
625
+ `(number)`: 返回两个数字的精确积。
626
+
627
+ #### 例子
628
+
629
+ ```typescript
630
+ import { multiply } from '@minto-ai/tools'
631
+
632
+ console.log(multiply(0.1, 0.2)) // 0.02
633
+ console.log(multiply(1.234, 5.678)) // 7.006652
634
+ console.log(multiply(100.001, 0.001)) // 0.100001
635
+ ```
636
+
637
+ ### `divide`
638
+
639
+ > `function divide(dividend: number, divisor: number): number`
640
+
641
+ 精确地除以两个数字,避免 JavaScript 中浮点数运算的精度问题。
642
+
643
+ #### 参数
644
+
645
+ - `dividend (number)`: 第一个数字(被除数)。
646
+ - `divisor (number)`: 第二个数字(除数)。
647
+
648
+ #### 返回
649
+
650
+ `(number)`: 返回两个数字的精确商。
651
+
652
+ #### 例子
653
+
654
+ ```typescript
655
+ import { divide } from '@minto-ai/tools'
656
+
657
+ console.log(divide(10, 2)) // 5
658
+ console.log(divide(1.234, 0.567)) // 2.1763668430335097
659
+ console.log(divide(100.001, 0.001)) // 100001
660
+ ```
661
+
662
+ ### `calcAspectRatio`
663
+
664
+ > `function calcAspectRatio(size: [number, number]): [number, number]`
665
+
666
+ 计算宽高的最简整数比例,常用于等比缩放或显示比例展示。
667
+
668
+ #### 参数
669
+
670
+ - `size ([number, number])`: 数组形式的宽高值。
671
+
672
+ #### 返回
673
+
674
+ `([number, number])`: 最简整数比例,例如 `[16, 9]`。
675
+
676
+ #### 例子
677
+
678
+ ```typescript
679
+ import { calcAspectRatio } from '@minto-ai/tools'
680
+
681
+ calcAspectRatio([1920, 1080]) // [16, 9]
682
+ calcAspectRatio([1280, 800]) // [16, 10]
683
+ ```
684
+
685
+ ## 浏览器
686
+
687
+ ### `copyText`
688
+
689
+ > `function copyText(text: string): Promise<string>`
690
+
691
+ 将指定的文本复制到剪贴板。
692
+
693
+ #### 参数
694
+
695
+ - `text (string)`: 需要复制到剪贴板的文本。
696
+
697
+ #### 返回
698
+
699
+ `Promise<string>`: 一个 Promise 对象,当文本成功复制到剪贴板时解析为被复制的文本,如果复制失败则拒绝 Promise。
700
+
701
+ #### 例子
702
+
703
+ ```typescript
704
+ import { copyText } from '@minto-ai/tools'
705
+
706
+ copyText('Hello, World!').then(
707
+ text => console.log('Text copied to clipboard:', text),
708
+ error => console.error('Failed to copy text:', error)
709
+ )
710
+ ```
711
+
712
+ ### `isIos`
713
+
714
+ > `function isIos(): boolean`
715
+
716
+ 判断当前环境是否为 iOS 设备。主要检测的设备类型包括 iPhone、iPad 和 iPod。
717
+
718
+ #### 返回
719
+
720
+ `(boolean)`: 如果是iOS设备,返回true;否则返回false。
721
+
722
+ #### 例子
723
+
724
+ ```typescript
725
+ import { isIos } from '@minto-ai/tools'
726
+
727
+ console.log(isIos()) // 输出 true 或 false
728
+ ```
729
+
730
+ ### `isAndroid`
731
+
732
+ > `function isAndroid(): boolean`
733
+
734
+ 判断当前环境是否为 Android 设备。主要检测设备类型为 Android。
735
+
736
+ #### 返回
737
+
738
+ `(boolean)`: 如果是Android设备,返回true;否则返回false。
739
+
740
+ #### 例子
741
+
742
+ ```typescript
743
+ import { isAndroid } from '@minto-ai/tools'
744
+
745
+ console.log(isAndroid()) // 输出 true 或 false
746
+ ```
747
+
748
+ ### `isMobile`
749
+
750
+ > `function isMobile(): boolean`
751
+
752
+ 判断当前环境是否为移动设备。主要检测设备类型为移动设备。
753
+
754
+ #### 返回
755
+
756
+ `(boolean)`: 如果是移动设备,返回true;否则返回false。
757
+
758
+ #### 例子
759
+
760
+ ```typescript
761
+ import { isMobile } from '@minto-ai/tools'
762
+
763
+ console.log(isMobile()) // 输出 true 或 false
764
+ ```
765
+
766
+ ### `isPc`
767
+
768
+ > `function isPc(): boolean`
769
+
770
+ 判断当前环境是否为PC设备。主要检测设备类型为PC设备。
771
+
772
+ #### 返回
773
+
774
+ `(boolean)`: 如果是PC设备,返回true;否则返回false。
775
+
776
+ #### 例子
777
+
778
+ ```typescript
779
+ import { isPc } from '@minto-ai/tools'
780
+
781
+ console.log(isPc()) // 输出 true 或 false
782
+ ```
783
+
784
+ ### `createAudioPermission`
785
+
786
+ > `function createAudioPermission(): AudioPermission`
787
+
788
+ 跨平台音频权限管理器,统一请求音频播放权限与麦克风权限,处理 PC/iOS/Android 在自动播放与权限授权上的差异。
789
+
790
+ #### 返回
791
+
792
+ `(AudioPermission)`: 返回单例的权限管理器,包含 `requestPlaybackPermission()` 与 `requestMicrophonePermission()` 两个方法。
793
+
794
+ #### 例子
795
+
796
+ ```typescript
797
+ import { createAudioPermission } from '@minto-ai/tools'
798
+
799
+ const audioPermission = createAudioPermission()
800
+
801
+ // 请求音频播放权限(需在用户交互后调用)
802
+ await audioPermission.requestPlaybackPermission()
803
+
804
+ // 请求麦克风权限
805
+ await audioPermission.requestMicrophonePermission()
806
+ ```
807
+
808
+ ### `getBrowserFingerprint`
809
+
810
+ > `function getBrowserFingerprint(): string`
811
+
812
+ 生成匿名浏览器指纹,用于区分浏览器匿名用户的唯一 ID。通过收集浏览器环境特征(如 UserAgent、语言、屏幕分辨率、色深、时区、硬件并发、平台,以及 Canvas 渲染指纹等),并使用 MurmurHash3(32 位)生成稳定的十六进制字符串。
813
+
814
+ #### 参数
815
+
816
+ - 无
817
+
818
+ #### 返回
819
+
820
+ `(string)`: 浏览器指纹 ID(十六进制字符串)。在非浏览器环境(如 Node/SSR)返回空字符串 `''`。
821
+
822
+ #### 例子
823
+
824
+ ```typescript
825
+ import { getBrowserFingerprint } from '@minto-ai/tools'
826
+
827
+ // 生成匿名用户唯一ID
828
+ const fingerprint = getBrowserFingerprint()
829
+ console.log(fingerprint) // 例如:'a1b2c3d4'
830
+
831
+ // 建议:结合本地存储缓存以保持一致性
832
+ localStorage.setItem('anonymous_id', fingerprint)
833
+ ```
834
+
835
+ #### 注意事项
836
+
837
+ - 指纹可能随浏览器升级、系统变更或渲染差异而变化,适合作为匿名用户的临时唯一标识,不建议用于强身份识别。
838
+ - 隐私增强模式或跨平台差异可能禁用部分特征(如 deviceMemory、Canvas),函数会自动兼容并尽可能生成可辨识的 ID。
839
+
840
+ ### `getAbsoluteUrl`
841
+
842
+ > `function getAbsoluteUrl(rawUrl: string): string`
843
+
844
+ 将任意格式的 URL 路径规范化为完整的绝对 URL 地址。
845
+
846
+ #### 参数
847
+
848
+ - `rawUrl (string)`: 待规范化的原始 URL 路径,支持空值、相对路径、绝对路径等格式。
849
+
850
+ #### 返回
851
+
852
+ `(string)`: 规范化后的绝对 URL 字符串,空输入返回空字符串。
853
+
854
+ #### 例子
855
+
856
+ ```typescript
857
+ import { getAbsoluteUrl } from '@minto-ai/tools'
858
+
859
+ // 假设当前页面 URL 为 https://example.com/page
860
+
861
+ getAbsoluteUrl('/api/data') // 'https://example.com/api/data'
862
+ getAbsoluteUrl('https://other.com/api') // 'https://other.com/api'
863
+ getAbsoluteUrl('') // ''
864
+ ```
865
+
866
+ ## 工具
867
+
868
+ ### `getUuid`
869
+
870
+ > `function getUuid(): string`
871
+
872
+ 利用 Web Crypto API,生成一个随机的 32 位无符号整数,并将其转换为一个基于 36 进制的字符串。这种格式的字符串通常用于标识,并且可以保证在很大范围内的唯一性。
873
+
874
+ #### 返回
875
+
876
+ `(string)`: 返回一个随机生成的 UUID 字符串。
877
+
878
+ #### 例子
879
+
880
+ ```typescript
881
+ import { getUuid } from '@minto-ai/tools'
882
+
883
+ const uuid = getUuid()
884
+ console.log(uuid) // 输出类似于 "1gann4cq9b6r"
885
+ ```
886
+
887
+ ### `timeDelay`
888
+
889
+ > `function timeDelay(delay: number): Promise<void>`
890
+
891
+ 创建一个延迟执行的 Promise,常用于等待动画或节流场景中的异步等待。
892
+
893
+ #### 参数
894
+
895
+ - `delay (number)`: 延迟时间,单位毫秒,必须为非负数。
896
+
897
+ #### 返回
898
+
899
+ `(Promise<void>)`: 在指定时间后解析的 Promise。
900
+
901
+ #### 例子
902
+
903
+ ```typescript
904
+ import { timeDelay } from '@minto-ai/tools'
905
+
906
+ await timeDelay(300)
907
+ // 继续后续逻辑
908
+ ```
909
+
910
+ ## 文件
911
+
912
+ ### 枚举
913
+
914
+ #### `FileSuffixEnum`
915
+
916
+ 一个包含常见文件后缀的枚举类型,用于统一管理文件后缀的字符串值。
917
+
918
+ | 枚举键名 | 后缀值 | 说明 |
919
+ | -------- | ------ | ------------------------------ |
920
+ | `DOC` | `doc` | Microsoft Word 文档 |
921
+ | `DOCX` | `docx` | Microsoft Word 文档 (XML 格式) |
922
+ | `PDF` | `pdf` | Portable Document Format (PDF) |
923
+ | `PPT` | `ppt` | PowerPoint 演示文稿 |
924
+ | `PPTX` | `pptx` | PowerPoint 演示文稿 (XML 格式) |
925
+ | `XLS` | `xls` | Excel 工作簿 |
926
+ | `XLSX` | `xlsx` | Excel 工作簿 (XML 格式) |
927
+ | `JSON` | `json` | JSON 数据文件 |
928
+ | `JPG` | `jpg` | JPEG 图像 |
929
+ | `PNG` | `png` | PNG 图像 |
930
+ | `JPEG` | `jpeg` | JPEG 图像 (另一种扩展名) |
931
+ | `WEBP` | `webp` | WebP 图像 |
932
+ | `MP4` | `mp4` | MP4 视频文件 |
933
+ | `AVI` | `avi` | AVI 视频文件 |
934
+ | `FLV` | `flv` | Flash 视频文件 |
935
+ | `MKV` | `mkv` | Matroska 视频文件 |
936
+ | `MP3` | `mp3` | MP3 音频文件 |
937
+ | `WAV` | `wav` | WAV 音频文件 |
938
+ | `TXT` | `txt` | 文本文件 |
939
+ | `HTML` | `html` | HTML 文件 |
940
+ | `NULL` | `''` | 空字符串,用于表示无后缀 |
941
+
942
+ #### `ImageFileSuffixEnum`
943
+
944
+ 一个包含常见图片文件后缀的枚举类型,继承自 `FileSuffixEnum`。
945
+
946
+ | 枚举键名 | 后缀值 | 说明 |
947
+ | -------- | ------ | --------- |
948
+ | `JPG` | `jpg` | JPEG 图像 |
949
+ | `PNG` | `png` | PNG 图像 |
950
+ | `JPEG` | `jpeg` | JPEG 图像 |
951
+ | `WEBP` | `webp` | WebP 图像 |
952
+
953
+ #### `VideoFileSuffixEnum`
954
+
955
+ 一个包含常见视频文件后缀的枚举类型,继承自 `FileSuffixEnum`。
956
+
957
+ | 枚举键名 | 后缀值 | 说明 |
958
+ | -------- | ------ | ----------------- |
959
+ | `MP4` | `mp4` | MP4 视频文件 |
960
+ | `AVI` | `avi` | AVI 视频文件 |
961
+ | `FLV` | `flv` | Flash 视频文件 |
962
+ | `MKV` | `mkv` | Matroska 视频文件 |
963
+
964
+ #### `PptFileSuffixEnum`
965
+
966
+ 一个包含常见 PPT 文件后缀的枚举类型,继承自 `FileSuffixEnum`。
967
+
968
+ | 枚举键名 | 后缀值 | 说明 |
969
+ | -------- | ------ | ------------------------------ |
970
+ | `PPT` | `ppt` | PowerPoint 演示文稿 |
971
+ | `PPTX` | `pptx` | PowerPoint 演示文稿 (XML 格式) |
972
+
973
+ #### `DocumentFileSuffixEnum`
974
+
975
+ 一个包含常见文档文件后缀的枚举类型,继承自 `FileSuffixEnum`。
976
+
977
+ | 枚举键名 | 后缀值 | 说明 |
978
+ | -------- | ------ | ------------------------------ |
979
+ | `DOC` | `doc` | Microsoft Word 文档 |
980
+ | `DOCX` | `docx` | Microsoft Word 文档 (XML 格式) |
981
+ | `PDF` | `pdf` | Portable Document Format (PDF) |
982
+ | `TXT` | `txt` | 文本文件 |
983
+ | `XLS` | `xls` | Excel 工作簿 |
984
+ | `XLSX` | `xlsx` | Excel 工作簿 (XML 格式) |
985
+ | `JSON` | `json` | JSON 数据文件 |
986
+
987
+ #### `MusicFileSuffixEnum`
988
+
989
+ 常见音乐文件后缀的枚举类型。
990
+
991
+ | 枚举键名 | 后缀值 | 说明 |
992
+ | -------- | ------ | ------------ |
993
+ | `MP3` | `mp3` | MP3 音频文件 |
994
+ | `WAV` | `wav` | WAV 音频文件 |
995
+
996
+ #### `CompressFileSuffixEnum`
997
+
998
+ 常见压缩文件后缀的枚举类型。
999
+
1000
+ | 枚举键名 | 后缀值 | 说明 |
1001
+ | -------- | ------ | ------------ |
1002
+ | `ZIP` | `zip` | ZIP 压缩文件 |
1003
+ | `RAR` | `rar` | RAR 压缩文件 |
1004
+
1005
+ ### 类型
1006
+
1007
+ #### `FileSuffix`
1008
+
1009
+ `FileSuffix` 是一个基于 `FileSuffixEnum` 枚举的类型别名,用于表示文件后缀。
1010
+
1011
+ #### `ImageFileSuffix`
1012
+
1013
+ `ImageFileSuffix` 是一个基于 `ImageFileSuffixEnum` 枚举的类型别名,用于表示图片文件后缀。
1014
+
1015
+ #### `VideoFileSuffix`
1016
+
1017
+ `VideoFileSuffix` 是一个基于 `VideoFileSuffixEnum` 枚举的类型别名,用于表示视频文件后缀。
1018
+
1019
+ #### `PptFileSuffix`
1020
+
1021
+ `PptFileSuffix` 是一个基于 `PptFileSuffixEnum` 枚举的类型别名,用于表示 PowerPoint 文件后缀。
1022
+
1023
+ #### `DocumentFileSuffix`
1024
+
1025
+ `DocumentFileSuffix` 是一个基于 `DocumentFileSuffixEnum` 枚举的类型别名,用于表示文档文件后缀。
1026
+
1027
+ ### `getFileSuffix`
1028
+
1029
+ > `function getFileSuffix(filePath: string): FileSuffix`
1030
+
1031
+ 从文件路径或文件名中提取文件后缀。
1032
+
1033
+ #### 参数
1034
+
1035
+ - `filePath (string)`: 文件路径或文件名。
1036
+
1037
+ #### 返回
1038
+
1039
+ `(FileSuffix)`: 文件后缀,以小写形式返回。如果文件没有后缀或路径无效,返回空字符串。
1040
+
1041
+ #### 例子
1042
+
1043
+ ```typescript
1044
+ import { getFileSuffix } from '@minto-ai/tools'
1045
+
1046
+ console.log(getFileSuffix('example/document.pdf')) // "pdf"
1047
+ console.log(getFileSuffix('image.jpg')) // "jpg"
1048
+ console.log(getFileSuffix('no_extension')) // ""
1049
+ ```
1050
+
1051
+ ### `getFileTitle`
1052
+
1053
+ > `function getFileTitle(filePath: string): string`
1054
+
1055
+ 从文件路径中提取文件名(不包含后缀)。
1056
+
1057
+ #### 参数
1058
+
1059
+ - `filePath (string)`: 文件路径,例如 `/path/to/file.txt`。
1060
+
1061
+ #### 返回
1062
+
1063
+ `(string)`: 文件名(不包含后缀)
1064
+
1065
+ #### 示例
1066
+
1067
+ ```typescript
1068
+ import { getFileTitle } from '@minto-ai/tools'
1069
+
1070
+ console.log(getFileTitle('/path/to/example/document.pdf')) // "document"
1071
+ console.log(getFileTitle('/path/to/image.jpg')) // "image"
1072
+ console.log(getFileTitle('/path/to/no_extension')) // "no_extension"
1073
+ ```
1074
+
1075
+ ### `getFileName`
1076
+
1077
+ > `function getFileName(filePath: string): string`
1078
+
1079
+ 从文件路径中提取完整的文件名(包含后缀)。
1080
+
1081
+ #### 参数
1082
+
1083
+ - `filePath (string)`: 文件路径,例如 `/path/to/file.txt`。
1084
+
1085
+ #### 返回
1086
+
1087
+ `(string)`: 文件名(包含后缀)。
1088
+
1089
+ #### 示例
1090
+
1091
+ ```typescript
1092
+ import { getFileName } from '@minto-ai/tools'
1093
+
1094
+ console.log(getFileName('/path/to/example/document.pdf')) // "document.pdf"
1095
+ console.log(getFileName('/path/to/image.jpg')) // "image.jpg"
1096
+ console.log(getFileName('/path/to/no_extension')) // "no_extension"
1097
+ ```
1098
+
1099
+ ### `isFilePath`
1100
+
1101
+ > `function isFilePath(filePath: string): boolean`
1102
+
1103
+ 判断是否是有效的文件路径。有效文件路径指的是文件后缀匹配到 `FileSuffixEnum` 中的任何值。
1104
+
1105
+ #### 参数
1106
+
1107
+ - `filePath (string)`: 文件路径,例如 `/path/to/file.txt`。
1108
+
1109
+ #### 返回
1110
+
1111
+ `(boolean)`: 如果文件路径的后缀匹配到 `FileSuffixEnum` 中的任何值,则返回 `true`,否则返回 `false`。
1112
+
1113
+ #### 示例
1114
+
1115
+ ```typescript
1116
+ import { isFilePath } from '@minto-ai/tools'
1117
+
1118
+ console.log(isFilePath('/path/to/example/document.pdf')) // true
1119
+ console.log(isFilePath('/path/to/image.jpg')) // true
1120
+ console.log(isFilePath('/path/to/no_extension')) // false
1121
+ ```
1122
+
1123
+ ### `isImageFilePath`
1124
+
1125
+ > `function isImageFilePath(filePath: string): boolean`
1126
+
1127
+ 判断是否是有效的图片文件路径。有效图片文件路径指的是文件后缀匹配到 `ImageFileSuffixEnum` 中的任何值。
1128
+
1129
+ #### 参数
1130
+
1131
+ - `filePath (string)`: 文件路径,例如 `/path/to/image.jpg`。
1132
+
1133
+ #### 返回值
1134
+
1135
+ `(boolean)`: 如果文件路径的后缀匹配到 `ImageFileSuffixEnum` 中的任何值,则返回 `true`,否则返回 `false`。
1136
+
1137
+ #### 例子
1138
+
1139
+ ```typescript
1140
+ import { isImageFilePath } from '@minto-ai/tools'
1141
+
1142
+ console.log(isImageFilePath('/path/to/image.jpg')) // true
1143
+ console.log(isImageFilePath('/path/to/image.png')) // true
1144
+ console.log(isImageFilePath('/path/to/image.gif')) // false
1145
+ console.log(isImageFilePath('/path/to/image.bmp')) // false
1146
+ ```
1147
+
1148
+ ### `isVideoFilePath`
1149
+
1150
+ > `function isVideoFilePath(filePath: string): boolean`
1151
+
1152
+ 判断是否是有效的视频文件路径。有效视频文件路径指的是文件后缀匹配到 `VideoFileSuffixEnum` 中的任何值。
1153
+
1154
+ #### 参数
1155
+
1156
+ - `filePath (string)`: 文件路径,例如 `/path/to/video.mp4`。
1157
+
1158
+ #### 返回值
1159
+
1160
+ `(boolean)`: 如果文件路径的后缀匹配到 `VideoFileSuffixEnum` 中的任何值,则返回 `true`,否则返回 `false`。
1161
+
1162
+ #### 例子
1163
+
1164
+ ```typescript
1165
+ import { isVideoFilePath } from '@minto-ai/tools'
1166
+
1167
+ console.log(isVideoFilePath('/path/to/video.mp4')) // true
1168
+ console.log(isVideoFilePath('/path/to/video.avi')) // true
1169
+ ```
1170
+
1171
+ ### `isPptFilePath`
1172
+
1173
+ > `function isPptFilePath(filePath: string): boolean`
1174
+
1175
+ 判断是否是有效的文件路径。有效文件路径指的是文件后缀匹配到 `PptFileSuffixEnum` 中的任何值。
1176
+
1177
+ #### 参数
1178
+
1179
+ - `filePath (string)`: 文件路径,例如 `/path/to/presentation.ppt`。
1180
+
1181
+ #### 返回值
1182
+
1183
+ `(boolean)`: 如果文件路径的后缀匹配到 `PptFileSuffixEnum` 中的任何值,则返回 `true`,否则返回 `false`。
1184
+
1185
+ #### 例子
1186
+
1187
+ ```typescript
1188
+ import { isPptFilePath } from '@minto-ai/tools'
1189
+
1190
+ console.log(isPptFilePath('/path/to/presentation.ppt')) // true
1191
+ console.log(isPptFilePath('/path/to/presentation.pptx')) // true
1192
+ ```
1193
+
1194
+ ### `isDocumentFilePath`
1195
+
1196
+ > `function isDocumentFilePath(filePath: string): boolean`
1197
+
1198
+ 判断是否是有效的文件路径。有效文件路径指的是文件后缀匹配到 `DocumentFileSuffixEnum` 中的任何值。
1199
+
1200
+ #### 参数
1201
+
1202
+ - `filePath (string)`: 文件路径,例如 `/path/to/document.pdf`。
1203
+
1204
+ #### 返回值
1205
+
1206
+ `(boolean)`: 如果文件路径的后缀匹配到 `DocumentFileSuffixEnum` 中的任何值,则返回 `true`,否则返回 `false`。
1207
+
1208
+ #### 例子
1209
+
1210
+ ```typescript
1211
+ import { isDocumentFilePath } from '@minto-ai/tools'
1212
+
1213
+ console.log(isDocumentFilePath('/path/to/document.pdf')) // true
1214
+ console.log(isDocumentFilePath('/path/to/document.docx')) // true
1215
+ console.log(isDocumentFilePath('/path/to/document.doc')) // true
1216
+ ```
1217
+
1218
+ ### `isMusicFilePath`
1219
+
1220
+ > `function isMusicFilePath(filePath: string): boolean`
1221
+
1222
+ 判断是否是有效的音乐文件路径。匹配 `MusicFileSuffixEnum` 中的后缀。
1223
+
1224
+ #### 参数
1225
+
1226
+ - `filePath (string)`: 文件路径,例如 `/path/to/audio.mp3`。
1227
+
1228
+ #### 返回
1229
+
1230
+ `(boolean)`: 如果匹配音乐后缀返回 `true`,否则返回 `false`。
1231
+
1232
+ #### 例子
1233
+
1234
+ ```typescript
1235
+ import { isMusicFilePath } from '@minto-ai/tools'
1236
+
1237
+ isMusicFilePath('/path/to/audio.mp3') // true
1238
+ isMusicFilePath('/path/to/audio.wav') // true
1239
+ ```
1240
+
1241
+ ### `isCompressFilePath`
1242
+
1243
+ > `function isCompressFilePath(filePath: string): boolean`
1244
+
1245
+ 判断是否是有效的压缩文件路径。匹配 `CompressFileSuffixEnum` 中的后缀。
1246
+
1247
+ #### 参数
1248
+
1249
+ - `filePath (string)`: 文件路径,例如 `/path/to/archive.zip`。
1250
+
1251
+ #### 返回
1252
+
1253
+ `(boolean)`: 如果匹配压缩后缀返回 `true`,否则返回 `false`。
1254
+
1255
+ #### 例子
1256
+
1257
+ ```typescript
1258
+ import { isCompressFilePath } from '@minto-ai/tools'
1259
+
1260
+ isCompressFilePath('/path/to/archive.zip') // true
1261
+ isCompressFilePath('/path/to/archive.rar') // true
1262
+ ```
1263
+
1264
+ ### `getFileSuffixIcon`
1265
+
1266
+ > `function getFileSuffixIcon(fileSuffix: FileSuffix): string`
1267
+
1268
+ 根据文件后缀获取对应的图标。
1269
+
1270
+ #### 参数
1271
+
1272
+ - `fileSuffix (FileSuffix)`: 文件后缀,需为 `FileSuffixEnum` 中的有效值。
1273
+
1274
+ #### 返回
1275
+
1276
+ `(string)`: 对应的图标路径。
1277
+
1278
+ #### 示例
1279
+
1280
+ ```typescript
1281
+ import { FileSuffixEnum, getFileSuffixIcon } from '@minto-ai/tools'
1282
+
1283
+ console.log(getFileSuffixIcon(FileSuffixEnum.PDF)) // 返回 pdfIcon 的路径
1284
+ console.log(getFileSuffixIcon(FileSuffixEnum.JPG)) // 返回 imageIcon 的路径
1285
+ console.log(getFileSuffixIcon(FileSuffixEnum.TXT)) // 返回 txtIcon 的路径
1286
+ ```
1287
+
1288
+ ### `getFileIcon`
1289
+
1290
+ > `function getFileIcon(filePath: string): string`
1291
+
1292
+ 根据文件路径获取对应的图标
1293
+
1294
+ #### 参数
1295
+
1296
+ - `filePath`: 文件路径
1297
+
1298
+ #### 返回
1299
+
1300
+ - `string`: 图标路径
1301
+
1302
+ #### 示例
1303
+
1304
+ ```typescript
1305
+ import { getFileIcon } from '@minto-ai/tools'
1306
+
1307
+ console.log(getFileIcon('/path/to/file.pdf')) // 返回 pdfIcon 的路径
1308
+ console.log(getFileIcon('/path/to/file.txt')) // 返回 txtIcon 的路径
1309
+ ```
1310
+
1311
+ ### `getFilePathNotQuery`
1312
+
1313
+ > `function getFilePathNotQuery(filePath: string): string`
1314
+
1315
+ 获取不包含查询参数的纯文件路径。
1316
+
1317
+ #### 参数
1318
+
1319
+ - `filePath (string)`: 包含查询参数的文件路径。
1320
+
1321
+ #### 返回
1322
+
1323
+ `(string)`: 纯文件路径。
1324
+
1325
+ #### 例子
1326
+
1327
+ ```typescript
1328
+ import { getFilePathNotQuery } from '@minto-ai/tools'
1329
+
1330
+ getFilePathNotQuery('https://a.com/file.pdf?x=1') // 'https://a.com/file.pdf'
1331
+ ```
1332
+
1333
+ ### `getFileQuery`
1334
+
1335
+ > `function getFileQuery(filePath: string): Record<string, any>`
1336
+
1337
+ 从文件路径中解析查询参数并转换为对象。
1338
+
1339
+ #### 参数
1340
+
1341
+ - `filePath (string)`: 包含查询参数的文件路径。
1342
+
1343
+ #### 返回
1344
+
1345
+ `(Record<string, any>)`: 查询参数对象。
1346
+
1347
+ #### 例子
1348
+
1349
+ ```typescript
1350
+ import { getFileQuery } from '@minto-ai/tools'
1351
+
1352
+ getFileQuery('https://a.com/file.pdf?x=1&y=2') // { x: '1', y: '2' }
1353
+ ```
1354
+
1355
+ ### `updateFilePathQuery`
1356
+
1357
+ > `function updateFilePathQuery(filePath: string, objectQuery: Record<string, string>, isAppend?: boolean): string`
1358
+
1359
+ 更新文件路径中的查询参数;支持在原有参数上追加或覆盖。
1360
+
1361
+ #### 参数
1362
+
1363
+ - `filePath (string)`: 原始文件路径。
1364
+ - `objectQuery (Record<string, string>)`: 需写入的查询参数对象。
1365
+ - `[isAppend=true] (boolean)`: 是否在原有参数基础上追加,`false` 则覆盖。
1366
+
1367
+ #### 返回
1368
+
1369
+ `(string)`: 更新后的完整文件路径。
1370
+
1371
+ #### 例子
1372
+
1373
+ ```typescript
1374
+ import { updateFilePathQuery } from '@minto-ai/tools'
1375
+
1376
+ updateFilePathQuery('https://a.com/file.pdf?x=1', { y: '2' })
1377
+ // 'https://a.com/file.pdf?x=1&y=2'
1378
+
1379
+ updateFilePathQuery('https://a.com/file.pdf?x=1', { y: '2' }, false)
1380
+ // 'https://a.com/file.pdf?y=2'
1381
+ ```
1382
+
1383
+ ### `singleDownloadFile`
1384
+
1385
+ > `function singleDownloadFile(filePath: string, fileName?: string): Promise<void>`
1386
+
1387
+ 下载单个文件。该函数通过指定的文件路径下载文件,并允许自定义下载文件的名称。
1388
+
1389
+ #### 参数
1390
+
1391
+ - `fileUrl (string)`: 文件的完整路径,例如 `https://example.com/file.pdf`。
1392
+ - `fileName (string)`: 下载时保存的文件名(可选)。如果未提供,将自动从 `filePath` 提取文件名。
1393
+
1394
+ #### 返回
1395
+
1396
+ `(Promise<void>)`: 返回一个 Promise,在文件下载完成后。
1397
+
1398
+ #### 例子
1399
+
1400
+ ```typescript
1401
+ import { singleDownloadFile } from '@minto-ai/tools'
1402
+
1403
+ // 示例 1: 使用默认文件名下载
1404
+ singleDownloadFile('https://example.com/document.pdf')
1405
+
1406
+ // 示例 2: 自定义文件名下载
1407
+ singleDownloadFile('https://example.com/document.pdf', 'custom_name.pdf')
1408
+ ```
1409
+
1410
+ ### `batchDownloadFile`
1411
+
1412
+ > `function batchDownloadFile(fileList: Array<{filePath: string;fileName?: string;}>, zipName: string):Promise<void>`
1413
+
1414
+ 批量下载文件并打包为 ZIP。该函数接受一个文件列表,每个文件包含文件路径和可选的文件名,最终将所有文件打包为一个 ZIP 文件并触发下载。
1415
+
1416
+ #### 参数
1417
+
1418
+ - `fileList (Array<{filePath: string;fileName?: string;}>)`: 文件列表,每个文件包含文件路径和可选的文件名。如果未提供文件名,将自动从文件路径中提取。
1419
+ - `zipName (string)`: 打包后的 ZIP 文件名。如果文件名不以 `.zip` 结尾,将自动添加 `.zip` 后缀。
1420
+
1421
+ #### 返回
1422
+
1423
+ `(Promise<void>)`: 返回一个 Promise,在文件打包和下载完成后。
1424
+
1425
+ #### 例子
1426
+
1427
+ ```typescript
1428
+ import { batchDownloadFile } from '@minto-ai/tools'
1429
+
1430
+ const fileList = [
1431
+ { filePath: 'https://example.com/file1.pdf', fileName: 'document1.pdf' },
1432
+ { filePath: 'https://example.com/file2.jpg' },
1433
+ { filePath: 'https://example.com/file3.txt', fileName: 'notes.txt' }
1434
+ ]
1435
+
1436
+ batchDownloadFile(fileList, 'my-files.zip')
1437
+ ```
1438
+
1439
+ ## 校验
1440
+
1441
+ ### `isValidUrl`
1442
+
1443
+ > `function isValidUrl(url: string): boolean`
1444
+
1445
+ 校验是否为合法的 HTTP/HTTPS 网页地址,支持域名、IP、端口、路径、查询、哈希等组合。
1446
+
1447
+ #### 参数
1448
+
1449
+ - `url (string)`: 需要校验的地址字符串。
1450
+
1451
+ #### 返回
1452
+
1453
+ `(boolean)`: 合法返回 `true`,否则返回 `false`。
1454
+
1455
+ #### 例子
1456
+
1457
+ ```typescript
1458
+ import { isValidUrl } from '@minto-ai/tools'
1459
+
1460
+ isValidUrl('https://example.com?a=1#hash') // true
1461
+ isValidUrl('ftp://example.com') // false
1462
+ ```
1463
+
1464
+ ## 主题
1465
+
1466
+ ### `useTheme`
1467
+
1468
+ > `function useTheme(): { theme: Ref<Theme>; isDark: ComputedRef<boolean>; isLight: ComputedRef<boolean>; setTheme: (theme?: Theme) => void; getTheme: () => Theme; setLight: () => void; setDark: () => void; toggleTheme: () => void }`
1469
+
1470
+ Vue 主题切换 Hook,支持亮色/暗黑模式切换与状态读取,并自动将主题类名应用到 `html` 根元素。
1471
+
1472
+ #### 返回
1473
+
1474
+ `({ theme, isDark, isLight, setTheme, getTheme, setLight, setDark, toggleTheme })`: 主题状态与操作方法。
1475
+
1476
+ #### 例子
1477
+
1478
+ ```typescript
1479
+ import { useTheme } from '@minto-ai/tools'
1480
+
1481
+ const { theme, isDark, setDark, toggleTheme } = useTheme()
1482
+
1483
+ setDark()
1484
+ toggleTheme()
1485
+ ```
1486
+
1487
+ ## 国际化
1488
+
1489
+ ### `locale`
1490
+
1491
+ > `function locale(locale: 'zh-cn' | 'zh-tw' | 'en' | 'ms' | 'ar' | 'th' | 'vi'): void`
1492
+
1493
+ 设置当前语言,内置中文简体/繁体、英文、马来语、阿拉伯语、泰语、越南语,并自动在包初始化时注册多语言文案。
1494
+
1495
+ #### 参数
1496
+
1497
+ - `locale`: 语言代码。
1498
+
1499
+ #### 例子
1500
+
1501
+ ```typescript
1502
+ import tools from '@minto-ai/tools'
1503
+
1504
+ tools.locale('zh-cn')
1505
+ ```
1506
+
1507
+ ### `getLocale`
1508
+
1509
+ > `function getLocale(): 'zh-cn' | 'zh-tw' | 'en' | 'ms' | 'ar' | 'th' | 'vi'`
1510
+
1511
+ 获取当前语言代码。
1512
+
1513
+ #### 例子
1514
+
1515
+ ```typescript
1516
+ import tools from '@minto-ai/tools'
1517
+
1518
+ tools.getLocale() // 'zh-cn'
1519
+ ```
1520
+
1521
+ ## webSocket
1522
+
1523
+ ### `isWebSocketSupported`
1524
+
1525
+ > `function isWebSocketSupported(): boolean`
1526
+
1527
+ 检查当前浏览器是否支持 WebSocket。
1528
+
1529
+ #### 返回
1530
+
1531
+ `(boolean)`: 如果浏览器支持 WebSocket,则返回 `true`;否则返回 `false`。
1532
+
1533
+ #### 例子
1534
+
1535
+ ```typescript
1536
+ import { isWebSocketSupported } from '@minto-ai/tools'
1537
+
1538
+ if (isWebSocketSupported()) {
1539
+ console.log('WebSocket is supported!')
1540
+ }
1541
+ else {
1542
+ console.log('WebSocket is not supported!')
1543
+ }
1544
+ ```
1545
+
1546
+ ### `createWebSocket`
1547
+
1548
+ > `function createWebSocket(url: string): WebSocket`
1549
+
1550
+ 创建一个新的 WebSocket 实例。此函数接受一个 WebSocket 服务器的 URL 并创建一个新的 WebSocket 实例。
1551
+
1552
+ #### 参数
1553
+
1554
+ - `url (string)`: WebSocket 服务器的 URL。
1555
+
1556
+ #### 返回
1557
+
1558
+ `(WebSocket)`: 返回一个新的 WebSocket 实例。
1559
+
1560
+ #### 例子
1561
+
1562
+ ```typescript
1563
+ import { createWebSocket } from '@minto-ai/tools'
1564
+
1565
+ const socket = createWebSocket('ws://example.com')
1566
+ // 使用 socket 进行通信...
1567
+ ```
1568
+
1569
+ ## worker
1570
+
1571
+ ### `createWorker`
1572
+
1573
+ > `function createWorker(fun: (...args: any) => any): Worker`
1574
+
1575
+ 创建并返回一个新的 Worker 实例,`fun`指的的任意函数,也就是要在 Worker 中执行的 JavaScript 函数。
1576
+
1577
+ #### 参数
1578
+
1579
+ - `fun (Function)`: 要在 Worker 中执行的 JavaScript 函数。
1580
+
1581
+ #### 返回
1582
+
1583
+ `(Worker)`: 返回新创建的 Worker 实例。
1584
+
1585
+ #### 例子
1586
+
1587
+ ```typescript
1588
+ import { createWorker } from '@minto-ai/tools'
1589
+
1590
+ const myWorker = createWorker(() => {
1591
+ console.log('Hello from the Web Worker!')
1592
+ })
1593
+ ```
1594
+
1595
+ ### `closeWorker`
1596
+
1597
+ > `function closeWorker(worker: Worker | undefined | null): void`
1598
+
1599
+ 关闭并终止传入 Web Worker 实例的执行,通常配合`createWorker`一起使用
1600
+
1601
+ #### 参数
1602
+
1603
+ - `worker (Worker | undefined | null)`: 要关闭的 Worker 实例。
1604
+
1605
+ #### 例子
1606
+
1607
+ ```typescript
1608
+ import { closeWorker, createWorker } from '@minto-ai/tools'
1609
+
1610
+ const myWorker = createWorker(() => {
1611
+ console.log('Hello from the Web Worker!')
1612
+ })
1613
+ // 当不再需要 Worker 时
1614
+ closeWorker(myWorker)
1615
+ ```