@zwa73/utils 1.0.163 → 1.0.165

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 { 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, Await, 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"];
@@ -32,6 +32,12 @@ export type RepeatPromiseOpt = Partial<{
32
32
  }>;
33
33
  type SuccessOut<T> = Outcome<Success, T>;
34
34
  type TimeoutOut<T> = Outcome<Timeout, Promise<T>>;
35
+ /**遍历对象的回调函数
36
+ * @param key - 字段名
37
+ * @param value - 字段值
38
+ * @param parent - 父对象
39
+ */
40
+ type EachFieldCallback = (key: string, value: JToken, parent: JObject) => void;
35
41
  /**常用函数 */
36
42
  export declare class UtilFunc {
37
43
  /**获取当前时间戳
@@ -111,21 +117,6 @@ export declare class UtilFunc {
111
117
  * @returns 超时则返回 处理函数委托 完成则返回结果
112
118
  */
113
119
  static timelimitPromise<T>(func: () => Promise<T> | T, timeLimit?: number): Promise<SuccessOut<T> | TimeoutOut<T>>;
114
- /**部分类组合
115
- * 将mixin的部分字段混入base
116
- * @param base - 基础类
117
- * @param mixin - 目标类
118
- * @param key - mixin 对象在 base 中的 key
119
- * @param fields - 需要混入的字段
120
- * @returns 混合完成的类
121
- */
122
- 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>;
123
- /**根据 MIXIN_FIELDS 自动混入
124
- * @deprecated 非必要情况下, 对class定义请使用 composeRefMixinable
125
- */
126
- static composeMixinable<Base extends AnyRecord, Mixins extends Mixinable<any>[]>(base: Base, ...mixins: Mixins): ComposedMixinable<Base, Mixins>;
127
- /**根据 MIXIN_FIELDS 自动混入 */
128
- static composeRefMixinable<Base extends AnyRecord, Mixins extends RefMixinable<any, unknown>[]>(base: Base, ...mixins: Mixins): ComposedRefMixinable<Base, Mixins>;
129
120
  /**对对象的每个属性应用映射函数,并返回一个新的对象。
130
121
  * @template T - 对象的类型
131
122
  * @param obj - 要处理的对象
@@ -133,6 +124,11 @@ export declare class UtilFunc {
133
124
  * @returns - 一个新的对象,它的属性是原对象的属性经过映射函数处理后的结果
134
125
  */
135
126
  static mapEntries<T extends AnyRecord>(obj: T, mapper: (key: keyof T, value: T[keyof T]) => T[keyof T]): T;
127
+ /**遍历对象的所有字段
128
+ * @param obj - 对象
129
+ * @param callback - 回调函数
130
+ */
131
+ static eachField(obj: JToken, callback: EachFieldCallback): void;
136
132
  /**将JToken转换为字符串
137
133
  * @param token - 待转换的Token
138
134
  * @param opt - 可选参数
@@ -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,6 +298,29 @@ class UtilFunc {
378
298
  return result;
379
299
  }, {});
380
300
  }
301
+ /**遍历对象的所有字段
302
+ * @param obj - 对象
303
+ * @param callback - 回调函数
304
+ */
305
+ static eachField(obj, callback) {
306
+ if (obj == null)
307
+ return;
308
+ if (typeof obj === 'object' && 'toJSON' in obj && typeof obj.toJSON == 'function') {
309
+ UtilLogger_1.SLogger.warn('UtilFunc.eachField 错误 无法遍历IJData 已跳过');
310
+ return;
311
+ }
312
+ if (Array.isArray(obj)) {
313
+ for (const item of obj)
314
+ UtilFunc.eachField(item, callback);
315
+ return;
316
+ }
317
+ if (typeof obj === 'object') {
318
+ for (const [k, v] of Object.entries(obj)) {
319
+ UtilFunc.eachField(v, callback);
320
+ callback(k, v, obj);
321
+ }
322
+ }
323
+ }
381
324
  /**将JToken转换为字符串
382
325
  * @param token - 待转换的Token
383
326
  * @param opt - 可选参数
@@ -107,57 +107,6 @@ export type Await<T> = T extends Promise<infer U> ? U : T;
107
107
  export type FuncPropNames<T> = {
108
108
  [K in keyof T]: T[K] extends AnyFunc ? K : never;
109
109
  }[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
110
  /** extends封装
162
111
  * @template B - 基础类型
163
112
  * @template T - 判断的目标类型
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.163",
3
+ "version": "1.0.165",
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 { 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, Await, 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";
@@ -55,6 +55,14 @@ type PromiseResult<T> = {
55
55
  /**请求下标/序号 */
56
56
  index:number;
57
57
  };
58
+
59
+ /**遍历对象的回调函数
60
+ * @param key - 字段名
61
+ * @param value - 字段值
62
+ * @param parent - 父对象
63
+ */
64
+ type EachFieldCallback = (key: string, value: JToken,parent: JObject) => void;
65
+
58
66
  /**常用函数 */
59
67
  export class UtilFunc{
60
68
  /**获取当前时间戳
@@ -339,99 +347,6 @@ static timelimitPromise<T>
339
347
  })
340
348
  }
341
349
 
342
-
343
-
344
- /**部分类组合
345
- * 将mixin的部分字段混入base
346
- * @param base - 基础类
347
- * @param mixin - 目标类
348
- * @param key - mixin 对象在 base 中的 key
349
- * @param fields - 需要混入的字段
350
- * @returns 混合完成的类
351
- */
352
- static composeClassPart
353
- <Base extends AnyRecord,Mixin extends AnyRecord,Field extends keyof Mixin>
354
- (base:Base,mixin:Mixin,key:string,...fields:Field[]):ComposedClass<Base,Mixin,typeof key,Field>{
355
- const compObj = base as any;
356
- if(compObj[key]!==undefined)
357
- SLogger.warn(`key: ${key as string} 已存在于基础类中, 混入可能导致类型问题`);
358
- compObj[key] = mixin;
359
- for(const fd of fields){
360
- if(compObj[fd]!==undefined)
361
- SLogger.warn(`field: ${fd as string} 已存在于基础类中, 混入可能导致类型问题, 如需覆盖, 请使用 ${key}=val`);
362
- //func需绑定this无法直接设置属性
363
- if(typeof mixin[fd] === 'function') {
364
- compObj[fd] = (...args: any[]) => compObj[key][fd](...args);
365
- //#region other
366
- //const memo = (...args: any[]) => compObj[key][fd](...args);
367
- //Object.defineProperty(compObj, fd, {
368
- // get:()=>memo,
369
- // set:()=>SLogger.warn(new Error(`试图修改一个由 composeClassPart 组合的函数字段:${String(fd)}, 修改已被忽略`)),
370
- // enumerable:true ,
371
- // //writable: true ,
372
- // configurable: true
373
- //});
374
- //(compObj as any)[fd] = (...args: any[]) => (mixin[fd] as any).apply(mixin, args);
375
- //(compObj as any)[fd] = (mixin[fd] as any).bind(mixin);
376
- //#endregion
377
- } else {
378
- Object.defineProperty(compObj, fd, {
379
- get: ()=>compObj[key][fd],
380
- set: (value)=>{compObj[key][fd] = value},
381
- enumerable:true ,
382
- //writable: true ,
383
- configurable: true
384
- });
385
- }
386
- }
387
- return compObj;
388
- }
389
-
390
- /**根据 MIXIN_FIELDS 自动混入
391
- * @deprecated 非必要情况下, 对class定义请使用 composeRefMixinable
392
- */
393
- static composeMixinable
394
- <Base extends AnyRecord, Mixins extends Mixinable<any>[]>
395
- (base:Base,...mixins:Mixins):
396
- ComposedMixinable<Base,Mixins>{
397
- let out = base;
398
- const fieldsSet = new Set<string>();
399
- for(const rawmixin of mixins){
400
- const mixin=rawmixin as any;
401
- for(const field of mixin.MIXIN_FIELDS) {
402
- const fixField = field as string;
403
- if(fieldsSet.has(fixField))
404
- SLogger.warn(`composeMixinable 出现了重复的 field: ${fixField} 可能会导致问题`);
405
- else
406
- fieldsSet.add(fixField);
407
- }
408
- out = UtilFunc.composeClassPart(base,mixin,mixin.MIXIN_KEY,...mixin.MIXIN_FIELDS);
409
- }
410
- return out as any;
411
- }
412
-
413
- /**根据 MIXIN_FIELDS 自动混入 */
414
- static composeRefMixinable
415
- <Base extends AnyRecord, Mixins extends RefMixinable<any,unknown>[]>
416
- (base:Base,...mixins:Mixins):
417
- ComposedRefMixinable<Base,Mixins>{
418
- let out = base;
419
- const fieldsSet = new Set<string>();
420
- for(const mixin of mixins){
421
- const ctor = mixin.CTOR;
422
- for(const field of ctor.MIXIN_FIELDS) {
423
- const fixField = field as string;
424
- if(fieldsSet.has(fixField))
425
- SLogger.warn(`composeMixinable 出现了重复的 field: ${fixField} 可能会导致问题`);
426
- else
427
- fieldsSet.add(fixField);
428
- }
429
- out = UtilFunc.composeClassPart(base,mixin,ctor.MIXIN_KEY,...ctor.MIXIN_FIELDS);
430
- }
431
- return out as any;
432
- }
433
-
434
-
435
350
  /**对对象的每个属性应用映射函数,并返回一个新的对象。
436
351
  * @template T - 对象的类型
437
352
  * @param obj - 要处理的对象
@@ -446,6 +361,29 @@ static mapEntries<T extends AnyRecord>
446
361
  }, {} as T);
447
362
  }
448
363
 
364
+ /**遍历对象的所有字段
365
+ * @param obj - 对象
366
+ * @param callback - 回调函数
367
+ */
368
+ static eachField(obj: JToken, callback: EachFieldCallback): void {
369
+ if(obj==null) return;
370
+ if(typeof obj === 'object' && 'toJSON' in obj && typeof obj.toJSON == 'function'){
371
+ SLogger.warn('UtilFunc.eachField 错误 无法遍历IJData 已跳过');
372
+ return;
373
+ }
374
+ if(Array.isArray(obj)){
375
+ for (const item of obj)
376
+ UtilFunc.eachField(item, callback);
377
+ return;
378
+ }
379
+ if (typeof obj === 'object') {
380
+ for (const [k, v] of Object.entries(obj)) {
381
+ UtilFunc.eachField(v, callback);
382
+ callback(k, v, obj as JObject);
383
+ }
384
+ }
385
+ }
386
+
449
387
  /**将JToken转换为字符串
450
388
  * @param token - 待转换的Token
451
389
  * @param opt - 可选参数
@@ -157,74 +157,6 @@ export type FuncPropNames<T> = {
157
157
  [K in keyof T]: T[K] extends AnyFunc ? K : never;
158
158
  }[keyof T];
159
159
 
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
160
  /** extends封装
229
161
  * @template B - 基础类型
230
162
  * @template T - 判断的目标类型