@whitesev/utils 2.11.8 → 2.11.10

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.
@@ -371,7 +371,7 @@ declare class Utils {
371
371
  x: number;
372
372
  y: number;
373
373
  }>>, deviation?: number): {
374
- /** 处理了偏移量后的z-index值 */
374
+ /** 处理了偏移量和阈值比较后的z-index值 */
375
375
  zIndex: number;
376
376
  /** 原始z-index值 */
377
377
  originZIndex: number;
@@ -393,7 +393,7 @@ declare class Utils {
393
393
  * Utils.getMaxZIndexNodeInfoFromPoint(20);
394
394
  */
395
395
  getMaxZIndexNodeInfoFromPoint(deviation: IFunction<number>): {
396
- /** 处理了偏移量后的z-index值 */
396
+ /** 处理了偏移量和阈值比较后的z-index值 */
397
397
  zIndex: number;
398
398
  /** 原始z-index值 */
399
399
  originZIndex: number;
@@ -1496,8 +1496,22 @@ declare class Utils {
1496
1496
  * 深度获取对象的某个属性
1497
1497
  * @param target 待获取的对象
1498
1498
  * @param handler 获取属性的回调
1499
+ * @example
1500
+ * Utils.queryProperty(window,(target)=>{
1501
+ * if(target.xxx){
1502
+ * return {
1503
+ * isFind: true,
1504
+ * data: target.xxx,
1505
+ * }
1506
+ * }else{
1507
+ * return {
1508
+ * isFind: false,
1509
+ * data: target.aabbcc,
1510
+ * }
1511
+ * }
1512
+ * })
1499
1513
  */
1500
- queryProperty<T = any>(target: any, handler: (
1514
+ queryProperty<T = any, R = any>(target: any, handler: (
1501
1515
  /**
1502
1516
  * 该值为返回的data值
1503
1517
  */
@@ -1510,26 +1524,37 @@ declare class Utils {
1510
1524
  isFind: boolean;
1511
1525
  /**
1512
1526
  * 对象/目标值
1527
+ *
1528
+ * 若`isFind`为true,那该值为返回的结果
1529
+ *
1530
+ * 若`isFind`为false,那该值为下次迭代传入的`target`
1513
1531
  */
1514
- data: any;
1515
- }): any;
1532
+ data: T | R | null | void;
1533
+ }): R | void | null;
1516
1534
  /**
1517
1535
  * 异步-深度获取对象属性
1518
1536
  * @param target 待获取的对象
1519
1537
  * @param handler 获取属性的回调
1538
+ * @example
1539
+ * Utils.asyncQueryProperty(window, async (target)=>{
1540
+ * if(target.xxx){
1541
+ * return {
1542
+ * isFind: true,
1543
+ * data: target.xxx,
1544
+ * }
1545
+ * }else{
1546
+ * return {
1547
+ * isFind: false,
1548
+ * data: target.aabbcc,
1549
+ * }
1550
+ * }
1551
+ * })
1520
1552
  */
1521
- asyncQueryProperty<T = any>(target: any, handler: (target: T) => {
1522
- /**
1523
- * 是否是需要的属性
1524
- * + true 将目标值赋值给data
1525
- * + false 不是需要的,data为下一个处理的对象
1526
- */
1527
- isFind: boolean;
1528
- /**
1529
- * 对象/目标值
1530
- */
1531
- data: any;
1532
- } | Promise<{
1553
+ asyncQueryProperty<T = any, R = any>(target: any, handler: (
1554
+ /**
1555
+ * 该值为返回的data值
1556
+ */
1557
+ target: T) => IPromise<{
1533
1558
  /**
1534
1559
  * 是否是需要的属性
1535
1560
  * + true 将目标值赋值给data
@@ -1538,13 +1563,18 @@ declare class Utils {
1538
1563
  isFind: boolean;
1539
1564
  /**
1540
1565
  * 对象/目标值
1566
+ *
1567
+ * 若`isFind`为true,那该值为返回的结果
1568
+ *
1569
+ * 若`isFind`为false,那该值为下次迭代传入的`target`
1541
1570
  */
1542
- data: any;
1543
- }>): Promise<Awaited<T>>;
1571
+ data: T | R | null | void;
1572
+ }>): Promise<R | void | null>;
1544
1573
  /**
1545
1574
  * 创建一个新的Utils实例
1546
1575
  * @param option
1547
- * @returns
1576
+ * @example
1577
+ * Utils.createUtils();
1548
1578
  */
1549
1579
  createUtils(option?: WindowApiOption): Utils;
1550
1580
  /**
@@ -1572,7 +1602,9 @@ declare class Utils {
1572
1602
  /**
1573
1603
  * 覆盖对象中的函数this指向
1574
1604
  * @param target 需要覆盖的对象
1575
- * @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
1605
+ * @param objectThis 覆盖的this指向,如果为传入,则默认为对象本身
1606
+ * @example
1607
+ * Utils.coverObjectFunctionThis({})
1576
1608
  */
1577
1609
  coverObjectFunctionThis: (target: any, objectThis?: any) => void;
1578
1610
  /**
@@ -1623,7 +1655,7 @@ declare class Utils {
1623
1655
  /**
1624
1656
  * 自动使用 Worker 执行 setTimeout
1625
1657
  * @param callback 回调函数
1626
- * @param [timeout=0] 延迟时间,默认为0
1658
+ * @param timeout 延迟时间,默认为0
1627
1659
  */
1628
1660
  workerSetTimeout(callback: (...args: any[]) => any, timeout?: number): number;
1629
1661
  /**
@@ -1683,6 +1715,10 @@ declare class Utils {
1683
1715
  /**
1684
1716
  * 判断页面中是否存在`worker-src`的CSP规则
1685
1717
  * @param timeout 超时时间,默认为`1500ms`
1718
+ * @example
1719
+ * Utils.hasWorkerCSP().then((hasCSP) => {
1720
+ * console.log(hasCSP);
1721
+ * })
1686
1722
  */
1687
1723
  hasWorkerCSP(timeout?: number): Promise<boolean>;
1688
1724
  /**
@@ -1691,12 +1727,18 @@ declare class Utils {
1691
1727
  * @param positionY 坐标y信息
1692
1728
  * @param otherPositionX 坐标x信息
1693
1729
  * @param otherPositionY 坐标y信息
1730
+ * @example
1731
+ * Utils.calcPositionDistance(1, 2, 3, 4);
1732
+ * > 2.8284271247461903
1694
1733
  */
1695
1734
  calcPositionDistance(positionX: number | string, positionY: number | string, otherPositionX: number | string, otherPositionY: number | string): number;
1696
1735
  /**
1697
1736
  * 计算两个坐标的直线距离
1698
1737
  * @param position 坐标信息
1699
1738
  * @param otherPosition 坐标信息
1739
+ * @example
1740
+ * Utils.calcPositionDistance({x: 1, y: 2}, {x: 3, y: 4});
1741
+ * > 2.8284271247461903
1700
1742
  */
1701
1743
  calcPositionDistance(position: {
1702
1744
  x: number | string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@whitesev/utils",
4
- "version": "2.11.8",
4
+ "version": "2.11.10",
5
5
  "description": "一个常用的工具库",
6
6
  "keywords": [
7
7
  "ScriptCat",
package/src/Utils.ts CHANGED
@@ -906,7 +906,7 @@ class Utils {
906
906
  $el?: IFunction<IArray<HTMLElement> | IArray<{ x: number; y: number }>>,
907
907
  deviation?: number
908
908
  ): {
909
- /** 处理了偏移量后的z-index值 */
909
+ /** 处理了偏移量和阈值比较后的z-index值 */
910
910
  zIndex: number;
911
911
  /** 原始z-index值 */
912
912
  originZIndex: number;
@@ -928,7 +928,7 @@ class Utils {
928
928
  * Utils.getMaxZIndexNodeInfoFromPoint(20);
929
929
  */
930
930
  getMaxZIndexNodeInfoFromPoint(deviation: IFunction<number>): {
931
- /** 处理了偏移量后的z-index值 */
931
+ /** 处理了偏移量和阈值比较后的z-index值 */
932
932
  zIndex: number;
933
933
  /** 原始z-index值 */
934
934
  originZIndex: number;
@@ -945,7 +945,7 @@ class Utils {
945
945
  $el?: IFunction<IArray<HTMLElement> | number | IArray<{ x: number; y: number }>>,
946
946
  deviation?: number
947
947
  ): {
948
- /** 处理了偏移量后的z-index值 */
948
+ /** 处理了偏移量和阈值比较后的z-index值 */
949
949
  zIndex: number;
950
950
  /** 原始z-index值 */
951
951
  originZIndex: number;
@@ -968,6 +968,10 @@ class Utils {
968
968
  if (typeof deviation !== "number" || Number.isNaN(deviation)) {
969
969
  deviation = 10;
970
970
  }
971
+ // 最大值 2147483647
972
+ // const maxZIndex = Math.pow(2, 31) - 1;
973
+ // 比较值 2000000000
974
+ const maxZIndexCompare = 2 * Math.pow(10, 9);
971
975
  /** 坐标偏移 */
972
976
  const positionDistance = 10;
973
977
  const defaultCalcPostion: {
@@ -1062,9 +1066,14 @@ class Utils {
1062
1066
  left: maxRect.left,
1063
1067
  };
1064
1068
  }
1069
+ let calcZIndex = zIndex + deviation;
1070
+ if (calcZIndex >= maxZIndexCompare) {
1071
+ // 不要超过最大值
1072
+ calcZIndex = maxZIndexCompare;
1073
+ }
1065
1074
  return {
1066
1075
  /** 计算偏移量后的z-index值 */
1067
- zIndex: zIndex + deviation,
1076
+ zIndex: calcZIndex,
1068
1077
  /** 获取到的最大的z-index值 */
1069
1078
  originZIndex: zIndex,
1070
1079
  /** 拥有最大z-index的元素 */
@@ -3816,8 +3825,22 @@ class Utils {
3816
3825
  * 深度获取对象的某个属性
3817
3826
  * @param target 待获取的对象
3818
3827
  * @param handler 获取属性的回调
3828
+ * @example
3829
+ * Utils.queryProperty(window,(target)=>{
3830
+ * if(target.xxx){
3831
+ * return {
3832
+ * isFind: true,
3833
+ * data: target.xxx,
3834
+ * }
3835
+ * }else{
3836
+ * return {
3837
+ * isFind: false,
3838
+ * data: target.aabbcc,
3839
+ * }
3840
+ * }
3841
+ * })
3819
3842
  */
3820
- queryProperty<T = any>(
3843
+ queryProperty<T = any, R = any>(
3821
3844
  target: any,
3822
3845
  handler: (
3823
3846
  /**
@@ -3833,16 +3856,20 @@ class Utils {
3833
3856
  isFind: boolean;
3834
3857
  /**
3835
3858
  * 对象/目标值
3859
+ *
3860
+ * 若`isFind`为true,那该值为返回的结果
3861
+ *
3862
+ * 若`isFind`为false,那该值为下次迭代传入的`target`
3836
3863
  */
3837
- data: any;
3864
+ data: T | R | null | void;
3838
3865
  }
3839
- ): any {
3866
+ ): R | void | null {
3840
3867
  if (target == null) {
3841
3868
  return;
3842
3869
  }
3843
3870
  const handleResult = handler(target);
3844
3871
  if (handleResult && typeof handleResult.isFind === "boolean" && handleResult.isFind) {
3845
- return handleResult.data;
3872
+ return handleResult.data as R | void | null;
3846
3873
  }
3847
3874
  return this.queryProperty(handleResult.data, handler);
3848
3875
  }
@@ -3850,54 +3877,63 @@ class Utils {
3850
3877
  * 异步-深度获取对象属性
3851
3878
  * @param target 待获取的对象
3852
3879
  * @param handler 获取属性的回调
3880
+ * @example
3881
+ * Utils.asyncQueryProperty(window, async (target)=>{
3882
+ * if(target.xxx){
3883
+ * return {
3884
+ * isFind: true,
3885
+ * data: target.xxx,
3886
+ * }
3887
+ * }else{
3888
+ * return {
3889
+ * isFind: false,
3890
+ * data: target.aabbcc,
3891
+ * }
3892
+ * }
3893
+ * })
3853
3894
  */
3854
- async asyncQueryProperty<T = any>(
3895
+ async asyncQueryProperty<T = any, R = any>(
3855
3896
  target: any,
3856
- handler: (target: T) =>
3857
- | {
3858
- /**
3859
- * 是否是需要的属性
3860
- * + true 将目标值赋值给data
3861
- * + false 不是需要的,data为下一个处理的对象
3862
- */
3863
- isFind: boolean;
3864
- /**
3865
- * 对象/目标值
3866
- */
3867
- data: any;
3868
- }
3869
- | Promise<{
3870
- /**
3871
- * 是否是需要的属性
3872
- * + true 将目标值赋值给data
3873
- * + false 不是需要的,data为下一个处理的对象
3874
- */
3875
- isFind: boolean;
3876
- /**
3877
- * 对象/目标值
3878
- */
3879
- data: any;
3880
- }>
3881
- ): Promise<Awaited<T>> {
3897
+ handler: (
3898
+ /**
3899
+ * 该值为返回的data值
3900
+ */
3901
+ target: T
3902
+ ) => IPromise<{
3903
+ /**
3904
+ * 是否是需要的属性
3905
+ * + true 将目标值赋值给data
3906
+ * + false 不是需要的,data为下一个处理的对象
3907
+ */
3908
+ isFind: boolean;
3909
+ /**
3910
+ * 对象/目标值
3911
+ *
3912
+ * 若`isFind`为true,那该值为返回的结果
3913
+ *
3914
+ * 若`isFind`为false,那该值为下次迭代传入的`target`
3915
+ */
3916
+ data: T | R | null | void;
3917
+ }>
3918
+ ): Promise<R | void | null> {
3882
3919
  if (target == null) {
3883
- // @ts-expect-error 空返回
3884
3920
  return;
3885
3921
  }
3886
3922
  const handleResult = await handler(target);
3887
3923
  if (handleResult && typeof handleResult.isFind === "boolean" && handleResult.isFind) {
3888
- return handleResult.data;
3924
+ return handleResult.data as R | void | null;
3889
3925
  }
3890
3926
  return await this.asyncQueryProperty(handleResult.data, handler);
3891
3927
  }
3892
3928
  /**
3893
3929
  * 创建一个新的Utils实例
3894
3930
  * @param option
3895
- * @returns
3931
+ * @example
3932
+ * Utils.createUtils();
3896
3933
  */
3897
3934
  createUtils(option?: WindowApiOption) {
3898
3935
  return new Utils(option);
3899
3936
  }
3900
-
3901
3937
  /**
3902
3938
  * 将对象转换为FormData
3903
3939
  * @param data 待转换的对象
@@ -3963,7 +3999,9 @@ class Utils {
3963
3999
  /**
3964
4000
  * 覆盖对象中的函数this指向
3965
4001
  * @param target 需要覆盖的对象
3966
- * @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
4002
+ * @param objectThis 覆盖的this指向,如果为传入,则默认为对象本身
4003
+ * @example
4004
+ * Utils.coverObjectFunctionThis({})
3967
4005
  */
3968
4006
  coverObjectFunctionThis = CommonUtil.coverObjectFunctionThis.bind(CommonUtil);
3969
4007
  /**
@@ -4014,7 +4052,7 @@ class Utils {
4014
4052
  /**
4015
4053
  * 自动使用 Worker 执行 setTimeout
4016
4054
  * @param callback 回调函数
4017
- * @param [timeout=0] 延迟时间,默认为0
4055
+ * @param timeout 延迟时间,默认为0
4018
4056
  */
4019
4057
  workerSetTimeout(callback: (...args: any[]) => any, timeout: number = 0) {
4020
4058
  try {
@@ -4115,6 +4153,10 @@ class Utils {
4115
4153
  /**
4116
4154
  * 判断页面中是否存在`worker-src`的CSP规则
4117
4155
  * @param timeout 超时时间,默认为`1500ms`
4156
+ * @example
4157
+ * Utils.hasWorkerCSP().then((hasCSP) => {
4158
+ * console.log(hasCSP);
4159
+ * })
4118
4160
  */
4119
4161
  hasWorkerCSP(timeout: number = 1500) {
4120
4162
  return new Promise<boolean>((resolve) => {
@@ -4197,6 +4239,9 @@ class Utils {
4197
4239
  * @param positionY 坐标y信息
4198
4240
  * @param otherPositionX 坐标x信息
4199
4241
  * @param otherPositionY 坐标y信息
4242
+ * @example
4243
+ * Utils.calcPositionDistance(1, 2, 3, 4);
4244
+ * > 2.8284271247461903
4200
4245
  */
4201
4246
  calcPositionDistance(
4202
4247
  positionX: number | string,
@@ -4208,6 +4253,9 @@ class Utils {
4208
4253
  * 计算两个坐标的直线距离
4209
4254
  * @param position 坐标信息
4210
4255
  * @param otherPosition 坐标信息
4256
+ * @example
4257
+ * Utils.calcPositionDistance({x: 1, y: 2}, {x: 3, y: 4});
4258
+ * > 2.8284271247461903
4211
4259
  */
4212
4260
  calcPositionDistance(
4213
4261
  position: { x: number | string; y: number | string },