minutool 1.0.18 → 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 +128 -55
- package/dist/minutool.js +986 -886
- package/dist/minutool.js.map +1 -1
- package/dist/minutool.umd.cjs +5 -5
- package/dist/minutool.umd.cjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 拓展原生 Fetch API 实现可中止的请求,支持超时设置
|
|
3
|
-
* @param
|
|
4
|
-
* @param
|
|
5
|
-
* @param
|
|
6
|
-
* @returns
|
|
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('
|
|
9
|
+
* req.abort('canceled'); // 中止请求
|
|
10
10
|
*/
|
|
11
|
-
export declare const abortableFetch: (url: string, fetchOption?:
|
|
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:
|
|
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
|
|
364
|
-
* @param
|
|
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
|
|
529
|
-
* @param
|
|
530
|
-
* @param
|
|
531
|
-
* @returns
|
|
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
|
|
616
|
-
* @
|
|
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
|
|
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
|
|
702
|
+
* @returns 如果元素可聚焦返回 true,否则返回 false
|
|
678
703
|
* @example
|
|
679
704
|
* isFocusable(inputElement) // true
|
|
680
705
|
*/
|
|
@@ -721,6 +746,11 @@ export declare const isLandscape: () => boolean;
|
|
|
721
746
|
* 判断一个值是否为数字
|
|
722
747
|
* @param val - 要判断的值
|
|
723
748
|
* @returns 如果是数字返回 true,否则返回 false
|
|
749
|
+
* @example
|
|
750
|
+
* isNumberic(123) // true
|
|
751
|
+
* isNumberic('123.45') // true
|
|
752
|
+
* isNumberic('-123') // true
|
|
753
|
+
* isNumberic('210mm') // false
|
|
724
754
|
*/
|
|
725
755
|
export declare const isNumberic: (val: any) => boolean;
|
|
726
756
|
|
|
@@ -1220,6 +1250,24 @@ export declare const mutationEffective: (dom: HTMLElement, option: MutationObser
|
|
|
1220
1250
|
*/
|
|
1221
1251
|
export declare const nodeIndex: (node: HTMLElement) => number;
|
|
1222
1252
|
|
|
1253
|
+
/**
|
|
1254
|
+
* 获取对象的键值对数组,代替 Object.entries,如果key是数字,则转换为数字类型
|
|
1255
|
+
* @param obj - 要获取键值对的对象
|
|
1256
|
+
* @returns 键值对数组
|
|
1257
|
+
* @example
|
|
1258
|
+
* objectEntries({ a: 1, b: 2 }) // [['a', 1], ['b', 2]]
|
|
1259
|
+
*/
|
|
1260
|
+
export declare const objectEntries: <T extends Record<string | number | symbol, any>>(obj: T) => [keyof T, T[keyof T]][];
|
|
1261
|
+
|
|
1262
|
+
/**
|
|
1263
|
+
* 从键值对数组创建对象,代替 Object.fromEntries,如果key是数字字符串,则转换为数字类型
|
|
1264
|
+
* @param entries - 键值对数组
|
|
1265
|
+
* @returns 创建的对象
|
|
1266
|
+
* @example
|
|
1267
|
+
* objectFromEntries([['a', 1], ['b', 2]]) // { a: 1, b: 2 }
|
|
1268
|
+
*/
|
|
1269
|
+
export declare const objectFromEntries: <T extends Record<string | number | symbol, any>>(entries: Iterable<readonly [string | number, T[keyof T]]>) => T;
|
|
1270
|
+
|
|
1223
1271
|
/**
|
|
1224
1272
|
* 获取对象指定路径的值
|
|
1225
1273
|
* @param obj - 对象
|
|
@@ -1233,22 +1281,15 @@ export declare const nodeIndex: (node: HTMLElement) => number;
|
|
|
1233
1281
|
export declare function objectGet<T = any>(obj: any, path: string, defaultValue?: T): T;
|
|
1234
1282
|
|
|
1235
1283
|
/**
|
|
1236
|
-
*
|
|
1237
|
-
* @param
|
|
1238
|
-
* @param {
|
|
1239
|
-
* @returns
|
|
1284
|
+
* 根据映射关系替换对象的键
|
|
1285
|
+
* @param obj - 要替换键的对象
|
|
1286
|
+
* @param mapping - 键映射关系,例如 { oldKey: 'newKey' }
|
|
1287
|
+
* @returns 替换键后的新对象
|
|
1240
1288
|
* @example
|
|
1241
|
-
*
|
|
1289
|
+
* objectKeyReplace({ a: 1, b: 2 }, { a: 'x' }) // { x: 1, b: 2 }
|
|
1242
1290
|
*/
|
|
1243
1291
|
export declare const objectKeyReplace: (obj: Record<string, any>, mapping: Record<string, string>) => Record<string, any>;
|
|
1244
1292
|
|
|
1245
|
-
/**
|
|
1246
|
-
* 交换对象中的键值对
|
|
1247
|
-
* @param {Object} obj
|
|
1248
|
-
* @returns
|
|
1249
|
-
*/
|
|
1250
|
-
export declare const objectKeyValSwap: (obj: object) => any;
|
|
1251
|
-
|
|
1252
1293
|
/**
|
|
1253
1294
|
* 合并对象
|
|
1254
1295
|
* @param target - 目标对象
|
|
@@ -1269,6 +1310,15 @@ export declare function objectMerge<T extends object>(target: T, ...sources: Par
|
|
|
1269
1310
|
*/
|
|
1270
1311
|
export declare function objectSet(obj: any, path: string, value: any): void;
|
|
1271
1312
|
|
|
1313
|
+
/**
|
|
1314
|
+
* 交换对象中的键值对
|
|
1315
|
+
* @param obj - 要交换键值对的对象
|
|
1316
|
+
* @returns 交换后的对象
|
|
1317
|
+
* @example
|
|
1318
|
+
* objectSwitchKV({a: 1, b: 2}) // {1: 'a', 2: 'b'}
|
|
1319
|
+
*/
|
|
1320
|
+
export declare const objectSwitchKV: (obj: object) => any;
|
|
1321
|
+
|
|
1272
1322
|
/**
|
|
1273
1323
|
* 将对象转换为 URL 查询字符串
|
|
1274
1324
|
* @param {Record<string, any>} data - 数据对象
|
|
@@ -1280,11 +1330,17 @@ export declare const objToQuery: (data: Record<string, any>) => string;
|
|
|
1280
1330
|
|
|
1281
1331
|
/**
|
|
1282
1332
|
* 取消订阅事件,event 可以是字符串或 Symbol,handler 是事件处理函数
|
|
1283
|
-
* @param
|
|
1284
|
-
* @param
|
|
1333
|
+
* @param event 事件名称
|
|
1334
|
+
* @param handler 事件处理函数
|
|
1285
1335
|
*/
|
|
1286
1336
|
export declare const offEvent: (event: string | symbol, handler: EventListenerOrEventListenerObject) => void;
|
|
1287
1337
|
|
|
1338
|
+
/**
|
|
1339
|
+
* 文档就绪事件,handler 是文档就绪时的回调函数
|
|
1340
|
+
* @param handler 文档就绪时的回调函数
|
|
1341
|
+
*/
|
|
1342
|
+
export declare const onDocReady: (handler: () => void) => void;
|
|
1343
|
+
|
|
1288
1344
|
/**
|
|
1289
1345
|
* 监听节点树变更
|
|
1290
1346
|
* @param {HTMLElement} dom - 要监听的 DOM 节点
|
|
@@ -1314,9 +1370,9 @@ export declare const ONE_YEAR366: number;
|
|
|
1314
1370
|
|
|
1315
1371
|
/**
|
|
1316
1372
|
* 订阅事件,event 可以是字符串或 Symbol,handler 是事件处理函数
|
|
1317
|
-
* @param
|
|
1318
|
-
* @param
|
|
1319
|
-
* @return
|
|
1373
|
+
* @param event 事件名称
|
|
1374
|
+
* @param handler 事件处理函数
|
|
1375
|
+
* @return 返回一个取消订阅的函数
|
|
1320
1376
|
*/
|
|
1321
1377
|
export declare const onEvent: (event: string | symbol, handler: EventListenerOrEventListenerObject) => () => void;
|
|
1322
1378
|
|
|
@@ -1327,7 +1383,6 @@ export declare const onHover: (el: HTMLElement | string, onHoverIn: () => void,
|
|
|
1327
1383
|
/**
|
|
1328
1384
|
* 非自关闭标签
|
|
1329
1385
|
* https://www.w3schools.com/html/html_blocks.asp
|
|
1330
|
-
* @type {*[]}
|
|
1331
1386
|
*/
|
|
1332
1387
|
export declare const PAIR_TAGS: string[];
|
|
1333
1388
|
|
|
@@ -1341,11 +1396,11 @@ export declare const parseUnit: (value: string | number, defaultUnit?: string) =
|
|
|
1341
1396
|
|
|
1342
1397
|
/**
|
|
1343
1398
|
* 上传文件
|
|
1344
|
-
* @param
|
|
1345
|
-
* @param
|
|
1346
|
-
* @param
|
|
1347
|
-
* @param
|
|
1348
|
-
* @returns
|
|
1399
|
+
* @param url - 请求 URL
|
|
1400
|
+
* @param fileMap - 文件对象映射
|
|
1401
|
+
* @param data - 额外的表单数据
|
|
1402
|
+
* @param option - 请求选项
|
|
1403
|
+
* @returns Promise<any> 返回解析后的 JSON 数据
|
|
1349
1404
|
* @example
|
|
1350
1405
|
* postFiles('/api/upload', {avatar: fileObject})
|
|
1351
1406
|
*/
|
|
@@ -1353,10 +1408,10 @@ export declare const postFiles: (url: string, fileMap: Record<string, File>, dat
|
|
|
1353
1408
|
|
|
1354
1409
|
/**
|
|
1355
1410
|
* 发送 POST 请求并获取 JSON 响应
|
|
1356
|
-
* @param
|
|
1357
|
-
* @param
|
|
1358
|
-
* @param
|
|
1359
|
-
* @returns
|
|
1411
|
+
* @param url - 请求 URL
|
|
1412
|
+
* @param data - 请求数据
|
|
1413
|
+
* @param option - 请求选项
|
|
1414
|
+
* @returns Promise<any> 返回解析后的 JSON 数据
|
|
1360
1415
|
* @example
|
|
1361
1416
|
* postJson('/api/users', {name: 'John'}).then(data => console.log(data))
|
|
1362
1417
|
*/
|
|
@@ -1481,7 +1536,6 @@ export declare const regQuote: (str: string) => string;
|
|
|
1481
1536
|
|
|
1482
1537
|
/**
|
|
1483
1538
|
* 非内容可清理标签
|
|
1484
|
-
* @type {string[]}
|
|
1485
1539
|
*/
|
|
1486
1540
|
export declare const REMOVABLE_TAGS: string[];
|
|
1487
1541
|
|
|
@@ -1503,8 +1557,11 @@ export declare const remove: (dom: HTMLElement | string) => Node | null;
|
|
|
1503
1557
|
* @example
|
|
1504
1558
|
* request('/api/data', null, {method: 'GET'})
|
|
1505
1559
|
*/
|
|
1506
|
-
export declare const request: (url: string, data: (BodyInit | null) | undefined,
|
|
1560
|
+
export declare const request: (url: string, data: (BodyInit | null) | undefined, requestOption: RequestOption) => AbortablePromise<Response>;
|
|
1507
1561
|
|
|
1562
|
+
/**
|
|
1563
|
+
* 扩展 RequestInit,添加 timeout 和其他自定义属性,timeout 用于设置请求超时时间,其他自定义属性可以直接添加到 headers 中
|
|
1564
|
+
*/
|
|
1508
1565
|
declare interface RequestOption extends RequestInit {
|
|
1509
1566
|
timeout?: number;
|
|
1510
1567
|
[key: string]: any;
|
|
@@ -1531,7 +1588,6 @@ export declare const SCREEN_DPI: number;
|
|
|
1531
1588
|
|
|
1532
1589
|
/**
|
|
1533
1590
|
* 自关闭标签
|
|
1534
|
-
* @type {string[]}
|
|
1535
1591
|
*/
|
|
1536
1592
|
export declare const SELF_CLOSING_TAGS: string[];
|
|
1537
1593
|
|
|
@@ -1625,14 +1681,14 @@ export declare const stripSlashes: (str: string) => string;
|
|
|
1625
1681
|
export declare const strToPascalCase: (str: string, capitalize_first?: boolean) => string;
|
|
1626
1682
|
|
|
1627
1683
|
/**
|
|
1628
|
-
*
|
|
1629
|
-
*
|
|
1630
|
-
* @
|
|
1631
|
-
* @returns {DOMRect
|
|
1632
|
-
* @returns {DOMRect.
|
|
1633
|
-
* @
|
|
1684
|
+
* 获取 SVG 的尺寸(像素单位)
|
|
1685
|
+
* 优先级:width/height 属性 > viewBox > getBoundingClientRect
|
|
1686
|
+
* @param {SVGSVGElement} svg - SVG 元素
|
|
1687
|
+
* @returns {DOMRect} 返回包含像素单位的尺寸对象
|
|
1688
|
+
* @returns {number} DOMRect.width - 宽度(像素)
|
|
1689
|
+
* @returns {number} DOMRect.height - 高度(像素)
|
|
1634
1690
|
*/
|
|
1635
|
-
export declare const
|
|
1691
|
+
export declare const svgGetDimension: (svg: SVGSVGElement) => [number, number];
|
|
1636
1692
|
|
|
1637
1693
|
/**
|
|
1638
1694
|
* svg 对象转换为图片对象
|
|
@@ -1712,6 +1768,13 @@ export declare const toggleDisabled: (el: HTMLElement | string, disabledClass?:
|
|
|
1712
1768
|
*/
|
|
1713
1769
|
export declare const toggleFullScreen: (element: any) => Promise<unknown>;
|
|
1714
1770
|
|
|
1771
|
+
/**
|
|
1772
|
+
* 触发HTML节点事件
|
|
1773
|
+
* @param node HTML节点
|
|
1774
|
+
* @param event 事件名称
|
|
1775
|
+
*/
|
|
1776
|
+
export declare const triggerDomEvent: (node: HTMLElement, event: string) => void;
|
|
1777
|
+
|
|
1715
1778
|
/**
|
|
1716
1779
|
* 去除字符串首尾指定字符或空白
|
|
1717
1780
|
* @param {string} str - 源字符串
|
|
@@ -1749,6 +1812,16 @@ export declare function truncate(str: string, length: number, suffix?: string):
|
|
|
1749
1812
|
*/
|
|
1750
1813
|
export declare const unescapeHtml: (html: string) => string;
|
|
1751
1814
|
|
|
1815
|
+
/**
|
|
1816
|
+
* 单位转换工具,支持px、mm、cm、in等常见单位之间的转换
|
|
1817
|
+
* @returns {Number} 转换后的数值(以目标单位为准)
|
|
1818
|
+
*/
|
|
1819
|
+
export declare const unitConvert: (value: string | number, toUnit: string, extOption?: {
|
|
1820
|
+
dpi?: number;
|
|
1821
|
+
fontSize?: number | null;
|
|
1822
|
+
lineHeight?: number | null;
|
|
1823
|
+
}) => number;
|
|
1824
|
+
|
|
1752
1825
|
/**
|
|
1753
1826
|
* URL 转 Base64 Data URL 数据缓存
|
|
1754
1827
|
* @param {string} url - 文件 URL
|