@zwa73/utils 1.0.164 → 1.0.166

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,5 @@
1
- import { Literal } from "./UtilInterfaces";
1
+ import { Keyable, Literal } from "./UtilInterfaces";
2
+ import Handlebars from 'handlebars';
2
3
  /**函数管道器 */
3
4
  export declare class Piper<Arg, Result = Arg> {
4
5
  /**管道函数列表 */
@@ -69,4 +70,20 @@ export declare class Stream<T> implements Iterable<T> {
69
70
  [Symbol.iterator](): Iterator<T>;
70
71
  get length(): number;
71
72
  }
73
+ /**文本模板渲染器
74
+ * @template T - 上下文对象的类型,默认为记录类型
75
+ */
76
+ export declare class Hbs<T extends Record<Keyable, any> = Record<Keyable, any>> {
77
+ hbs: typeof Handlebars;
78
+ context: T;
79
+ /**创建一个新的 Hbs 实例
80
+ * @param context - 上下文对象,用于在渲染模板时提供数据
81
+ */
82
+ constructor(context?: T);
83
+ /**渲染模板
84
+ * @param template - 要渲染的 Handlebars 模板字符串
85
+ * @returns 渲染后的字符串
86
+ */
87
+ render(template: string): string;
88
+ }
72
89
  export {};
package/dist/UtilClass.js CHANGED
@@ -1,7 +1,12 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Stream = exports.Piper = void 0;
6
+ exports.Hbs = exports.Stream = exports.Piper = void 0;
4
7
  const QuickFunction_1 = require("./QuickFunction");
8
+ const handlebars_1 = __importDefault(require("handlebars"));
9
+ const UtilLogger_1 = require("./UtilLogger");
5
10
  /**函数管道器 */
6
11
  class Piper {
7
12
  /**管道函数列表 */
@@ -188,3 +193,44 @@ class Stream {
188
193
  }
189
194
  }
190
195
  exports.Stream = Stream;
196
+ /**文本模板渲染器
197
+ * @template T - 上下文对象的类型,默认为记录类型
198
+ */
199
+ class Hbs {
200
+ hbs = handlebars_1.default.create();
201
+ context;
202
+ /**创建一个新的 Hbs 实例
203
+ * @param context - 上下文对象,用于在渲染模板时提供数据
204
+ */
205
+ constructor(context = {}) {
206
+ this.context = context;
207
+ this.hbs.registerHelper('def', (name, options) => {
208
+ const value = options.fn(this.context).trim();
209
+ this.context[name] = value;
210
+ return '';
211
+ });
212
+ this.hbs.registerHelper('defli', (name, options) => {
213
+ const rawvalue = options.fn();
214
+ const values = rawvalue
215
+ .split('[[br]]')
216
+ .map(value => value.trim())
217
+ .map(value => this.hbs.compile(value)(this.context));
218
+ this.context[name] = values;
219
+ return '';
220
+ });
221
+ }
222
+ /**渲染模板
223
+ * @param template - 要渲染的 Handlebars 模板字符串
224
+ * @returns 渲染后的字符串
225
+ */
226
+ render(template) {
227
+ try {
228
+ return this.hbs.compile(template)(this.context);
229
+ }
230
+ catch (e) {
231
+ UtilLogger_1.SLogger.error(e);
232
+ return '';
233
+ }
234
+ }
235
+ }
236
+ exports.Hbs = Hbs;
@@ -1,4 +1,4 @@
1
- import { PRecord, AnyFunc, ComposedClass, ComposedMixinable, ComposedRefMixinable, ExtractOutcome, IJData, JObject, JToken, Keyable, Literal, Matchable, MatchableFlag, Mixinable, Outcome, ReqVerifyFn, ProperSubsetCheck, RefMixinable, UnionToIntersection, Await, AnyRecord, AllExtends } from "./UtilInterfaces";
1
+ import { PRecord, AnyFunc, ExtractOutcome, IJData, JObject, JToken, Keyable, Literal, Matchable, MatchableFlag, Outcome, ReqVerifyFn, ProperSubsetCheck, UnionToIntersection, AnyRecord, AllExtends } from "./UtilInterfaces";
2
2
  import { LogLevel } from "./UtilLogger";
3
3
  import { Failed, FailedLike, None, StatusSymbol, Success, SuccessLike, Timeout } from "./UtilSymbol";
4
4
  declare const HashMethodList: readonly ["md5", "sha1", "sha256", "sha512", "sha3", "blake2", "blake3"];
@@ -117,21 +117,6 @@ export declare class UtilFunc {
117
117
  * @returns 超时则返回 处理函数委托 完成则返回结果
118
118
  */
119
119
  static timelimitPromise<T>(func: () => Promise<T> | T, timeLimit?: number): Promise<SuccessOut<T> | TimeoutOut<T>>;
120
- /**部分类组合
121
- * 将mixin的部分字段混入base
122
- * @param base - 基础类
123
- * @param mixin - 目标类
124
- * @param key - mixin 对象在 base 中的 key
125
- * @param fields - 需要混入的字段
126
- * @returns 混合完成的类
127
- */
128
- static composeClassPart<Base extends AnyRecord, Mixin extends AnyRecord, Field extends keyof Mixin>(base: Base, mixin: Mixin, key: string, ...fields: Field[]): ComposedClass<Base, Mixin, typeof key, Field>;
129
- /**根据 MIXIN_FIELDS 自动混入
130
- * @deprecated 非必要情况下, 对class定义请使用 composeRefMixinable
131
- */
132
- static composeMixinable<Base extends AnyRecord, Mixins extends Mixinable<any>[]>(base: Base, ...mixins: Mixins): ComposedMixinable<Base, Mixins>;
133
- /**根据 MIXIN_FIELDS 自动混入 */
134
- static composeRefMixinable<Base extends AnyRecord, Mixins extends RefMixinable<any, unknown>[]>(base: Base, ...mixins: Mixins): ComposedRefMixinable<Base, Mixins>;
135
120
  /**对对象的每个属性应用映射函数,并返回一个新的对象。
136
121
  * @template T - 对象的类型
137
122
  * @param obj - 要处理的对象
@@ -139,7 +124,7 @@ export declare class UtilFunc {
139
124
  * @returns - 一个新的对象,它的属性是原对象的属性经过映射函数处理后的结果
140
125
  */
141
126
  static mapEntries<T extends AnyRecord>(obj: T, mapper: (key: keyof T, value: T[keyof T]) => T[keyof T]): T;
142
- /** 遍历对象的所有字段
127
+ /**遍历对象的所有字段
143
128
  * @param obj - 对象
144
129
  * @param callback - 回调函数
145
130
  */
@@ -237,10 +222,6 @@ export declare class UtilFunc {
237
222
  * @returns 是否安全
238
223
  */
239
224
  static isSafeNumber(num: unknown): boolean;
240
- /**将一个字段加入一个对象中,将改变对象,返回新类型 */
241
- static bindTo<K extends Keyable, V, B extends AnyRecord = {}>(key: Literal<K>, value: V, base?: B): keyof B extends K ? "Base中已有对应键" & Error : {
242
- [P in K | keyof B]: P extends K ? V : P extends keyof B ? B[P] : never;
243
- };
244
225
  /**移除多行字符串中每行开始的最小空格数。
245
226
  *
246
227
  * @param input - 需要处理的多行 字符串模板 或 字符串。
@@ -284,7 +265,7 @@ export declare class UtilFunc {
284
265
  * @param func - 待转换函数
285
266
  * @returns 转换完成的函数
286
267
  */
287
- static taskEitherize<T extends (...args: any) => Promise<any>>(func: T): (...args: Parameters<T>) => Promise<Outcome<typeof Failed, Error> | Outcome<typeof Success, Await<ReturnType<T>>>>;
268
+ static taskEitherize<T extends (...args: any) => Promise<any>>(func: T): (...args: Parameters<T>) => Promise<Outcome<typeof Failed, Error> | Outcome<typeof Success, Awaited<ReturnType<T>>>>;
288
269
  /**动态导入模块的函数。这个函数是为了在TypeScript的模块系统配置为CommonJS时,防止动态import被转译为require而设计的。
289
270
  * 使用这个函数,你可以在TypeScript中动态地导入模块,而不需要担心import()被转译为require()。
290
271
  * 请注意,这个函数使用了eval(),可能会带来安全风险。
@@ -286,86 +286,6 @@ class UtilFunc {
286
286
  reslove(result);
287
287
  });
288
288
  }
289
- /**部分类组合
290
- * 将mixin的部分字段混入base
291
- * @param base - 基础类
292
- * @param mixin - 目标类
293
- * @param key - mixin 对象在 base 中的 key
294
- * @param fields - 需要混入的字段
295
- * @returns 混合完成的类
296
- */
297
- static composeClassPart(base, mixin, key, ...fields) {
298
- const compObj = base;
299
- if (compObj[key] !== undefined)
300
- UtilLogger_1.SLogger.warn(`key: ${key} 已存在于基础类中, 混入可能导致类型问题`);
301
- compObj[key] = mixin;
302
- for (const fd of fields) {
303
- if (compObj[fd] !== undefined)
304
- UtilLogger_1.SLogger.warn(`field: ${fd} 已存在于基础类中, 混入可能导致类型问题, 如需覆盖, 请使用 ${key}=val`);
305
- //func需绑定this无法直接设置属性
306
- if (typeof mixin[fd] === 'function') {
307
- compObj[fd] = (...args) => compObj[key][fd](...args);
308
- //#region other
309
- //const memo = (...args: any[]) => compObj[key][fd](...args);
310
- //Object.defineProperty(compObj, fd, {
311
- // get:()=>memo,
312
- // set:()=>SLogger.warn(new Error(`试图修改一个由 composeClassPart 组合的函数字段:${String(fd)}, 修改已被忽略`)),
313
- // enumerable:true ,
314
- // //writable: true ,
315
- // configurable: true
316
- //});
317
- //(compObj as any)[fd] = (...args: any[]) => (mixin[fd] as any).apply(mixin, args);
318
- //(compObj as any)[fd] = (mixin[fd] as any).bind(mixin);
319
- //#endregion
320
- }
321
- else {
322
- Object.defineProperty(compObj, fd, {
323
- get: () => compObj[key][fd],
324
- set: (value) => { compObj[key][fd] = value; },
325
- enumerable: true,
326
- //writable: true ,
327
- configurable: true
328
- });
329
- }
330
- }
331
- return compObj;
332
- }
333
- /**根据 MIXIN_FIELDS 自动混入
334
- * @deprecated 非必要情况下, 对class定义请使用 composeRefMixinable
335
- */
336
- static composeMixinable(base, ...mixins) {
337
- let out = base;
338
- const fieldsSet = new Set();
339
- for (const rawmixin of mixins) {
340
- const mixin = rawmixin;
341
- for (const field of mixin.MIXIN_FIELDS) {
342
- const fixField = field;
343
- if (fieldsSet.has(fixField))
344
- UtilLogger_1.SLogger.warn(`composeMixinable 出现了重复的 field: ${fixField} 可能会导致问题`);
345
- else
346
- fieldsSet.add(fixField);
347
- }
348
- out = UtilFunc.composeClassPart(base, mixin, mixin.MIXIN_KEY, ...mixin.MIXIN_FIELDS);
349
- }
350
- return out;
351
- }
352
- /**根据 MIXIN_FIELDS 自动混入 */
353
- static composeRefMixinable(base, ...mixins) {
354
- let out = base;
355
- const fieldsSet = new Set();
356
- for (const mixin of mixins) {
357
- const ctor = mixin.CTOR;
358
- for (const field of ctor.MIXIN_FIELDS) {
359
- const fixField = field;
360
- if (fieldsSet.has(fixField))
361
- UtilLogger_1.SLogger.warn(`composeMixinable 出现了重复的 field: ${fixField} 可能会导致问题`);
362
- else
363
- fieldsSet.add(fixField);
364
- }
365
- out = UtilFunc.composeClassPart(base, mixin, ctor.MIXIN_KEY, ...ctor.MIXIN_FIELDS);
366
- }
367
- return out;
368
- }
369
289
  /**对对象的每个属性应用映射函数,并返回一个新的对象。
370
290
  * @template T - 对象的类型
371
291
  * @param obj - 要处理的对象
@@ -378,7 +298,7 @@ class UtilFunc {
378
298
  return result;
379
299
  }, {});
380
300
  }
381
- /** 遍历对象的所有字段
301
+ /**遍历对象的所有字段
382
302
  * @param obj - 对象
383
303
  * @param callback - 回调函数
384
304
  */
@@ -623,13 +543,6 @@ class UtilFunc {
623
543
  }
624
544
  return false;
625
545
  }
626
- /**将一个字段加入一个对象中,将改变对象,返回新类型 */
627
- static bindTo(key, value, base) {
628
- let out = base;
629
- out = out ?? {};
630
- out[key] = value;
631
- return out;
632
- }
633
546
  /**移除多行字符串中每行开始的最小空格数。
634
547
  *
635
548
  * @param input - 需要处理的多行 字符串模板 或 字符串。
@@ -101,63 +101,14 @@ export type ExclusiveJObject<B extends JObject, T extends JToken, K extends stri
101
101
  export type ReqStat = Success | Failed | Terminated;
102
102
  /**请求状态验证函数 */
103
103
  export type ReqVerifyFn<T> = (obj: T) => Promise<ReqStat> | ReqStat;
104
- /**获取Promise的结果类型 如同await那样 */
104
+ /**获取Promise的结果类型 如同await那样
105
+ * @deprecated 请使用官方的 Awaited<T> 类型
106
+ */
105
107
  export type Await<T> = T extends Promise<infer U> ? U : T;
106
108
  /**类型中任意函数的字符串名称 */
107
109
  export type FuncPropNames<T> = {
108
110
  [K in keyof T]: T[K] extends AnyFunc ? K : never;
109
111
  }[keyof T];
110
- /**部分组合的类
111
- * @template Base - 基类
112
- * @template Mixin - 待混入的类
113
- * @template PropKeys - 需要混入的成员key
114
- */
115
- export type ComposedClass<Base, Mixin, Key extends string, PropKeys extends keyof Mixin> = Base & Pick<Mixin, PropKeys> & {
116
- [P in Key]: Mixin;
117
- };
118
- /**可自动混入的类型 */
119
- export type Mixinable<Mixin> = {
120
- /**混入时自身所在位置
121
- * 请使用字面量
122
- */
123
- readonly MIXIN_KEY: string;
124
- /**可混入的字段
125
- * 请使用字面量
126
- */
127
- readonly MIXIN_FIELDS: readonly (keyof Mixin)[];
128
- };
129
- /**是原型构造器类型 */
130
- export type IsCtor<T> = T extends {
131
- new (): infer Ins;
132
- } ? T : never;
133
- /**可反射的
134
- * @template Ctor - 构造器类型
135
- * @template Constraint - 构造器约束
136
- */
137
- export type ReflectionAble<Ctor, Constraint = {}> = {
138
- /**原型构造器 */
139
- readonly CTOR: Ctor & Constraint;
140
- };
141
- /**可反射的 且 构造函数是可混入的类
142
- * @template Ctor - 构造器类型
143
- * @template Instance - 实例类型 如果采用了内部混入, 那么填入对外实际暴露的类型, 以支持混入多层字段
144
- * @template Constraint - 构造器约束
145
- * @example
146
- * export class MixinModule implements RefMixinable<typeof MixinModule,MixinModule>{
147
- * static readonly MIXIN_FIELDS = [
148
- * 'func_a',
149
- * 'field_b'
150
- * ] as const;
151
- * static readonly MIXIN_KEY = '__mixinModule';
152
- * CTOR = MixinModule;
153
- * }
154
- */
155
- export type RefMixinable<Ctor, Instance, Constraint = {}> = ReflectionAble<Ctor, Mixinable<Instance> & Constraint>;
156
- /**自动组合可混入的类
157
- */
158
- export type ComposedMixinable<B, Ms extends unknown[]> = Ms extends [infer M, ...infer Rest] ? M extends Mixinable<M> ? ComposedMixinable<ComposedClass<B, M, M['MIXIN_KEY'], M['MIXIN_FIELDS'][number]>, Rest> : "一个混入类没有实现 Mixinable<self>" & Error : B;
159
- /**自动组合 可反射的 且 构造函数是可混入的 类 */
160
- export type ComposedRefMixinable<B, Ms extends unknown[]> = Ms extends [infer M, ...infer Rest] ? M extends RefMixinable<unknown, M> ? ComposedRefMixinable<ComposedClass<B, M, M['CTOR']['MIXIN_KEY'], M['CTOR']['MIXIN_FIELDS'][number]>, Rest> : "一个混入类没有实现 RefMixinable<typeof self, self>" & Error : B;
161
112
  /** extends封装
162
113
  * @template B - 基础类型
163
114
  * @template T - 判断的目标类型
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.164",
3
+ "version": "1.0.166",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -19,6 +19,7 @@
19
19
  "@deepkit/type": "^1.0.1-alpha.153",
20
20
  "fluent-ffmpeg": "2.1.2",
21
21
  "glob": "^10.4.1",
22
+ "handlebars": "^4.7.8",
22
23
  "html-entities": "^2.3.3",
23
24
  "http-proxy-agent": "^5.0.0",
24
25
  "https-proxy-agent": "^5.0.1",
package/src/UtilClass.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { matchProc } from "./QuickFunction";
2
- import { Literal } from "./UtilInterfaces";
2
+ import { Keyable, Literal } from "./UtilInterfaces";
3
+ import Handlebars from 'handlebars';
4
+ import { SLogger } from "./UtilLogger";
3
5
 
4
6
  /**函数管道器 */
5
7
  export class Piper<Arg, Result = Arg> {
@@ -194,4 +196,49 @@ export class Stream<T> implements Iterable<T>{
194
196
  get length(): number {
195
197
  return this._list.length;
196
198
  }
197
- }
199
+ }
200
+
201
+ /**文本模板渲染器
202
+ * @template T - 上下文对象的类型,默认为记录类型
203
+ */
204
+ export class Hbs<T extends Record<Keyable,any> = Record<Keyable,any>>{
205
+ hbs = Handlebars.create();
206
+ context:T;
207
+
208
+ /**创建一个新的 Hbs 实例
209
+ * @param context - 上下文对象,用于在渲染模板时提供数据
210
+ */
211
+ constructor(context:T={} as T) {
212
+ this.context = context;
213
+
214
+ this.hbs.registerHelper('def', (name, options)=> {
215
+ const value = options.fn(this.context).trim();
216
+ (this.context as any)[name] = value;
217
+ return '';
218
+ });
219
+
220
+ this.hbs.registerHelper('defli', (name,options)=> {
221
+ const rawvalue = options.fn() as string;
222
+
223
+ const values = rawvalue
224
+ .split('[[br]]')
225
+ .map(value => value.trim())
226
+ .map(value => this.hbs.compile(value)(this.context));
227
+ (this.context as any)[name] = values;
228
+ return '';
229
+ });
230
+ }
231
+
232
+ /**渲染模板
233
+ * @param template - 要渲染的 Handlebars 模板字符串
234
+ * @returns 渲染后的字符串
235
+ */
236
+ render(template:string):string{
237
+ try{
238
+ return this.hbs.compile(template)(this.context);
239
+ } catch(e){
240
+ SLogger.error(e);
241
+ return '';
242
+ }
243
+ }
244
+ }
@@ -1,5 +1,5 @@
1
1
  import { UtilFunc } from "./UtilFunctions";
2
- import { Await, NeedInit } from "./UtilInterfaces";
2
+ import { NeedInit } from "./UtilInterfaces";
3
3
  import { SLogger } from "./UtilLogger";
4
4
 
5
5
  type TDTg<T> =
@@ -1,5 +1,5 @@
1
1
  import * as crypto from "crypto";
2
- import { PRecord, AnyFunc, ComposedClass, ComposedMixinable, ComposedRefMixinable, ExtractOutcome, IJData, JObject, JToken, Keyable, Literal, Matchable, MatchableFlag, Mixinable, Outcome, ReqStat, ReqVerifyFn, ProperSubsetCheck, RefMixinable, UnionToIntersection, Await, AnyRecord, AllExtends } from "@src/UtilInterfaces";
2
+ import { PRecord, AnyFunc, ExtractOutcome, IJData, JObject, JToken, Keyable, Literal, Matchable, MatchableFlag, Outcome, ReqStat, ReqVerifyFn, ProperSubsetCheck, UnionToIntersection, AnyRecord, AllExtends } from "@src/UtilInterfaces";
3
3
  import * as cp from "child_process";
4
4
  import { LogLevel, SLogger } from "@src/UtilLogger";
5
5
  import { Completed, Failed, FailedLike, None, StatusSymbol, Success, SuccessLike, Terminated, Timeout } from "./UtilSymbol";
@@ -347,99 +347,6 @@ static timelimitPromise<T>
347
347
  })
348
348
  }
349
349
 
350
-
351
-
352
- /**部分类组合
353
- * 将mixin的部分字段混入base
354
- * @param base - 基础类
355
- * @param mixin - 目标类
356
- * @param key - mixin 对象在 base 中的 key
357
- * @param fields - 需要混入的字段
358
- * @returns 混合完成的类
359
- */
360
- static composeClassPart
361
- <Base extends AnyRecord,Mixin extends AnyRecord,Field extends keyof Mixin>
362
- (base:Base,mixin:Mixin,key:string,...fields:Field[]):ComposedClass<Base,Mixin,typeof key,Field>{
363
- const compObj = base as any;
364
- if(compObj[key]!==undefined)
365
- SLogger.warn(`key: ${key as string} 已存在于基础类中, 混入可能导致类型问题`);
366
- compObj[key] = mixin;
367
- for(const fd of fields){
368
- if(compObj[fd]!==undefined)
369
- SLogger.warn(`field: ${fd as string} 已存在于基础类中, 混入可能导致类型问题, 如需覆盖, 请使用 ${key}=val`);
370
- //func需绑定this无法直接设置属性
371
- if(typeof mixin[fd] === 'function') {
372
- compObj[fd] = (...args: any[]) => compObj[key][fd](...args);
373
- //#region other
374
- //const memo = (...args: any[]) => compObj[key][fd](...args);
375
- //Object.defineProperty(compObj, fd, {
376
- // get:()=>memo,
377
- // set:()=>SLogger.warn(new Error(`试图修改一个由 composeClassPart 组合的函数字段:${String(fd)}, 修改已被忽略`)),
378
- // enumerable:true ,
379
- // //writable: true ,
380
- // configurable: true
381
- //});
382
- //(compObj as any)[fd] = (...args: any[]) => (mixin[fd] as any).apply(mixin, args);
383
- //(compObj as any)[fd] = (mixin[fd] as any).bind(mixin);
384
- //#endregion
385
- } else {
386
- Object.defineProperty(compObj, fd, {
387
- get: ()=>compObj[key][fd],
388
- set: (value)=>{compObj[key][fd] = value},
389
- enumerable:true ,
390
- //writable: true ,
391
- configurable: true
392
- });
393
- }
394
- }
395
- return compObj;
396
- }
397
-
398
- /**根据 MIXIN_FIELDS 自动混入
399
- * @deprecated 非必要情况下, 对class定义请使用 composeRefMixinable
400
- */
401
- static composeMixinable
402
- <Base extends AnyRecord, Mixins extends Mixinable<any>[]>
403
- (base:Base,...mixins:Mixins):
404
- ComposedMixinable<Base,Mixins>{
405
- let out = base;
406
- const fieldsSet = new Set<string>();
407
- for(const rawmixin of mixins){
408
- const mixin=rawmixin as any;
409
- for(const field of mixin.MIXIN_FIELDS) {
410
- const fixField = field as string;
411
- if(fieldsSet.has(fixField))
412
- SLogger.warn(`composeMixinable 出现了重复的 field: ${fixField} 可能会导致问题`);
413
- else
414
- fieldsSet.add(fixField);
415
- }
416
- out = UtilFunc.composeClassPart(base,mixin,mixin.MIXIN_KEY,...mixin.MIXIN_FIELDS);
417
- }
418
- return out as any;
419
- }
420
-
421
- /**根据 MIXIN_FIELDS 自动混入 */
422
- static composeRefMixinable
423
- <Base extends AnyRecord, Mixins extends RefMixinable<any,unknown>[]>
424
- (base:Base,...mixins:Mixins):
425
- ComposedRefMixinable<Base,Mixins>{
426
- let out = base;
427
- const fieldsSet = new Set<string>();
428
- for(const mixin of mixins){
429
- const ctor = mixin.CTOR;
430
- for(const field of ctor.MIXIN_FIELDS) {
431
- const fixField = field as string;
432
- if(fieldsSet.has(fixField))
433
- SLogger.warn(`composeMixinable 出现了重复的 field: ${fixField} 可能会导致问题`);
434
- else
435
- fieldsSet.add(fixField);
436
- }
437
- out = UtilFunc.composeClassPart(base,mixin,ctor.MIXIN_KEY,...ctor.MIXIN_FIELDS);
438
- }
439
- return out as any;
440
- }
441
-
442
-
443
350
  /**对对象的每个属性应用映射函数,并返回一个新的对象。
444
351
  * @template T - 对象的类型
445
352
  * @param obj - 要处理的对象
@@ -454,7 +361,7 @@ static mapEntries<T extends AnyRecord>
454
361
  }, {} as T);
455
362
  }
456
363
 
457
- /** 遍历对象的所有字段
364
+ /**遍历对象的所有字段
458
365
  * @param obj - 对象
459
366
  * @param callback - 回调函数
460
367
  */
@@ -744,16 +651,6 @@ static isSafeNumber(num: unknown): boolean {
744
651
  return false;
745
652
  }
746
653
 
747
- /**将一个字段加入一个对象中,将改变对象,返回新类型 */
748
- static bindTo<K extends Keyable, V, B extends AnyRecord = {}>
749
- (key: Literal<K>,value: V, base?:B):keyof B extends K
750
- ? "Base中已有对应键"&Error
751
- : {[P in K | keyof B]: P extends K ? V : P extends keyof B ? B[P] : never;}{
752
- let out = base as any;
753
- out = out??{};
754
- out[key]=value;
755
- return out;
756
- }
757
654
  /**移除多行字符串中每行开始的最小空格数。
758
655
  *
759
656
  * @param input - 需要处理的多行 字符串模板 或 字符串。
@@ -847,7 +744,7 @@ static eitherize<T extends AnyFunc>(func:T) {
847
744
  static taskEitherize<T extends (...args:any)=>Promise<any>>(func:T) {
848
745
  return async (...args: Parameters<T>) => {
849
746
  try {
850
- const result:Await<ReturnType<T>> = await func(...args as any);
747
+ const result:Awaited<ReturnType<T>> = await func(...args as any);
851
748
  return UtilFunc.outcome(Success,result);
852
749
  } catch (error) {
853
750
  if(error instanceof Error)
@@ -149,82 +149,17 @@ export type ReqStat = Success|Failed|Terminated;
149
149
  /**请求状态验证函数 */
150
150
  export type ReqVerifyFn<T> = (obj:T)=>Promise<ReqStat>|ReqStat;
151
151
 
152
- /**获取Promise的结果类型 如同await那样 */
152
+ /**获取Promise的结果类型 如同await那样
153
+ * @deprecated 请使用官方的 Awaited<T> 类型
154
+ */
153
155
  export type Await<T> = T extends Promise<infer U> ? U : T;
154
156
 
157
+
155
158
  /**类型中任意函数的字符串名称 */
156
159
  export type FuncPropNames<T> = {
157
160
  [K in keyof T]: T[K] extends AnyFunc ? K : never;
158
161
  }[keyof T];
159
162
 
160
- /**部分组合的类
161
- * @template Base - 基类
162
- * @template Mixin - 待混入的类
163
- * @template PropKeys - 需要混入的成员key
164
- */
165
- export type ComposedClass<Base, Mixin, Key extends string, PropKeys extends keyof Mixin> =
166
- Base & Pick<Mixin, PropKeys> & {[P in Key]:Mixin};
167
-
168
- /**可自动混入的类型 */
169
- export type Mixinable<Mixin> = {
170
- /**混入时自身所在位置
171
- * 请使用字面量
172
- */
173
- readonly MIXIN_KEY: string;
174
- /**可混入的字段
175
- * 请使用字面量
176
- */
177
- readonly MIXIN_FIELDS: readonly (keyof Mixin)[];
178
- };
179
- /**是原型构造器类型 */
180
- export type IsCtor<T> = T extends {new():infer Ins}
181
- ? T : never;
182
- /**可反射的
183
- * @template Ctor - 构造器类型
184
- * @template Constraint - 构造器约束
185
- */
186
- export type ReflectionAble<Ctor,Constraint={}> = {
187
- /**原型构造器 */
188
- readonly CTOR:Ctor&Constraint
189
- };
190
- /**可反射的 且 构造函数是可混入的类
191
- * @template Ctor - 构造器类型
192
- * @template Instance - 实例类型 如果采用了内部混入, 那么填入对外实际暴露的类型, 以支持混入多层字段
193
- * @template Constraint - 构造器约束
194
- * @example
195
- * export class MixinModule implements RefMixinable<typeof MixinModule,MixinModule>{
196
- * static readonly MIXIN_FIELDS = [
197
- * 'func_a',
198
- * 'field_b'
199
- * ] as const;
200
- * static readonly MIXIN_KEY = '__mixinModule';
201
- * CTOR = MixinModule;
202
- * }
203
- */
204
- export type RefMixinable<Ctor,Instance,Constraint={}> =
205
- ReflectionAble<Ctor,Mixinable<Instance>&Constraint>;
206
-
207
-
208
- /**自动组合可混入的类
209
- */
210
- export type ComposedMixinable<B, Ms extends unknown[]> =
211
- Ms extends [infer M, ...infer Rest]
212
- ? M extends Mixinable<M>
213
- ? ComposedMixinable<ComposedClass<B,M,M['MIXIN_KEY'],M['MIXIN_FIELDS'][number]>,Rest>
214
- : "一个混入类没有实现 Mixinable<self>" & Error
215
- : B
216
-
217
- /**自动组合 可反射的 且 构造函数是可混入的 类 */
218
- export type ComposedRefMixinable<B, Ms extends unknown[]> =
219
- Ms extends [infer M, ...infer Rest]
220
- ? M extends RefMixinable<unknown,M>
221
- ? ComposedRefMixinable<ComposedClass<B,M,
222
- M['CTOR']['MIXIN_KEY'],
223
- M['CTOR']['MIXIN_FIELDS'][number]>,
224
- Rest>
225
- : "一个混入类没有实现 RefMixinable<typeof self, self>" & Error
226
- : B
227
-
228
163
  /** extends封装
229
164
  * @template B - 基础类型
230
165
  * @template T - 判断的目标类型