minutool 1.0.19 → 1.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  /**
2
2
  * 拓展原生 Fetch API 实现可中止的请求,支持超时设置
3
- * @param {string} url - 请求 URL
4
- * @param {RequestInit} [fetchOption={}] - fetch请求选项,包括 method、headers、body 等
5
- * @param {number} [timeout=0] - 超时时间,单位毫秒,0 表示不设置超时
6
- * @returns {AbortablePromise<any>} 返回可中止的 Promise 对象
3
+ * @param url - 请求 URL
4
+ * @param fetchOption - fetch请求选项,包括 method、headers、body 等
5
+ * @param timeout - 超时时间,单位毫秒,0 表示不设置超时
6
+ * @returns AbortablePromise<any> 返回可中止的 Promise 对象
7
7
  * @example
8
8
  * const req = abortableFetch('/api/data');
9
- * req.abort('cancled'); // 中止请求
9
+ * req.abort('canceled'); // 中止请求
10
10
  */
11
- export declare const abortableFetch: (url: string, fetchOption?: RequestInit, timeout?: number) => AbortablePromise<any>;
11
+ export declare const abortableFetch: (url: string, fetchOption?: RequestOption) => AbortablePromise<any>;
12
12
 
13
13
  /**
14
14
  * 可中止的 Fetch 请求
@@ -129,6 +129,54 @@ export declare const base64UrlSafeEncode: (text: string) => string;
129
129
  */
130
130
  export declare const between: (val: number, min: number, max: number, includeEqual?: boolean) => boolean;
131
131
 
132
+ /**
133
+ * 绑定点击事件,并返回一个解绑函数
134
+ * @param element - 要绑定事件的元素
135
+ * @param callback - 事件回调函数
136
+ * @returns 解绑函数
137
+ */
138
+ export declare const bindClick: (element: HTMLElement | string, callback: (event: Event) => void) => () => void;
139
+
140
+ /**
141
+ * 绑定DOM事件,并返回一个解绑函数
142
+ * @param element - 要绑定事件的元素
143
+ * @param eventName - 事件名称
144
+ * @param callback - 事件回调函数
145
+ * @returns 解绑函数
146
+ */
147
+ export declare const bindDomEvent: (element: HTMLElement | string, eventName: string, callback: (event: Event) => void) => () => void;
148
+
149
+ /**
150
+ * 绑定双击事件,并返回一个解绑函数
151
+ * @param element - 要绑定事件的元素
152
+ * @param callback - 事件回调函数
153
+ * @returns 解绑函数
154
+ */
155
+ export declare const bindDoubleClick: (element: HTMLElement | string, callback: (event: Event) => void) => () => void;
156
+
157
+ /**
158
+ * 绑定键盘按键按下事件,并返回一个解绑函数
159
+ * @param element - 要绑定事件的元素
160
+ * @param callback - 事件回调函数
161
+ * @returns 解绑函数
162
+ */
163
+ export declare const bindKeyDown: (element: HTMLElement | string, callback: (event: Event) => void) => () => void;
164
+
165
+ /**
166
+ * 绑定键盘按键抬起事件,并返回一个解绑函数
167
+ * @param element - 要绑定事件的元素
168
+ * @param callback - 事件回调函数
169
+ * @returns 解绑函数
170
+ */
171
+ export declare const bindKeyUp: (element: HTMLElement | string, callback: (event: Event) => void) => () => void;
172
+
173
+ /**
174
+ * 绑定对象移动事件
175
+ * @param element - 要移动的元素
176
+ * @param handle - 拖动句柄,默认为元素本身
177
+ */
178
+ export declare function bindNodeMove(element: HTMLElement | string, handle?: HTMLElement | string | null): () => void;
179
+
132
180
  /**
133
181
  * 转换 Blob 数据到 Base64 Data URL
134
182
  * @param {Blob} blob - Blob 对象
@@ -150,9 +198,8 @@ export declare const blobToDataUri: (blob: Blob) => Promise<string>;
150
198
  /**
151
199
  * 块元素
152
200
  * 用大写定义,方便直接匹配 node.tagName
153
- * @type {string[]}
154
201
  */
155
- export declare const BLOCK_TAGS: string[];
202
+ export declare const BLOCK_TAGS: readonly ["ADDRESS", "ARTICLE", "ASIDE", "BLOCKQUOTE", "CANVAS", "DD", "DIV", "DL", "DT", "FIELDSET", "FIGCAPTION", "FIGURE", "FOOTER", "FORM", "H1", "H2", "H3", "H4", "H5", "H6", "HEADER", "HR", "LI", "MAIN", "NAV", "NOSCRIPT", "OL", "P", "PRE", "SECTION", "TABLE", "TFOOT", "UL", "VIDEO"];
156
203
 
157
204
  /**
158
205
  * 构建 HTML Input:hidden 标签
@@ -360,8 +407,8 @@ export declare const disabled: (el: HTMLElement | string, disabledClass?: string
360
407
 
361
408
  /**
362
409
  * 发布事件,event 可以是字符串或 Symbol,detail 是事件详情
363
- * @param {string | symbol} event 事件名称
364
- * @param {any} detail 事件详情
410
+ * @param event 事件名称
411
+ * @param detail 事件详情
365
412
  */
366
413
  declare const dispatchEvent_2: (event: string | symbol, detail?: any) => void;
367
414
  export { dispatchEvent_2 as dispatchEvent }
@@ -423,6 +470,16 @@ export declare const escapeAttr: (s: string, preserveCR?: string) => string;
423
470
  */
424
471
  export declare const escapeHtml: (str: string, tabSize?: number, allowLineBreaker?: boolean) => string;
425
472
 
473
+ /**
474
+ * 事件委托,parent 是父元素,selector 是子元素选择器,event 是事件名称,handler 是事件处理函数
475
+ * @param parent 父元素
476
+ * @param selector 子元素选择器
477
+ * @param event 事件名称
478
+ * @param handler 事件处理函数, 参数为事件对象和匹配的子元素 (event, target)
479
+ * @return 返回一个取消事件委托的函数
480
+ */
481
+ export declare const eventDelegate: (parent: HTMLElement | string, selector: string, event: string, handler: (event: Event, target: HTMLElement) => void) => () => void;
482
+
426
483
  /**
427
484
  * 退出全屏模式
428
485
  * @returns {Promise<void>} 返回 Promise,退出全屏操作完成后 resolve
@@ -492,6 +549,17 @@ export declare const floatVal: (str: any, defaultVal?: number) => number;
492
549
  */
493
550
  export declare const formatDate: (format: string, date?: Date | number | string | null) => string;
494
551
 
552
+ /**
553
+ * 格式化文件大小
554
+ * @param bytes 文件大小(字节数)
555
+ * @param toUnit 目标单位(B/KB/MB/GB/TB/PB)
556
+ * @param decimalPlaces 小数位数,默认为 2
557
+ * @return 转换后的数值
558
+ * @example
559
+ * formatSize(1024) // 1
560
+ */
561
+ export declare const formatSize: (bytes: number, toUnit?: string, decimalPlaces?: number) => number;
562
+
495
563
  /**
496
564
  * 获取元素的位置(相对于视口)
497
565
  * @param {HTMLElement} el - DOM 元素
@@ -525,10 +593,10 @@ export declare const getDomDimension: (dom: HTMLElement) => {
525
593
 
526
594
  /**
527
595
  * 发送 GET 请求并获取 JSON 响应
528
- * @param {string} url - 请求 URL
529
- * @param {any} [data=null] - 请求数据
530
- * @param {RequestOption} [option={}] - 请求选项
531
- * @returns {Promise<any>} 返回解析后的 JSON 数据
596
+ * @param url - 请求 URL
597
+ * @param data - 请求数据
598
+ * @param option - 请求选项
599
+ * @returns Promise<any> 返回解析后的 JSON 数据
532
600
  * @example
533
601
  * getJson('/api/users').then(data => console.log(data))
534
602
  */
@@ -612,12 +680,17 @@ export declare const highlightText: (text: string, kw: string, replaceTpl?: stri
612
680
 
613
681
  /**
614
682
  * 将 HTML 转换为纯文本(移除所有标签和样式)
615
- * @param {string} html - HTML 字符串
616
- * @returns {string} 返回纯文本
683
+ * @param html - HTML 字符串
684
+ * @param option - 转换选项
685
+ * @returns 返回纯文本
617
686
  * @example
618
687
  * html2Text('<p>Hello <b>World</b></p>') // 'Hello World'
619
688
  */
620
- export declare const html2Text: (html: string) => string;
689
+ export declare const html2Text: (html: string, option?: {
690
+ keepLineBreak?: boolean;
691
+ imgReplacer?: string | ((src: string, alt: string) => string);
692
+ videoReplacer?: string | ((src: string, alt: string) => string);
693
+ }) => string;
621
694
 
622
695
  /**
623
696
  * 通过 Image 元素获取 Base64 数据
@@ -674,7 +747,7 @@ export declare const isFirefox: () => boolean;
674
747
  /**
675
748
  * 检测元素是否可聚焦
676
749
  * @param {HTMLElement} el - DOM 元素
677
- * @returns {boolean} 如果元素可聚焦返回 true,否则返回 false
750
+ * @returns 如果元素可聚焦返回 true,否则返回 false
678
751
  * @example
679
752
  * isFocusable(inputElement) // true
680
753
  */
@@ -821,8 +894,8 @@ export declare const loadScript: (src: string, forceReload?: boolean) => Promise
821
894
 
822
895
  /**
823
896
  * 绑定元素,禁止交互
824
- * @param {Node} el
825
- * @param {Function} payload 处理函数,参数为 reset
897
+ * @param el - DOM 元素或选择器
898
+ * @param payload - 处理函数,参数为 reset
826
899
  */
827
900
  export declare const lockElementInteraction: (el: HTMLElement | string, payload: (reset: () => void) => void) => void;
828
901
 
@@ -1305,11 +1378,17 @@ export declare const objToQuery: (data: Record<string, any>) => string;
1305
1378
 
1306
1379
  /**
1307
1380
  * 取消订阅事件,event 可以是字符串或 Symbol,handler 是事件处理函数
1308
- * @param {string | symbol} event 事件名称
1309
- * @param {Function} handler 事件处理函数
1381
+ * @param event 事件名称
1382
+ * @param handler 事件处理函数
1310
1383
  */
1311
1384
  export declare const offEvent: (event: string | symbol, handler: EventListenerOrEventListenerObject) => void;
1312
1385
 
1386
+ /**
1387
+ * 文档就绪事件,handler 是文档就绪时的回调函数
1388
+ * @param handler 文档就绪时的回调函数
1389
+ */
1390
+ export declare const onDocReady: (handler: () => void) => void;
1391
+
1313
1392
  /**
1314
1393
  * 监听节点树变更
1315
1394
  * @param {HTMLElement} dom - 要监听的 DOM 节点
@@ -1339,20 +1418,26 @@ export declare const ONE_YEAR366: number;
1339
1418
 
1340
1419
  /**
1341
1420
  * 订阅事件,event 可以是字符串或 Symbol,handler 是事件处理函数
1342
- * @param {string | symbol} event 事件名称
1343
- * @param {Function} handler 事件处理函数
1344
- * @return {Function} 返回一个取消订阅的函数
1421
+ * @param event 事件名称
1422
+ * @param handler 事件处理函数
1423
+ * @return 返回一个取消订阅的函数
1345
1424
  */
1346
1425
  export declare const onEvent: (event: string | symbol, handler: EventListenerOrEventListenerObject) => () => void;
1347
1426
 
1348
1427
  export declare const onEvents: (events: (string | symbol)[], handler: EventListenerOrEventListenerObject) => () => void;
1349
1428
 
1350
- export declare const onHover: (el: HTMLElement | string, onHoverIn: () => void, onHoverOut: () => void) => void;
1429
+ /**
1430
+ * 绑定元素 hover 事件,返回解绑函数
1431
+ * @param el - DOM 元素或选择器
1432
+ * @param onHoverIn - 鼠标进入回调函数
1433
+ * @param onHoverOut - 鼠标离开回调函数
1434
+ * @returns 返回解绑函数
1435
+ */
1436
+ export declare const onHover: (el: HTMLElement | string, onHoverIn: () => void, onHoverOut: () => void) => () => void;
1351
1437
 
1352
1438
  /**
1353
1439
  * 非自关闭标签
1354
1440
  * https://www.w3schools.com/html/html_blocks.asp
1355
- * @type {*[]}
1356
1441
  */
1357
1442
  export declare const PAIR_TAGS: string[];
1358
1443
 
@@ -1366,11 +1451,11 @@ export declare const parseUnit: (value: string | number, defaultUnit?: string) =
1366
1451
 
1367
1452
  /**
1368
1453
  * 上传文件
1369
- * @param {string} url - 请求 URL
1370
- * @param {Record<string, File>} fileMap - 文件对象映射
1371
- * @param {any} [data=null] - 额外的表单数据
1372
- * @param {RequestOption} [option={}] - 请求选项
1373
- * @returns {Promise<any>} 返回解析后的 JSON 数据
1454
+ * @param url - 请求 URL
1455
+ * @param fileMap - 文件对象映射
1456
+ * @param data - 额外的表单数据
1457
+ * @param option - 请求选项
1458
+ * @returns Promise<any> 返回解析后的 JSON 数据
1374
1459
  * @example
1375
1460
  * postFiles('/api/upload', {avatar: fileObject})
1376
1461
  */
@@ -1378,10 +1463,10 @@ export declare const postFiles: (url: string, fileMap: Record<string, File>, dat
1378
1463
 
1379
1464
  /**
1380
1465
  * 发送 POST 请求并获取 JSON 响应
1381
- * @param {string} url - 请求 URL
1382
- * @param {any} [data=null] - 请求数据
1383
- * @param {RequestOption} [option={}] - 请求选项
1384
- * @returns {Promise<any>} 返回解析后的 JSON 数据
1466
+ * @param url - 请求 URL
1467
+ * @param data - 请求数据
1468
+ * @param option - 请求选项
1469
+ * @returns Promise<any> 返回解析后的 JSON 数据
1385
1470
  * @example
1386
1471
  * postJson('/api/users', {name: 'John'}).then(data => console.log(data))
1387
1472
  */
@@ -1506,7 +1591,6 @@ export declare const regQuote: (str: string) => string;
1506
1591
 
1507
1592
  /**
1508
1593
  * 非内容可清理标签
1509
- * @type {string[]}
1510
1594
  */
1511
1595
  export declare const REMOVABLE_TAGS: string[];
1512
1596
 
@@ -1528,8 +1612,11 @@ export declare const remove: (dom: HTMLElement | string) => Node | null;
1528
1612
  * @example
1529
1613
  * request('/api/data', null, {method: 'GET'})
1530
1614
  */
1531
- export declare const request: (url: string, data: (BodyInit | null) | undefined, option: RequestOption) => AbortablePromise<Response>;
1615
+ export declare const request: (url: string, data: (BodyInit | null) | undefined, requestOption: RequestOption) => AbortablePromise<Response>;
1532
1616
 
1617
+ /**
1618
+ * 扩展 RequestInit,添加 timeout 和其他自定义属性,timeout 用于设置请求超时时间,其他自定义属性可以直接添加到 headers 中
1619
+ */
1533
1620
  declare interface RequestOption extends RequestInit {
1534
1621
  timeout?: number;
1535
1622
  [key: string]: any;
@@ -1556,7 +1643,6 @@ export declare const SCREEN_DPI: number;
1556
1643
 
1557
1644
  /**
1558
1645
  * 自关闭标签
1559
- * @type {string[]}
1560
1646
  */
1561
1647
  export declare const SELF_CLOSING_TAGS: string[];
1562
1648
 
@@ -1737,6 +1823,13 @@ export declare const toggleDisabled: (el: HTMLElement | string, disabledClass?:
1737
1823
  */
1738
1824
  export declare const toggleFullScreen: (element: any) => Promise<unknown>;
1739
1825
 
1826
+ /**
1827
+ * 触发HTML节点事件
1828
+ * @param node HTML节点
1829
+ * @param event 事件名称
1830
+ */
1831
+ export declare const triggerDomEvent: (node: HTMLElement, event: string) => void;
1832
+
1740
1833
  /**
1741
1834
  * 去除字符串首尾指定字符或空白
1742
1835
  * @param {string} str - 源字符串