@zwa73/utils 1.0.65 → 1.0.67

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.
@@ -1,4 +1,4 @@
1
- import { ComposedClassMult, ComposedClassPart, JToken, PromiseProcFn, PromiseVerifyFn } from "./UtilInterfaces";
1
+ import { ComposedClassMult, ComposedClassPart, ComposedMixinable, JToken, PromiseProcFn, PromiseVerifyFn } from "./UtilInterfaces";
2
2
  /**常用函数 */
3
3
  export declare namespace UtilFunc {
4
4
  /**获取当前时间戳
@@ -65,6 +65,8 @@ export declare namespace UtilFunc {
65
65
  * @returns 混合完成的类
66
66
  */
67
67
  function composeClassPart<Base extends object, Mixin extends object, Field extends keyof Mixin>(base: Base, mixin: Mixin, ...fields: Field[]): ComposedClassPart<Base, Mixin, Field>;
68
+ /**根据 MIXIN_FIELDS 自动混入 */
69
+ function composeMixinable<B extends object, M1, M2, M3, M4, M5, M6, M7, M8, M9>(base: B, m1?: M1, m2?: M2, m3?: M3, m4?: M4, m5?: M5, m6?: M6, m7?: M7, m8?: M8, m9?: M9): ComposedMixinable<B, [M1, M2, M3, M4, M5, M6, M4, M8, M9]>;
68
70
  /**类组合
69
71
  * 将mixinList每个成员的字段混入base
70
72
  * @param base - 基础类
@@ -217,7 +217,8 @@ var UtilFunc;
217
217
  });
218
218
  */
219
219
  if (typeof mixin[fd] === 'function') {
220
- compObj[fd] = mixin[fd].bind(mixin);
220
+ compObj[fd] = (...args) => mixin[fd].apply(mixin, args);
221
+ //(compObj as any)[fd] = (mixin[fd] as any).bind(mixin);
221
222
  }
222
223
  else {
223
224
  Object.defineProperty(compObj, fd, {
@@ -232,6 +233,13 @@ var UtilFunc;
232
233
  return compObj;
233
234
  }
234
235
  UtilFunc.composeClassPart = composeClassPart;
236
+ function composeMixinable(base, ...mixins) {
237
+ let out = base;
238
+ for (const mixin of mixins)
239
+ out = composeClassPart(base, mixin, ...mixin.MIXIN_FIELDS);
240
+ return out;
241
+ }
242
+ UtilFunc.composeMixinable = composeMixinable;
235
243
  /**类组合
236
244
  * 将mixinList每个成员的字段混入base
237
245
  * @param base - 基础类
@@ -100,4 +100,10 @@ export type MethodsThisAs<Methods, T> = {
100
100
  * @param prototype - 类的原型
101
101
  */
102
102
  export declare function assertThisAs<Self, T>(prototype: MethodsThisAs<Self, T>): void;
103
+ /**可自动混入的类型 */
104
+ export type MixinAble<Mixin> = {
105
+ MIXIN_FIELDS: ReadonlyArray<keyof Mixin>;
106
+ };
107
+ /**自动混入的组合类 */
108
+ export type ComposedMixinable<B extends object, Ms extends Array<unknown>> = Ms extends [infer M, ...infer Rest] ? M extends MixinAble<M> ? ComposedMixinable<ComposedClassPart<B, M, M['MIXIN_FIELDS'][number]>, Rest> : B : B;
103
109
  export {};
@@ -1,10 +1,12 @@
1
- declare class A {
1
+ import { MixinAble } from "..";
2
+ declare class A implements MixinAble<A> {
2
3
  private num;
3
4
  getNum: () => number;
4
5
  /**A的函数 */
5
6
  getA: () => this;
7
+ MIXIN_FIELDS: readonly ["getNum", "getA"];
6
8
  }
7
- declare class B {
9
+ declare class B implements MixinAble<B> {
8
10
  private text;
9
11
  getText: () => string;
10
12
  /**B的函数 */
@@ -13,14 +15,15 @@ declare class B {
13
15
  static getNewB: () => B;
14
16
  /**保留的函数 */
15
17
  privateFunc(): void;
18
+ MIXIN_FIELDS: readonly ["getText", "getB"];
16
19
  }
17
20
  declare class _C {
18
- static create(): _C & Pick<B, "getB" | "getText"> & Pick<A, "getA" | "getNum"> & Record<"bindFunc", () => "这是动态绑定的函数">;
21
+ static create(): _C & Pick<B, "getText" | "getB"> & Pick<A, "getNum" | "getA"> & Record<"bindFunc", () => "这是动态绑定的函数">;
19
22
  private constructor();
20
- getC(this: C): _C & Pick<B, "getB" | "getText"> & Pick<A, "getA" | "getNum"> & Record<"bindFunc", () => "这是动态绑定的函数">;
23
+ getC(this: C): _C & Pick<B, "getText" | "getB"> & Pick<A, "getNum" | "getA"> & Record<"bindFunc", () => "这是动态绑定的函数">;
21
24
  testFunc(this: C): number;
22
25
  }
23
- declare function composeC(obj: _C): _C & Pick<B, "getB" | "getText"> & Pick<A, "getA" | "getNum"> & Record<"bindFunc", () => "这是动态绑定的函数">;
26
+ declare function composeC(obj: _C): _C & Pick<B, "getText" | "getB"> & Pick<A, "getNum" | "getA"> & Record<"bindFunc", () => "这是动态绑定的函数">;
24
27
  export type C = ReturnType<typeof composeC>;
25
28
  export declare const C: typeof _C;
26
29
  export {};
@@ -7,6 +7,7 @@ class A {
7
7
  getNum = () => this.num;
8
8
  /**A的函数 */
9
9
  getA = () => this;
10
+ MIXIN_FIELDS = ['getNum', 'getA'];
10
11
  }
11
12
  class B {
12
13
  text = "B的text";
@@ -19,6 +20,7 @@ class B {
19
20
  privateFunc() {
20
21
  console.log();
21
22
  }
23
+ MIXIN_FIELDS = ['getText', 'getB'];
22
24
  }
23
25
  class _C {
24
26
  static create() {
@@ -35,9 +37,8 @@ class _C {
35
37
  }
36
38
  }
37
39
  function composeC(obj) {
38
- let ob1 = __1.UtilFunc.composeClassPart(obj, new B(), 'getB', 'getText');
39
- let ob2 = __1.UtilFunc.composeClassPart(ob1, new A(), 'getA', 'getNum');
40
- let ob3 = __1.UtilFP.bindTo('bindFunc', () => "这是动态绑定的函数", ob2);
40
+ let ob1 = __1.UtilFunc.composeMixinable(obj, new B(), new A());
41
+ let ob3 = __1.UtilFP.bindTo('bindFunc', () => "这是动态绑定的函数", ob1);
41
42
  return ob3;
42
43
  }
43
44
  exports.C = _C;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.65",
3
+ "version": "1.0.67",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,5 +1,5 @@
1
1
  import * as crypto from "crypto";
2
- import { ComposedClass, ComposedClassMult, ComposedClassPart, FuncPropNames, JToken, PromiseProcFn, PromiseStat, PromiseVerifyFn } from "@src/UtilInterfaces";
2
+ import { ComposedClass, ComposedClassMult, ComposedClassPart, ComposedMixinable, FuncPropNames, JToken, MixinAble, PromiseProcFn, PromiseStat, PromiseVerifyFn } from "@src/UtilInterfaces";
3
3
  import * as cp from "child_process";
4
4
  import { SLogger } from "@src/UtilLogger";
5
5
 
@@ -235,7 +235,8 @@ export function composeClassPart
235
235
  });
236
236
  */
237
237
  if(typeof mixin[fd] === 'function') {
238
- (compObj as any)[fd] = (mixin[fd] as any).bind(mixin);
238
+ (compObj as any)[fd] = (...args: any[]) => (mixin[fd] as any).apply(mixin, args);
239
+ //(compObj as any)[fd] = (mixin[fd] as any).bind(mixin);
239
240
  } else {
240
241
  Object.defineProperty(compObj, fd, {
241
242
  get: ()=>mixin[fd],
@@ -248,6 +249,20 @@ export function composeClassPart
248
249
  }
249
250
  return compObj;
250
251
  }
252
+
253
+ /**根据 MIXIN_FIELDS 自动混入 */
254
+ export function composeMixinable
255
+ <B extends object,
256
+ M1, M2, M3,M4, M5, M6,M7, M8, M9>
257
+ (base:B,m1?:M1,m2?:M2,m3?:M3,m4?:M4,m5?:M5,m6?:M6,m7?:M7,m8?:M8,m9?:M9):
258
+ ComposedMixinable<B,[M1,M2,M3,M4,M5,M6,M4,M8,M9]>
259
+ export function composeMixinable<Base extends object, Mixin extends MixinAble<Mixin>>(base:Base,...mixins:Mixin[]){
260
+ let out = base;
261
+ for(const mixin of mixins)
262
+ out = composeClassPart(base,mixin,...mixin.MIXIN_FIELDS);
263
+ return out;
264
+ }
265
+
251
266
  /**类组合
252
267
  * 将mixinList每个成员的字段混入base
253
268
  * @param base - 基础类
@@ -155,4 +155,15 @@ export type MethodsThisAs<Methods, T> = {
155
155
  * @template T - 目标this类型
156
156
  * @param prototype - 类的原型
157
157
  */
158
- export function assertThisAs<Self,T>(prototype: MethodsThisAs<Self, T>) {}
158
+ export function assertThisAs<Self,T>(prototype: MethodsThisAs<Self, T>) {}
159
+
160
+ /**可自动混入的类型 */
161
+ export type MixinAble<Mixin> = {MIXIN_FIELDS:ReadonlyArray<keyof Mixin>};
162
+
163
+ /**自动混入的组合类 */
164
+ export type ComposedMixinable<B extends object, Ms extends Array<unknown>> =
165
+ Ms extends [infer M, ...infer Rest]
166
+ ? M extends MixinAble<M>
167
+ ? ComposedMixinable<ComposedClassPart<B,M,M['MIXIN_FIELDS'][number]>,Rest>
168
+ : B
169
+ : B
@@ -1,12 +1,13 @@
1
- import { UtilFP, UtilFunc, assertThisAs } from ".."
1
+ import { MixinAble, UtilFP, UtilFunc, assertThisAs } from ".."
2
2
 
3
- class A {
3
+ class A implements MixinAble<A>{
4
4
  private num = 1;
5
5
  getNum = ()=>this.num;
6
6
  /**A的函数 */
7
7
  getA=()=>this;
8
+ MIXIN_FIELDS =['getNum','getA'] as const;
8
9
  }
9
- class B {
10
+ class B implements MixinAble<B>{
10
11
  private text = "B的text";
11
12
  getText = ()=>this.text;
12
13
  /**B的函数 */
@@ -17,6 +18,7 @@ class B {
17
18
  privateFunc(){
18
19
  console.log()
19
20
  }
21
+ MIXIN_FIELDS = ['getText','getB'] as const;
20
22
  }
21
23
  class _C {
22
24
  static create(){
@@ -32,9 +34,8 @@ class _C {
32
34
  }
33
35
  }
34
36
  function composeC(obj:_C){
35
- let ob1 = UtilFunc.composeClassPart(obj,new B(),'getB','getText');
36
- let ob2 = UtilFunc.composeClassPart(ob1,new A(),'getA','getNum');
37
- let ob3 = UtilFP.bindTo('bindFunc',()=>"这是动态绑定的函数",ob2);
37
+ let ob1 = UtilFunc.composeMixinable(obj,new B(),new A());
38
+ let ob3 = UtilFP.bindTo('bindFunc',()=>"这是动态绑定的函数",ob1);
38
39
  return ob3;
39
40
  }
40
41
  export type C = ReturnType<typeof composeC>;