@whitesev/utils 2.11.8 → 2.11.9

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.
@@ -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.9",
5
5
  "description": "一个常用的工具库",
6
6
  "keywords": [
7
7
  "ScriptCat",
package/src/Utils.ts CHANGED
@@ -3816,8 +3816,22 @@ class Utils {
3816
3816
  * 深度获取对象的某个属性
3817
3817
  * @param target 待获取的对象
3818
3818
  * @param handler 获取属性的回调
3819
+ * @example
3820
+ * Utils.queryProperty(window,(target)=>{
3821
+ * if(target.xxx){
3822
+ * return {
3823
+ * isFind: true,
3824
+ * data: target.xxx,
3825
+ * }
3826
+ * }else{
3827
+ * return {
3828
+ * isFind: false,
3829
+ * data: target.aabbcc,
3830
+ * }
3831
+ * }
3832
+ * })
3819
3833
  */
3820
- queryProperty<T = any>(
3834
+ queryProperty<T = any, R = any>(
3821
3835
  target: any,
3822
3836
  handler: (
3823
3837
  /**
@@ -3833,16 +3847,20 @@ class Utils {
3833
3847
  isFind: boolean;
3834
3848
  /**
3835
3849
  * 对象/目标值
3850
+ *
3851
+ * 若`isFind`为true,那该值为返回的结果
3852
+ *
3853
+ * 若`isFind`为false,那该值为下次迭代传入的`target`
3836
3854
  */
3837
- data: any;
3855
+ data: T | R | null | void;
3838
3856
  }
3839
- ): any {
3857
+ ): R | void | null {
3840
3858
  if (target == null) {
3841
3859
  return;
3842
3860
  }
3843
3861
  const handleResult = handler(target);
3844
3862
  if (handleResult && typeof handleResult.isFind === "boolean" && handleResult.isFind) {
3845
- return handleResult.data;
3863
+ return handleResult.data as R | void | null;
3846
3864
  }
3847
3865
  return this.queryProperty(handleResult.data, handler);
3848
3866
  }
@@ -3850,54 +3868,63 @@ class Utils {
3850
3868
  * 异步-深度获取对象属性
3851
3869
  * @param target 待获取的对象
3852
3870
  * @param handler 获取属性的回调
3871
+ * @example
3872
+ * Utils.asyncQueryProperty(window, async (target)=>{
3873
+ * if(target.xxx){
3874
+ * return {
3875
+ * isFind: true,
3876
+ * data: target.xxx,
3877
+ * }
3878
+ * }else{
3879
+ * return {
3880
+ * isFind: false,
3881
+ * data: target.aabbcc,
3882
+ * }
3883
+ * }
3884
+ * })
3853
3885
  */
3854
- async asyncQueryProperty<T = any>(
3886
+ async asyncQueryProperty<T = any, R = any>(
3855
3887
  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>> {
3888
+ handler: (
3889
+ /**
3890
+ * 该值为返回的data值
3891
+ */
3892
+ target: T
3893
+ ) => IPromise<{
3894
+ /**
3895
+ * 是否是需要的属性
3896
+ * + true 将目标值赋值给data
3897
+ * + false 不是需要的,data为下一个处理的对象
3898
+ */
3899
+ isFind: boolean;
3900
+ /**
3901
+ * 对象/目标值
3902
+ *
3903
+ * 若`isFind`为true,那该值为返回的结果
3904
+ *
3905
+ * 若`isFind`为false,那该值为下次迭代传入的`target`
3906
+ */
3907
+ data: T | R | null | void;
3908
+ }>
3909
+ ): Promise<R | void | null> {
3882
3910
  if (target == null) {
3883
- // @ts-expect-error 空返回
3884
3911
  return;
3885
3912
  }
3886
3913
  const handleResult = await handler(target);
3887
3914
  if (handleResult && typeof handleResult.isFind === "boolean" && handleResult.isFind) {
3888
- return handleResult.data;
3915
+ return handleResult.data as R | void | null;
3889
3916
  }
3890
3917
  return await this.asyncQueryProperty(handleResult.data, handler);
3891
3918
  }
3892
3919
  /**
3893
3920
  * 创建一个新的Utils实例
3894
3921
  * @param option
3895
- * @returns
3922
+ * @example
3923
+ * Utils.createUtils();
3896
3924
  */
3897
3925
  createUtils(option?: WindowApiOption) {
3898
3926
  return new Utils(option);
3899
3927
  }
3900
-
3901
3928
  /**
3902
3929
  * 将对象转换为FormData
3903
3930
  * @param data 待转换的对象
@@ -3963,7 +3990,9 @@ class Utils {
3963
3990
  /**
3964
3991
  * 覆盖对象中的函数this指向
3965
3992
  * @param target 需要覆盖的对象
3966
- * @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
3993
+ * @param objectThis 覆盖的this指向,如果为传入,则默认为对象本身
3994
+ * @example
3995
+ * Utils.coverObjectFunctionThis({})
3967
3996
  */
3968
3997
  coverObjectFunctionThis = CommonUtil.coverObjectFunctionThis.bind(CommonUtil);
3969
3998
  /**
@@ -4014,7 +4043,7 @@ class Utils {
4014
4043
  /**
4015
4044
  * 自动使用 Worker 执行 setTimeout
4016
4045
  * @param callback 回调函数
4017
- * @param [timeout=0] 延迟时间,默认为0
4046
+ * @param timeout 延迟时间,默认为0
4018
4047
  */
4019
4048
  workerSetTimeout(callback: (...args: any[]) => any, timeout: number = 0) {
4020
4049
  try {
@@ -4115,6 +4144,10 @@ class Utils {
4115
4144
  /**
4116
4145
  * 判断页面中是否存在`worker-src`的CSP规则
4117
4146
  * @param timeout 超时时间,默认为`1500ms`
4147
+ * @example
4148
+ * Utils.hasWorkerCSP().then((hasCSP) => {
4149
+ * console.log(hasCSP);
4150
+ * })
4118
4151
  */
4119
4152
  hasWorkerCSP(timeout: number = 1500) {
4120
4153
  return new Promise<boolean>((resolve) => {
@@ -4197,6 +4230,9 @@ class Utils {
4197
4230
  * @param positionY 坐标y信息
4198
4231
  * @param otherPositionX 坐标x信息
4199
4232
  * @param otherPositionY 坐标y信息
4233
+ * @example
4234
+ * Utils.calcPositionDistance(1, 2, 3, 4);
4235
+ * > 2.8284271247461903
4200
4236
  */
4201
4237
  calcPositionDistance(
4202
4238
  positionX: number | string,
@@ -4208,6 +4244,9 @@ class Utils {
4208
4244
  * 计算两个坐标的直线距离
4209
4245
  * @param position 坐标信息
4210
4246
  * @param otherPosition 坐标信息
4247
+ * @example
4248
+ * Utils.calcPositionDistance({x: 1, y: 2}, {x: 3, y: 4});
4249
+ * > 2.8284271247461903
4211
4250
  */
4212
4251
  calcPositionDistance(
4213
4252
  position: { x: number | string; y: number | string },