@whitesev/utils 2.2.5 → 2.2.7

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.
@@ -19,6 +19,7 @@ export type HttpxResponseMap = {
19
19
  stream: ReadableStream<string>;
20
20
  document: Document;
21
21
  undefined: string;
22
+ html: string;
22
23
  };
23
24
  /**
24
25
  * headers的配置
@@ -1208,7 +1209,7 @@ declare class Httpx {
1208
1209
  * @param url 网址
1209
1210
  * @param details 配置
1210
1211
  */
1211
- get<T extends HttpxDetails>(url: string, details: T): HttpxPromise<HttpxAsyncResult<T>>;
1212
+ get<T extends HttpxDetails>(url: string, details?: T): HttpxPromise<HttpxAsyncResult<T>>;
1212
1213
  /**
1213
1214
  * GET 请求
1214
1215
  * @param details 配置
@@ -1224,7 +1225,7 @@ declare class Httpx {
1224
1225
  * @param url 网址
1225
1226
  * @param details 配置
1226
1227
  */
1227
- post<T extends HttpxDetails>(url: string, details: T): HttpxPromise<HttpxAsyncResult<T>>;
1228
+ post<T extends HttpxDetails>(url: string, details?: T): HttpxPromise<HttpxAsyncResult<T>>;
1228
1229
  /**
1229
1230
  * HEAD 请求
1230
1231
  * @param details 配置
@@ -1235,7 +1236,7 @@ declare class Httpx {
1235
1236
  * @param url 网址
1236
1237
  * @param details 配置
1237
1238
  */
1238
- head<T extends HttpxDetails>(url: string, details: T): HttpxPromise<HttpxAsyncResult<T>>;
1239
+ head<T extends HttpxDetails>(url: string, details?: T): HttpxPromise<HttpxAsyncResult<T>>;
1239
1240
  /**
1240
1241
  * OPTIONS 请求
1241
1242
  * @param details 配置
@@ -1246,7 +1247,7 @@ declare class Httpx {
1246
1247
  * @param url 网址
1247
1248
  * @param details 配置
1248
1249
  */
1249
- options<T extends HttpxDetails>(url: string, details: T): HttpxPromise<HttpxAsyncResult<T>>;
1250
+ options<T extends HttpxDetails>(url: string, details?: T): HttpxPromise<HttpxAsyncResult<T>>;
1250
1251
  /**
1251
1252
  * DELETE 请求
1252
1253
  * @param details 配置
@@ -1257,7 +1258,7 @@ declare class Httpx {
1257
1258
  * @param url 网址
1258
1259
  * @param details 配置
1259
1260
  */
1260
- delete<T extends HttpxDetails>(url: string, details: T): HttpxPromise<HttpxAsyncResult<T>>;
1261
+ delete<T extends HttpxDetails>(url: string, details?: T): HttpxPromise<HttpxAsyncResult<T>>;
1261
1262
  /**
1262
1263
  * PUT 请求
1263
1264
  * @param details 配置
@@ -1268,6 +1269,6 @@ declare class Httpx {
1268
1269
  * @param url 网址
1269
1270
  * @param details 配置
1270
1271
  */
1271
- put<T extends HttpxDetails>(url: string, details: T): HttpxPromise<HttpxAsyncResult<T>>;
1272
+ put<T extends HttpxDetails>(url: string, details?: T): HttpxPromise<HttpxAsyncResult<T>>;
1272
1273
  }
1273
1274
  export { Httpx };
@@ -254,6 +254,7 @@ declare class Utils {
254
254
  * Utils.formatByteToSize("812304",false);
255
255
  * > 793.27
256
256
  **/
257
+ formatByteToSize(byteSize: number | string): number;
257
258
  formatByteToSize<T extends boolean>(byteSize: number | string, addType?: T): T extends true ? string : number;
258
259
  /**
259
260
  * 应用场景: 当你想要获取数组形式的元素时,它可能是其它的选择器,那么需要按照先后顺序填入参数
@@ -430,6 +431,20 @@ declare class Utils {
430
431
  * > 456
431
432
  */
432
433
  getMaxValue(val: UtilsOwnObject<number>, handler: (key: any, value: any) => number): number;
434
+ /**
435
+ * 获取页面中最大的z-index的元素信息
436
+ * @param deviation 获取最大的z-index值的偏移,默认是+1
437
+ * @example
438
+ * Utils.getMaxZIndexNodeInfo();
439
+ * > {
440
+ * node: ...,
441
+ * zIndex: 1001
442
+ * }
443
+ **/
444
+ getMaxZIndexNodeInfo(deviation?: number): {
445
+ node: Element;
446
+ zIndex: number;
447
+ };
433
448
  /**
434
449
  * 获取页面中最大的z-index
435
450
  * @param deviation 获取最大的z-index值的偏移,默认是+1
@@ -1221,7 +1236,7 @@ declare class Utils {
1221
1236
  * @param startIndex (可选)开始坐标,可为空
1222
1237
  * @param endIndex (可选)结束坐标,可为空
1223
1238
  */
1224
- selectElementText(element: HTMLElement | Element | Node, childTextNode: ChildNode, startIndex?: number, endIndex?: number): void;
1239
+ selectElementText(element: HTMLElement | Element | Node, childTextNode?: ChildNode, startIndex?: number, endIndex?: number): void;
1225
1240
  /**
1226
1241
  * 复制到剪贴板
1227
1242
  * @param data 需要复制到剪贴板的文本
@@ -1343,12 +1358,33 @@ declare class Utils {
1343
1358
  * Utils.toJSON("{123:123}")
1344
1359
  * > {123:123}
1345
1360
  */
1346
- toJSON<T extends AnyObject>(data: string | null, errorCallBack?: (error: Error) => void): T;
1361
+ toJSON<T extends any>(data: string | null, errorCallBack?: (error: Error) => void): T;
1347
1362
  /**
1348
1363
  * 对象转为UrlSearchParams格式的字符串
1349
1364
  * @param obj 目标对象,可以是对象组成的数组
1365
+ * @param addPrefix 是否添加前缀?
1366
+ * @example
1367
+ * Utils.toSearchParamsStr({
1368
+ * "test": 1,
1369
+ * "test2": 2
1370
+ * })
1371
+ * > test=1&test2=2
1372
+ * @example
1373
+ * Utils.toSearchParamsStr([{
1374
+ * "test": 1,
1375
+ * "test2": 2
1376
+ * },
1377
+ * {
1378
+ * "test3": 3
1379
+ * }
1380
+ * ])
1381
+ * > test=1&test2=2&test3=3
1382
+ */
1383
+ toSearchParamsStr(obj: object | object[], addPrefix?: boolean): string;
1384
+ /**
1385
+ * 将UrlSearchParams格式的字符串转为对象
1350
1386
  */
1351
- toSearchParamsStr(obj: object | object[]): string;
1387
+ searchParamStrToObj<T extends any>(searhParamsStr?: string | null | undefined): T;
1352
1388
  /**
1353
1389
  * 提供一个封装了 try-catch 的函数,可以执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
1354
1390
  * @example
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/utils",
3
- "version": "2.2.5",
3
+ "version": "2.2.7",
4
4
  "description": "一个常用的工具库",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
@@ -44,6 +44,7 @@
44
44
  "typescript": "^5.5.4"
45
45
  },
46
46
  "scripts": {
47
- "build": "rollup --config"
47
+ "build": "rollup --config",
48
+ "build:all": "rollup --config"
48
49
  }
49
50
  }
package/src/Httpx.ts CHANGED
@@ -102,6 +102,7 @@ export type HttpxResponseMap = {
102
102
  stream: ReadableStream<string>;
103
103
  document: Document;
104
104
  undefined: string;
105
+ html: string;
105
106
  };
106
107
 
107
108
  /**
@@ -2322,7 +2323,7 @@ class Httpx {
2322
2323
  */
2323
2324
  async get<T extends HttpxDetails>(
2324
2325
  url: string,
2325
- details: T // @ts-ignore
2326
+ details?: T // @ts-ignore
2326
2327
  ): HttpxPromise<HttpxAsyncResult<T>>;
2327
2328
  /**
2328
2329
  * GET 请求
@@ -2386,7 +2387,7 @@ class Httpx {
2386
2387
  */
2387
2388
  async post<T extends HttpxDetails>(
2388
2389
  url: string,
2389
- details: T // @ts-ignore
2390
+ details?: T // @ts-ignore
2390
2391
  ): HttpxPromise<HttpxAsyncResult<T>>;
2391
2392
  /**
2392
2393
  * POST 请求
@@ -2440,7 +2441,7 @@ class Httpx {
2440
2441
  */
2441
2442
  async head<T extends HttpxDetails>(
2442
2443
  url: string,
2443
- details: T // @ts-ignore
2444
+ details?: T // @ts-ignore
2444
2445
  ): HttpxPromise<HttpxAsyncResult<T>>;
2445
2446
  /**
2446
2447
  * HEAD 请求
@@ -2499,7 +2500,7 @@ class Httpx {
2499
2500
  */
2500
2501
  options<T extends HttpxDetails>(
2501
2502
  url: string,
2502
- details: T // @ts-ignore
2503
+ details?: T // @ts-ignore
2503
2504
  ): HttpxPromise<HttpxAsyncResult<T>>;
2504
2505
  /**
2505
2506
  * OPTIONS 请求
@@ -2555,7 +2556,7 @@ class Httpx {
2555
2556
  */
2556
2557
  async delete<T extends HttpxDetails>(
2557
2558
  url: string,
2558
- details: T // @ts-ignore
2559
+ details?: T // @ts-ignore
2559
2560
  ): HttpxPromise<HttpxAsyncResult<T>>;
2560
2561
  /**
2561
2562
  * DELETE 请求
@@ -2612,7 +2613,7 @@ class Httpx {
2612
2613
  */
2613
2614
  async put<T extends HttpxDetails>(
2614
2615
  url: string,
2615
- details: T // @ts-ignore
2616
+ details?: T // @ts-ignore
2616
2617
  ): HttpxPromise<HttpxAsyncResult<T>>;
2617
2618
  /**
2618
2619
  * PUT 请求
package/src/Utils.ts CHANGED
@@ -52,7 +52,7 @@ class Utils {
52
52
  this.windowApi = new WindowApi(option);
53
53
  }
54
54
  /** 版本号 */
55
- version = "2024.9.4";
55
+ version = "2024.9.10";
56
56
 
57
57
  /**
58
58
  * 在页面中增加style元素,如果html节点存在子节点,添加子节点第一个,反之,添加到html节点的子节点最后一个
@@ -724,6 +724,7 @@ class Utils {
724
724
  * Utils.formatByteToSize("812304",false);
725
725
  * > 793.27
726
726
  **/
727
+ formatByteToSize(byteSize: number | string): number;
727
728
  formatByteToSize<T extends boolean>(
728
729
  byteSize: number | string,
729
730
  addType?: T
@@ -1225,43 +1226,90 @@ class Utils {
1225
1226
  }
1226
1227
  }
1227
1228
  /**
1228
- * 获取页面中最大的z-index
1229
+ * 获取页面中最大的z-index的元素信息
1229
1230
  * @param deviation 获取最大的z-index值的偏移,默认是+1
1230
1231
  * @example
1231
- * Utils.getMaxZIndex();
1232
- * > 1001
1232
+ * Utils.getMaxZIndexNodeInfo();
1233
+ * > {
1234
+ * node: ...,
1235
+ * zIndex: 1001
1236
+ * }
1233
1237
  **/
1234
- getMaxZIndex(deviation?: number): number;
1235
- getMaxZIndex(deviation = 1): number {
1238
+ getMaxZIndexNodeInfo(deviation?: number): {
1239
+ node: Element;
1240
+ zIndex: number;
1241
+ };
1242
+ getMaxZIndexNodeInfo(deviation = 1): {
1243
+ node: Element;
1244
+ zIndex: number;
1245
+ } {
1236
1246
  deviation = Number.isNaN(deviation) ? 1 : deviation;
1247
+ const UtilsContext = this;
1237
1248
  // 最大值2147483647
1238
- let maxZIndex = Math.pow(2, 31) - 1;
1249
+ const maxZIndex = Math.pow(2, 31) - 1;
1239
1250
  // 比较值2000000000
1240
- let maxZIndexCompare = 2 * Math.pow(10, 9);
1251
+ const maxZIndexCompare = 2 * Math.pow(10, 9);
1241
1252
  // 当前页面最大的z-index
1242
1253
  let zIndex = 0;
1243
1254
  // 当前的最大z-index的元素,调试使用
1244
1255
  // @ts-ignore
1245
- let maxZIndexNode = null;
1246
- this.windowApi.document.querySelectorAll("*").forEach(($ele, index) => {
1247
- let nodeStyle = this.windowApi.window.getComputedStyle($ele);
1256
+ let maxZIndexNode: Element = null;
1257
+ /**
1258
+ * 元素是否可见
1259
+ * @param $css
1260
+ */
1261
+ function isVisibleNode($css: CSSStyleDeclaration): boolean {
1262
+ return $css.position !== "static" && $css.display !== "none";
1263
+ }
1264
+ /**
1265
+ * 查询元素的z-index
1266
+ * 并比较值是否是已获取的最大值
1267
+ * @param $ele
1268
+ */
1269
+ function queryMaxZIndex($ele: Element) {
1270
+ /** 元素的样式 */
1271
+ const nodeStyle = UtilsContext.windowApi.window.getComputedStyle($ele);
1248
1272
  /* 不对position为static和display为none的元素进行获取它们的z-index */
1249
- if (nodeStyle.position !== "static" && nodeStyle.display !== "none") {
1273
+ if (isVisibleNode(nodeStyle)) {
1250
1274
  let nodeZIndex = parseInt(nodeStyle.zIndex);
1251
1275
  if (!isNaN(nodeZIndex)) {
1252
1276
  if (nodeZIndex > zIndex) {
1277
+ // 赋值到全局
1253
1278
  zIndex = nodeZIndex;
1254
1279
  maxZIndexNode = $ele;
1255
1280
  }
1256
1281
  }
1282
+ // 判断shadowRoot
1283
+ if ($ele.shadowRoot != null && $ele instanceof ShadowRoot) {
1284
+ $ele.shadowRoot.querySelectorAll("*").forEach(($shadowEle) => {
1285
+ queryMaxZIndex($shadowEle);
1286
+ });
1287
+ }
1257
1288
  }
1289
+ }
1290
+ this.windowApi.document.querySelectorAll("*").forEach(($ele, index) => {
1291
+ queryMaxZIndex($ele);
1258
1292
  });
1259
1293
  zIndex += deviation;
1260
1294
  if (zIndex >= maxZIndexCompare) {
1261
1295
  // 最好不要超过最大值
1262
1296
  zIndex = maxZIndex;
1263
1297
  }
1264
- return zIndex;
1298
+ return {
1299
+ node: maxZIndexNode,
1300
+ zIndex: zIndex,
1301
+ };
1302
+ }
1303
+ /**
1304
+ * 获取页面中最大的z-index
1305
+ * @param deviation 获取最大的z-index值的偏移,默认是+1
1306
+ * @example
1307
+ * Utils.getMaxZIndex();
1308
+ * > 1001
1309
+ **/
1310
+ getMaxZIndex(deviation?: number): number;
1311
+ getMaxZIndex(deviation = 1): number {
1312
+ return this.getMaxZIndexNodeInfo(deviation).zIndex;
1265
1313
  }
1266
1314
  /**
1267
1315
  * 获取最小值
@@ -3111,13 +3159,13 @@ class Utils {
3111
3159
  */
3112
3160
  selectElementText(
3113
3161
  element: HTMLElement | Element | Node,
3114
- childTextNode: ChildNode,
3162
+ childTextNode?: ChildNode,
3115
3163
  startIndex?: number,
3116
3164
  endIndex?: number
3117
3165
  ): void;
3118
3166
  selectElementText(
3119
3167
  element: HTMLElement | Element | Node,
3120
- childTextNode: ChildNode,
3168
+ childTextNode?: ChildNode,
3121
3169
  startIndex?: number,
3122
3170
  endIndex?: number
3123
3171
  ): void {
@@ -3764,16 +3812,16 @@ class Utils {
3764
3812
  * Utils.toJSON("{123:123}")
3765
3813
  * > {123:123}
3766
3814
  */
3767
- toJSON<T extends AnyObject>(
3815
+ toJSON<T extends any>(
3768
3816
  data: string | null,
3769
3817
  errorCallBack?: (error: Error) => void
3770
3818
  ): T;
3771
- toJSON<T extends AnyObject>(
3819
+ toJSON<T extends any>(
3772
3820
  data: string | null,
3773
3821
  errorCallBack?: (error: Error) => void
3774
3822
  ): T {
3775
3823
  let UtilsContext = this;
3776
- let result: AnyObject = {};
3824
+ let result: any = {};
3777
3825
  if (typeof data === "object") {
3778
3826
  return data as any;
3779
3827
  }
@@ -3822,9 +3870,26 @@ class Utils {
3822
3870
  /**
3823
3871
  * 对象转为UrlSearchParams格式的字符串
3824
3872
  * @param obj 目标对象,可以是对象组成的数组
3873
+ * @param addPrefix 是否添加前缀?
3874
+ * @example
3875
+ * Utils.toSearchParamsStr({
3876
+ * "test": 1,
3877
+ * "test2": 2
3878
+ * })
3879
+ * > test=1&test2=2
3880
+ * @example
3881
+ * Utils.toSearchParamsStr([{
3882
+ * "test": 1,
3883
+ * "test2": 2
3884
+ * },
3885
+ * {
3886
+ * "test3": 3
3887
+ * }
3888
+ * ])
3889
+ * > test=1&test2=2&test3=3
3825
3890
  */
3826
- toSearchParamsStr(obj: object | object[]): string;
3827
- toSearchParamsStr(obj: object | object[]): string {
3891
+ toSearchParamsStr(obj: object | object[], addPrefix?: boolean): string;
3892
+ toSearchParamsStr(obj: object | object[], addPrefix?: boolean): string {
3828
3893
  let UtilsContext = this;
3829
3894
  let searhParamsStr = "";
3830
3895
  if (Array.isArray(obj)) {
@@ -3838,8 +3903,22 @@ class Utils {
3838
3903
  } else {
3839
3904
  searhParamsStr = new URLSearchParams(Object.entries(obj)).toString();
3840
3905
  }
3906
+ if (addPrefix && !searhParamsStr.startsWith("?")) {
3907
+ searhParamsStr = "?" + searhParamsStr;
3908
+ }
3841
3909
  return searhParamsStr;
3842
3910
  }
3911
+ /**
3912
+ * 将UrlSearchParams格式的字符串转为对象
3913
+ */
3914
+ searchParamStrToObj<T extends any>(searhParamsStr?: string | null | undefined): T {
3915
+ if (typeof searhParamsStr !== "string") {
3916
+ // @ts-ignore
3917
+ return {};
3918
+ }
3919
+ // @ts-ignore
3920
+ return Object.fromEntries(new URLSearchParams(searhParamsStr));
3921
+ }
3843
3922
  /**
3844
3923
  * 提供一个封装了 try-catch 的函数,可以执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
3845
3924
  * @example