@whitesev/utils 2.9.0 → 2.9.2

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.
@@ -1539,6 +1539,44 @@ declare class Utils {
1539
1539
  * @param timeId setInterval 返回的`id`
1540
1540
  */
1541
1541
  workerClearInterval(timeId: number | undefined): void;
1542
+ /**
1543
+ * 构造一个函数(可为异步函数)
1544
+ *
1545
+ * 前面的是入参,后面的是代码字符串
1546
+ *
1547
+ * 如果无入参,那么直接是代码字符串
1548
+ *
1549
+ * 注意:至少传入一个参数
1550
+ * @param args
1551
+ * @example
1552
+ * const fn = utils.createFunction("console.log(123)");
1553
+ * > 123
1554
+ * @example
1555
+ * const fn = await utils.createFunction("console.log(123)", true);
1556
+ * > 123
1557
+ * @example
1558
+ * const asyncFn = await utils.createFunction("a", "return a", true);
1559
+ * asyncFn(111);
1560
+ * > 111
1561
+ * @example
1562
+ * const asyncFn = await utils.createFunction("a", "b", "return a + b", true);
1563
+ * asyncFn(1,2);
1564
+ * > 3
1565
+ * @example
1566
+ * const fn = utils.createFunction("a", "b", "return a + b", false);
1567
+ * fn(4,5);
1568
+ * > 9
1569
+ */
1570
+ createFunction<R = any>(code: string): () => R;
1571
+ createFunction<R = any>(param: string, code: string): (param: any) => R;
1572
+ createFunction<P extends string[]>(...params: [...P, code: string]): (...args: {
1573
+ [K in keyof P]: any;
1574
+ }) => any;
1575
+ createFunction<P extends string[], T extends boolean>(...params: [...P, code: string, isAsync: T]): T extends true ? (...params: {
1576
+ [K in keyof P]: any;
1577
+ }) => Promise<any> : (...args: {
1578
+ [K in keyof P]: any;
1579
+ }) => any;
1542
1580
  }
1543
1581
  declare const utils: Utils;
1544
1582
  export { utils as Utils };
@@ -122,7 +122,9 @@ export declare interface UtilsAjaxHookResult {
122
122
  };
123
123
  });
124
124
  */
125
- hook(callback: (request: UtilsAjaxHookRequestOptions) => void | Promise<undefined>): void;
125
+ hook(
126
+ callback: (request: UtilsAjaxHookRequestOptions) => void | undefined | null | Promise<void | undefined | null>
127
+ ): void;
126
128
  /**
127
129
  * 过滤
128
130
  * @example
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.9.0",
4
+ "version": "2.9.2",
5
5
  "type": "module",
6
6
  "description": "一个常用的工具库",
7
7
  "main": "dist/index.cjs.js",
@@ -39,29 +39,26 @@
39
39
  "worker-timers": "^8.0.25"
40
40
  },
41
41
  "devDependencies": {
42
- "@eslint/js": "^9.36.0",
42
+ "@eslint/js": "^9.37.0",
43
43
  "@rollup/plugin-commonjs": "^28.0.6",
44
44
  "@rollup/plugin-json": "^6.1.0",
45
45
  "@rollup/plugin-node-resolve": "^16.0.1",
46
46
  "@rollup/plugin-terser": "^0.4.4",
47
47
  "@rollup/plugin-typescript": "^12.1.4",
48
- "browserslist": "^4.26.2",
49
- "caniuse-lite": "^1.0.30001745",
50
- "eslint": "^9.36.0",
48
+ "browserslist": "^4.26.3",
49
+ "caniuse-lite": "^1.0.30001747",
50
+ "eslint": "^9.37.0",
51
51
  "eslint-config-prettier": "^10.1.8",
52
52
  "eslint-plugin-compat": "^6.0.2",
53
53
  "eslint-plugin-prettier": "^5.5.4",
54
54
  "globals": "^16.4.0",
55
- "rollup": "^4.52.3",
55
+ "rollup": "^4.52.4",
56
56
  "rollup-plugin-clear": "^2.0.7",
57
57
  "tslib": "^2.8.1",
58
- "typescript": "^5.9.2",
59
- "typescript-eslint": "^8.44.1"
58
+ "typescript": "^5.9.3",
59
+ "typescript-eslint": "^8.45.0"
60
60
  },
61
61
  "scripts": {
62
- "install": "pnpm install --ignore-workspace",
63
- "outdated": "pnpm outdated --ignore-workspace",
64
- "update": "pnpm update --latest --ignore-workspace",
65
62
  "lint": "eslint .",
66
63
  "lint:fix": "eslint . --fix",
67
64
  "format": "prettier . --write",
package/src/Utils.ts CHANGED
@@ -3771,6 +3771,57 @@ class Utils {
3771
3771
  this.windowApi.clearInterval(timeId);
3772
3772
  }
3773
3773
  }
3774
+ /**
3775
+ * 构造一个函数(可为异步函数)
3776
+ *
3777
+ * 前面的是入参,后面的是代码字符串
3778
+ *
3779
+ * 如果无入参,那么直接是代码字符串
3780
+ *
3781
+ * 注意:至少传入一个参数
3782
+ * @param args
3783
+ * @example
3784
+ * const fn = utils.createFunction("console.log(123)");
3785
+ * > 123
3786
+ * @example
3787
+ * const fn = await utils.createFunction("console.log(123)", true);
3788
+ * > 123
3789
+ * @example
3790
+ * const asyncFn = await utils.createFunction("a", "return a", true);
3791
+ * asyncFn(111);
3792
+ * > 111
3793
+ * @example
3794
+ * const asyncFn = await utils.createFunction("a", "b", "return a + b", true);
3795
+ * asyncFn(1,2);
3796
+ * > 3
3797
+ * @example
3798
+ * const fn = utils.createFunction("a", "b", "return a + b", false);
3799
+ * fn(4,5);
3800
+ * > 9
3801
+ */
3802
+ createFunction<R = any>(code: string): () => R;
3803
+ createFunction<R = any>(param: string, code: string): (param: any) => R;
3804
+ createFunction<P extends string[]>(...params: [...P, code: string]): (...args: { [K in keyof P]: any }) => any;
3805
+ createFunction<P extends string[], T extends boolean>(
3806
+ ...params: [...P, code: string, isAsync: T]
3807
+ ): T extends true ? (...params: { [K in keyof P]: any }) => Promise<any> : (...args: { [K in keyof P]: any }) => any;
3808
+ createFunction<A extends string[], T extends boolean>(
3809
+ ...args: [...A, code: string, isAsync?: T]
3810
+ ): T extends true ? (...args: any[]) => Promise<any> : (...args: any[]) => any {
3811
+ let isAsync: string | boolean | undefined = args[args.length - 1];
3812
+ if (typeof isAsync === "boolean") {
3813
+ args.splice(args.length - 1, 1);
3814
+ } else {
3815
+ isAsync = false;
3816
+ }
3817
+ if (isAsync) {
3818
+ const AsyncFunctionConstructor = Object.getPrototypeOf(async function () {}).constructor;
3819
+ return new AsyncFunctionConstructor(...args);
3820
+ } else {
3821
+ const FunctionConstructor = Object.getPrototypeOf(function () {}).constructor;
3822
+ return new FunctionConstructor(...args);
3823
+ }
3824
+ }
3774
3825
  }
3775
3826
 
3776
3827
  const utils = new Utils();
@@ -122,7 +122,9 @@ export declare interface UtilsAjaxHookResult {
122
122
  };
123
123
  });
124
124
  */
125
- hook(callback: (request: UtilsAjaxHookRequestOptions) => void | Promise<undefined>): void;
125
+ hook(
126
+ callback: (request: UtilsAjaxHookRequestOptions) => void | undefined | null | Promise<void | undefined | null>
127
+ ): void;
126
128
  /**
127
129
  * 过滤
128
130
  * @example