minutool 1.0.15 → 1.0.17

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
@@ -174,6 +174,16 @@ export declare const buildHtmlHidden: (maps: Record<string, any>) => string;
174
174
  */
175
175
  export declare const buildStyleVars: (vars: Record<string, number | string | undefined>) => Record<string, string>;
176
176
 
177
+ /**
178
+ * 计算剩余时间,单位为毫秒
179
+ * @param progress 当前进度
180
+ * @param total 总进度
181
+ * @param startTime 开始时间(毫秒时间戳)
182
+ * @returns 剩余时间,单位毫秒
183
+ * @description 根据当前进度、总进度和已用时间估算剩余时间。公式为:剩余时间 = (已用时间 / 当前进度) * (总进度 - 当前进度)。如果当前进度为0,则返回Infinity。
184
+ */
185
+ export declare const calcRemainingMSecs: (progress: number, total: number, startTime: number) => number;
186
+
177
187
  /**
178
188
  * 驼峰命名转换
179
189
  * @param str - 输入字符串
@@ -203,13 +213,12 @@ export declare function capitalize(str: string): string;
203
213
  */
204
214
  export declare const cleanNull: (obj: any, recursive?: boolean) => any;
205
215
 
216
+ export declare const COMMON_DPI = 96;
217
+
206
218
  export declare const convertMinutesToTimezoneOffsetStr: (offsetMinutes: number) => string;
207
219
 
208
220
  /**
209
221
  * 倒计时函数(该方法采用 setTimeout 方式,不够精准)
210
- * @param {number} timeout - 倒计时总秒数
211
- * @param {Function} [tickFunc] - 每秒回调函数,接收剩余秒数作为参数
212
- * @param {Function} [onFinish] - 倒计时结束回调函数
213
222
  * @returns {void}
214
223
  * @example
215
224
  * countDown(10, (sec) => console.log(sec), () => console.log('done'))
@@ -324,7 +333,7 @@ export declare const detectedPrecision: (...numbers: number[]) => number;
324
333
  * @example
325
334
  * detectLanguage(['en', 'zh-CN', 'zh']) // '根据浏览器语言返回匹配的语言'
326
335
  */
327
- export declare const detectLanguage: (supportedLngs: string[]) => any;
336
+ export declare const detectLanguage: (supportedLngs: string[]) => string;
328
337
 
329
338
  /**
330
339
  * 计算数字位数
@@ -443,23 +452,17 @@ export declare const fileToBase64DataUri: (file: File | Blob | string) => Promis
443
452
 
444
453
  /**
445
454
  * 通过选择器查找子节点(强制添加 :scope 来约束必须是子节点)
446
- * @param {string|HTMLElement|HTMLElement[]|NodeList|HTMLCollection} selector - 选择器或 DOM 元素
447
- * @param {Document|HTMLElement} [parent=document] - 父节点,默认为 document
448
- * @returns {HTMLElement[]} 返回查找到的所有元素数组
449
455
  * @example
450
456
  * findAll('.item', container)
451
457
  */
452
- export declare const findAll: (selector: string | HTMLElement | HTMLElement[] | NodeList | HTMLCollection, parent?: Document | HTMLElement) => HTMLElement[];
458
+ export declare const findAll: (selector: string | Element | Element[], parent?: Document | DocumentFragment | Element) => Element[];
453
459
 
454
460
  /**
455
461
  * 通过选择器查找单个节点
456
- * @param {string|HTMLElement} selector - 选择器,如果是 HTMLElement,则直接返回
457
- * @param {Document|HTMLElement} [parent=document] - 父节点,默认为 document
458
- * @returns {HTMLElement} 返回查找到的元素
459
462
  * @example
460
463
  * findOne('.item')
461
464
  */
462
- export declare const findOne: (selector: string | HTMLElement, parent?: Document | HTMLElement) => HTMLElement;
465
+ export declare const findOne: (selector: string | Element, parent?: Document | DocumentFragment | Element) => Element | null;
463
466
 
464
467
  /**
465
468
  * 修复基本 URL(将相对路径转换为绝对 URL)
@@ -473,10 +476,11 @@ export declare const fixBaseUrl: (url: string, baseUrl: string) => string;
473
476
 
474
477
  /**
475
478
  * 将字符串转换为浮点数
476
- * @param {string} str - 要转换的字符串
479
+ * @param {*} str - 要转换的字符串
477
480
  * @param {number} [defaultVal=0] - 转换失败时的默认值
481
+ * @returns {number} 返回转换后的浮点数,如果转换失败则返回默认值
478
482
  */
479
- export declare const floatVal: (str: string, defaultVal?: number) => number;
483
+ export declare const floatVal: (str: any, defaultVal?: number) => number;
480
484
 
481
485
  /**
482
486
  * 格式化日期(以 PHP 方式格式化)
@@ -587,6 +591,7 @@ export declare const guid: (prefix?: string) => string;
587
591
 
588
592
  /**
589
593
  * 隐藏节点(通过设置 display:none 方式)
594
+ * 注:只有 HTMLElement 才有 style 属性,其他类型的 Node 没有,因此需要断言为 HTMLElement
590
595
  * @param {HTMLElement|string} dom - DOM 元素或选择器
591
596
  * @returns {void}
592
597
  * @example
@@ -710,6 +715,15 @@ export declare const isJSON: (json: string) => boolean;
710
715
  */
711
716
  export declare const isJson: (json: string) => boolean;
712
717
 
718
+ export declare const isLandscape: () => boolean;
719
+
720
+ /**
721
+ * 判断一个值是否为数字
722
+ * @param val - 要判断的值
723
+ * @returns 如果是数字返回 true,否则返回 false
724
+ */
725
+ export declare const isNumberic: (val: any) => boolean;
726
+
713
727
  /**
714
728
  * 检测目标是否为 Object(非数组的对象)
715
729
  * @param {any} item - 要检测的对象
@@ -720,6 +734,8 @@ export declare const isJson: (json: string) => boolean;
720
734
  */
721
735
  export declare const isObject: (item: any) => boolean;
722
736
 
737
+ export declare const isPortrait: () => boolean;
738
+
723
739
  /**
724
740
  * 检测对象是否为 Promise 对象
725
741
  * @param {any} obj - 要检测的对象
@@ -1178,8 +1194,6 @@ export declare const MONTH_NOW: number;
1178
1194
 
1179
1195
  /**
1180
1196
  * 毫秒转换为“时分秒前”格式
1181
- * @param {number} ms - 毫秒数
1182
- * @returns {string} 返回格式化后的字符串
1183
1197
  * @example
1184
1198
  * msToHMS(3661000) // '1小时0分钟1秒前'
1185
1199
  */
@@ -1226,7 +1240,14 @@ export declare function objectGet<T = any>(obj: any, path: string, defaultValue?
1226
1240
  * @example
1227
1241
  * objectKeyMapping({a: 1, b: 2}, {a: 'x'}) // {x: 1, b: 2}
1228
1242
  */
1229
- export declare const objectKeyMapping: (obj: Record<string, any>, mapping: Record<string, string>) => Record<string, any>;
1243
+ export declare const objectKeyReplace: (obj: Record<string, any>, mapping: Record<string, string>) => Record<string, any>;
1244
+
1245
+ /**
1246
+ * 交换对象中的键值对
1247
+ * @param {Object} obj
1248
+ * @returns
1249
+ */
1250
+ export declare const objectKeyValSwap: (obj: object) => any;
1230
1251
 
1231
1252
  /**
1232
1253
  * 合并对象
@@ -1295,8 +1316,11 @@ export declare const ONE_YEAR366: number;
1295
1316
  * 订阅事件,event 可以是字符串或 Symbol,handler 是事件处理函数
1296
1317
  * @param {string | symbol} event 事件名称
1297
1318
  * @param {Function} handler 事件处理函数
1319
+ * @return {Function} 返回一个取消订阅的函数
1298
1320
  */
1299
- export declare const onEvent: (event: string | symbol, handler: EventListenerOrEventListenerObject) => void;
1321
+ export declare const onEvent: (event: string | symbol, handler: EventListenerOrEventListenerObject) => () => void;
1322
+
1323
+ export declare const onEvents: (events: (string | symbol)[], handler: EventListenerOrEventListenerObject) => () => void;
1300
1324
 
1301
1325
  export declare const onHover: (el: HTMLElement | string, onHoverIn: () => void, onHoverOut: () => void) => void;
1302
1326
 
@@ -1307,6 +1331,14 @@ export declare const onHover: (el: HTMLElement | string, onHoverIn: () => void,
1307
1331
  */
1308
1332
  export declare const PAIR_TAGS: string[];
1309
1333
 
1334
+ /**
1335
+ * 解析带单位的值,返回数值和单位
1336
+ */
1337
+ export declare const parseUnit: (value: string | number, defaultUnit?: string) => {
1338
+ val: number;
1339
+ unit: string;
1340
+ } | null;
1341
+
1310
1342
  /**
1311
1343
  * 上传文件
1312
1344
  * @param {string} url - 请求 URL
@@ -1495,6 +1527,8 @@ export declare const round: (num: number, precision?: number) => number;
1495
1527
  */
1496
1528
  export declare const sanitizeFileName: (name: string) => string;
1497
1529
 
1530
+ export declare const SCREEN_DPI: number;
1531
+
1498
1532
  /**
1499
1533
  * 自关闭标签
1500
1534
  * @type {string[]}
@@ -1517,6 +1551,7 @@ export declare const setCookie: (name: string, value: string, days: number, path
1517
1551
 
1518
1552
  /**
1519
1553
  * 显示节点(通过设置 display 为空方式)
1554
+ * 注:只有 HTMLElement 才有 style 属性,其他类型的 Node 没有,因此需要断言为 HTMLElement
1520
1555
  * @param {HTMLElement|string} dom - DOM 元素或选择器
1521
1556
  * @returns {void}
1522
1557
  * @example
@@ -1526,13 +1561,13 @@ export declare const show: (dom: HTMLElement | string) => void;
1526
1561
 
1527
1562
  /**
1528
1563
  * 通过图片 URL 获取 Base64(网络请求模式)
1529
- * @param {string} src - 图片 URL
1564
+ * @param {string} url - 图片 URL
1530
1565
  * @param {boolean} [cache=false] - 是否缓存结果
1531
1566
  * @returns {Promise<unknown>} 返回 Base64 Data URL 的 Promise
1532
1567
  * @example
1533
1568
  * srcToBase64('https://example.com/image.png').then(base64 => console.log(base64))
1534
1569
  */
1535
- export declare const srcToBase64: (src: string, cache?: boolean) => Promise<unknown>;
1570
+ export declare const srcToBase64: (url: string, cache?: boolean) => Promise<unknown>;
1536
1571
 
1537
1572
  export declare const STAND_DPI = 96;
1538
1573
 
@@ -1589,6 +1624,26 @@ export declare const stripSlashes: (str: string) => string;
1589
1624
  */
1590
1625
  export declare const strToPascalCase: (str: string, capitalize_first?: boolean) => string;
1591
1626
 
1627
+ /**
1628
+ * svg 对象转换为图片对象
1629
+ * @returns
1630
+ */
1631
+ export declare const svgToImg: (svg: SVGSVGElement) => Promise<HTMLImageElement>;
1632
+
1633
+ /**
1634
+ * svg 对象转换为图片数据
1635
+ * @param {SVGSVGElement} svg
1636
+ * @param {String} format
1637
+ * @param {number} quality
1638
+ * @returns
1639
+ */
1640
+ export declare const svgToImgData: (svg: SVGSVGElement, format?: string | null, quality?: number | null) => Promise<string>;
1641
+
1642
+ /**
1643
+ * svg 对象转换为图片数据
1644
+ */
1645
+ export declare const svgToSrc: (svg: SVGSVGElement) => string;
1646
+
1592
1647
  /**
1593
1648
  * 将纯文本转换为 HTML(转义特殊字符并处理换行)
1594
1649
  * @param {string} text - 纯文本字符串
@@ -1621,6 +1676,15 @@ export declare const throttle: (fn: Function, intervalMiSec: number) => Function
1621
1676
  */
1622
1677
  export declare const throttleEffect: (fn: Function, intervalMiSec: number) => Function;
1623
1678
 
1679
+ /**
1680
+ * 将时间戳转换为本地时间的日期时间字符串(YYYY-MM-DDTHH:mm),
1681
+ * 一般用于填充到 input[type="datetime-local"] 的 value 属性中
1682
+ * @param timestamp 时间戳,可以是数字、字符串或 Date 对象
1683
+ * @param includeSec 是否包含秒,默认为 false,如果为 true 则返回的字符串格式为 YYYY-MM-DDTHH:mm:ss
1684
+ * @returns 本地时间的日期时间字符串,格式为 YYYY-MM-DDTHH:mm:ss(如果 includeSec 参数为 true 则包含秒)
1685
+ */
1686
+ export declare const timestampToDateTimeLocal: (timestamp: number | string | Date, includeSec?: boolean) => string;
1687
+
1624
1688
  /**
1625
1689
  * 禁用启用元素切换
1626
1690
  * @param {String|Node} el