minutool 1.0.19 → 1.0.20

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 请求
@@ -150,9 +150,8 @@ export declare const blobToDataUri: (blob: Blob) => Promise<string>;
150
150
  /**
151
151
  * 块元素
152
152
  * 用大写定义,方便直接匹配 node.tagName
153
- * @type {string[]}
154
153
  */
155
- export declare const BLOCK_TAGS: string[];
154
+ 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
155
 
157
156
  /**
158
157
  * 构建 HTML Input:hidden 标签
@@ -360,8 +359,8 @@ export declare const disabled: (el: HTMLElement | string, disabledClass?: string
360
359
 
361
360
  /**
362
361
  * 发布事件,event 可以是字符串或 Symbol,detail 是事件详情
363
- * @param {string | symbol} event 事件名称
364
- * @param {any} detail 事件详情
362
+ * @param event 事件名称
363
+ * @param detail 事件详情
365
364
  */
366
365
  declare const dispatchEvent_2: (event: string | symbol, detail?: any) => void;
367
366
  export { dispatchEvent_2 as dispatchEvent }
@@ -423,6 +422,16 @@ export declare const escapeAttr: (s: string, preserveCR?: string) => string;
423
422
  */
424
423
  export declare const escapeHtml: (str: string, tabSize?: number, allowLineBreaker?: boolean) => string;
425
424
 
425
+ /**
426
+ * 事件委托,parent 是父元素,selector 是子元素选择器,event 是事件名称,handler 是事件处理函数
427
+ * @param parent 父元素
428
+ * @param selector 子元素选择器
429
+ * @param event 事件名称
430
+ * @param handler 事件处理函数, 参数为事件对象和匹配的子元素 (event, target)
431
+ * @return 返回一个取消事件委托的函数
432
+ */
433
+ export declare const eventDelegate: (parent: HTMLElement, selector: string, event: string, handler: (event: Event, target: HTMLElement) => void) => () => void;
434
+
426
435
  /**
427
436
  * 退出全屏模式
428
437
  * @returns {Promise<void>} 返回 Promise,退出全屏操作完成后 resolve
@@ -492,6 +501,17 @@ export declare const floatVal: (str: any, defaultVal?: number) => number;
492
501
  */
493
502
  export declare const formatDate: (format: string, date?: Date | number | string | null) => string;
494
503
 
504
+ /**
505
+ * 格式化文件大小
506
+ * @param bytes 文件大小(字节数)
507
+ * @param toUnit 目标单位(B/KB/MB/GB/TB/PB)
508
+ * @param decimalPlaces 小数位数,默认为 2
509
+ * @return 转换后的数值
510
+ * @example
511
+ * formatSize(1024) // 1
512
+ */
513
+ export declare const formatSize: (bytes: number, toUnit?: string, decimalPlaces?: number) => number;
514
+
495
515
  /**
496
516
  * 获取元素的位置(相对于视口)
497
517
  * @param {HTMLElement} el - DOM 元素
@@ -525,10 +545,10 @@ export declare const getDomDimension: (dom: HTMLElement) => {
525
545
 
526
546
  /**
527
547
  * 发送 GET 请求并获取 JSON 响应
528
- * @param {string} url - 请求 URL
529
- * @param {any} [data=null] - 请求数据
530
- * @param {RequestOption} [option={}] - 请求选项
531
- * @returns {Promise<any>} 返回解析后的 JSON 数据
548
+ * @param url - 请求 URL
549
+ * @param data - 请求数据
550
+ * @param option - 请求选项
551
+ * @returns Promise<any> 返回解析后的 JSON 数据
532
552
  * @example
533
553
  * getJson('/api/users').then(data => console.log(data))
534
554
  */
@@ -612,12 +632,17 @@ export declare const highlightText: (text: string, kw: string, replaceTpl?: stri
612
632
 
613
633
  /**
614
634
  * 将 HTML 转换为纯文本(移除所有标签和样式)
615
- * @param {string} html - HTML 字符串
616
- * @returns {string} 返回纯文本
635
+ * @param html - HTML 字符串
636
+ * @param option - 转换选项
637
+ * @returns 返回纯文本
617
638
  * @example
618
639
  * html2Text('<p>Hello <b>World</b></p>') // 'Hello World'
619
640
  */
620
- export declare const html2Text: (html: string) => string;
641
+ export declare const html2Text: (html: string, option?: {
642
+ keepLineBreak?: boolean;
643
+ imgReplacer?: string | ((src: string, alt: string) => string);
644
+ videoReplacer?: string | ((src: string, alt: string) => string);
645
+ }) => string;
621
646
 
622
647
  /**
623
648
  * 通过 Image 元素获取 Base64 数据
@@ -674,7 +699,7 @@ export declare const isFirefox: () => boolean;
674
699
  /**
675
700
  * 检测元素是否可聚焦
676
701
  * @param {HTMLElement} el - DOM 元素
677
- * @returns {boolean} 如果元素可聚焦返回 true,否则返回 false
702
+ * @returns 如果元素可聚焦返回 true,否则返回 false
678
703
  * @example
679
704
  * isFocusable(inputElement) // true
680
705
  */
@@ -1305,11 +1330,17 @@ export declare const objToQuery: (data: Record<string, any>) => string;
1305
1330
 
1306
1331
  /**
1307
1332
  * 取消订阅事件,event 可以是字符串或 Symbol,handler 是事件处理函数
1308
- * @param {string | symbol} event 事件名称
1309
- * @param {Function} handler 事件处理函数
1333
+ * @param event 事件名称
1334
+ * @param handler 事件处理函数
1310
1335
  */
1311
1336
  export declare const offEvent: (event: string | symbol, handler: EventListenerOrEventListenerObject) => void;
1312
1337
 
1338
+ /**
1339
+ * 文档就绪事件,handler 是文档就绪时的回调函数
1340
+ * @param handler 文档就绪时的回调函数
1341
+ */
1342
+ export declare const onDocReady: (handler: () => void) => void;
1343
+
1313
1344
  /**
1314
1345
  * 监听节点树变更
1315
1346
  * @param {HTMLElement} dom - 要监听的 DOM 节点
@@ -1339,9 +1370,9 @@ export declare const ONE_YEAR366: number;
1339
1370
 
1340
1371
  /**
1341
1372
  * 订阅事件,event 可以是字符串或 Symbol,handler 是事件处理函数
1342
- * @param {string | symbol} event 事件名称
1343
- * @param {Function} handler 事件处理函数
1344
- * @return {Function} 返回一个取消订阅的函数
1373
+ * @param event 事件名称
1374
+ * @param handler 事件处理函数
1375
+ * @return 返回一个取消订阅的函数
1345
1376
  */
1346
1377
  export declare const onEvent: (event: string | symbol, handler: EventListenerOrEventListenerObject) => () => void;
1347
1378
 
@@ -1352,7 +1383,6 @@ export declare const onHover: (el: HTMLElement | string, onHoverIn: () => void,
1352
1383
  /**
1353
1384
  * 非自关闭标签
1354
1385
  * https://www.w3schools.com/html/html_blocks.asp
1355
- * @type {*[]}
1356
1386
  */
1357
1387
  export declare const PAIR_TAGS: string[];
1358
1388
 
@@ -1366,11 +1396,11 @@ export declare const parseUnit: (value: string | number, defaultUnit?: string) =
1366
1396
 
1367
1397
  /**
1368
1398
  * 上传文件
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 数据
1399
+ * @param url - 请求 URL
1400
+ * @param fileMap - 文件对象映射
1401
+ * @param data - 额外的表单数据
1402
+ * @param option - 请求选项
1403
+ * @returns Promise<any> 返回解析后的 JSON 数据
1374
1404
  * @example
1375
1405
  * postFiles('/api/upload', {avatar: fileObject})
1376
1406
  */
@@ -1378,10 +1408,10 @@ export declare const postFiles: (url: string, fileMap: Record<string, File>, dat
1378
1408
 
1379
1409
  /**
1380
1410
  * 发送 POST 请求并获取 JSON 响应
1381
- * @param {string} url - 请求 URL
1382
- * @param {any} [data=null] - 请求数据
1383
- * @param {RequestOption} [option={}] - 请求选项
1384
- * @returns {Promise<any>} 返回解析后的 JSON 数据
1411
+ * @param url - 请求 URL
1412
+ * @param data - 请求数据
1413
+ * @param option - 请求选项
1414
+ * @returns Promise<any> 返回解析后的 JSON 数据
1385
1415
  * @example
1386
1416
  * postJson('/api/users', {name: 'John'}).then(data => console.log(data))
1387
1417
  */
@@ -1506,7 +1536,6 @@ export declare const regQuote: (str: string) => string;
1506
1536
 
1507
1537
  /**
1508
1538
  * 非内容可清理标签
1509
- * @type {string[]}
1510
1539
  */
1511
1540
  export declare const REMOVABLE_TAGS: string[];
1512
1541
 
@@ -1528,8 +1557,11 @@ export declare const remove: (dom: HTMLElement | string) => Node | null;
1528
1557
  * @example
1529
1558
  * request('/api/data', null, {method: 'GET'})
1530
1559
  */
1531
- export declare const request: (url: string, data: (BodyInit | null) | undefined, option: RequestOption) => AbortablePromise<Response>;
1560
+ export declare const request: (url: string, data: (BodyInit | null) | undefined, requestOption: RequestOption) => AbortablePromise<Response>;
1532
1561
 
1562
+ /**
1563
+ * 扩展 RequestInit,添加 timeout 和其他自定义属性,timeout 用于设置请求超时时间,其他自定义属性可以直接添加到 headers 中
1564
+ */
1533
1565
  declare interface RequestOption extends RequestInit {
1534
1566
  timeout?: number;
1535
1567
  [key: string]: any;
@@ -1556,7 +1588,6 @@ export declare const SCREEN_DPI: number;
1556
1588
 
1557
1589
  /**
1558
1590
  * 自关闭标签
1559
- * @type {string[]}
1560
1591
  */
1561
1592
  export declare const SELF_CLOSING_TAGS: string[];
1562
1593
 
@@ -1737,6 +1768,13 @@ export declare const toggleDisabled: (el: HTMLElement | string, disabledClass?:
1737
1768
  */
1738
1769
  export declare const toggleFullScreen: (element: any) => Promise<unknown>;
1739
1770
 
1771
+ /**
1772
+ * 触发HTML节点事件
1773
+ * @param node HTML节点
1774
+ * @param event 事件名称
1775
+ */
1776
+ export declare const triggerDomEvent: (node: HTMLElement, event: string) => void;
1777
+
1740
1778
  /**
1741
1779
  * 去除字符串首尾指定字符或空白
1742
1780
  * @param {string} str - 源字符串