@zwa73/utils 1.0.47 → 1.0.49

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.
@@ -500,4 +500,40 @@ export declare class SHashMap<K, V> {
500
500
  };
501
501
  };
502
502
  }
503
+ /**函数组合器 */
504
+ export declare class Composer<Result, PreArg = Result> {
505
+ /**组合函数列表 */
506
+ private funcs;
507
+ private constructor();
508
+ /**添加单个参数与返回值不同的函数 */
509
+ static push<Result, Arg>(func: (arg: Arg) => Result): Composer<Result, Arg>;
510
+ /**添加多个参数与返回值相同的函数 */
511
+ static push<Result>(func: ((arg: Result) => Result), ...funcs: ((arg: Result) => Result)[]): Composer<Result>;
512
+ /**添加单个参数与返回值不同的函数 */
513
+ push<T>(func: (arg: T) => PreArg): Composer<Result, T>;
514
+ /**添加多个参数与返回值相同的函数 */
515
+ push(func: ((arg: Result) => Result), ...funcs: ((arg: PreArg) => PreArg)[]): Composer<Result, PreArg>;
516
+ /**组合函数 */
517
+ compose(): (arg: PreArg) => Result;
518
+ /**直接调用 */
519
+ invoke(arg: PreArg): Result;
520
+ }
521
+ /**函数管道器 */
522
+ export declare class Piper<Arg, Result = Arg> {
523
+ /**管道函数列表 */
524
+ private funcs;
525
+ private constructor();
526
+ /**添加单个参数与返回值不同的函数 */
527
+ static pipe<Arg, Result>(func: (arg: Arg) => Result): Piper<Arg, Result>;
528
+ /**添加多个参数与返回值相同的函数 */
529
+ static pipe<Arg>(func: ((arg: Arg) => Arg), ...funcs: ((arg: Arg) => Arg)[]): Piper<Arg>;
530
+ /**添加单个参数与返回值不同的函数 */
531
+ pipe<T>(func: (arg: Result) => T): Piper<Arg, T>;
532
+ /**添加多个参数与返回值相同的函数 */
533
+ pipe(func: ((arg: Result) => Result), ...funcs: ((arg: Result) => Result)[]): Piper<Arg, Result>;
534
+ /**管道函数 */
535
+ pipeline(): (arg: Arg) => Result;
536
+ /**直接调用 */
537
+ invoke(arg: Arg): Result;
538
+ }
503
539
  export {};
package/dist/UtilClass.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SHashMap = exports.SEntry = exports.SIterator = exports.SStream = exports.SList = void 0;
3
+ exports.Piper = exports.Composer = exports.SHashMap = exports.SEntry = exports.SIterator = exports.SStream = exports.SList = void 0;
4
4
  class SList {
5
5
  _arr;
6
6
  constructor(obj) {
@@ -840,3 +840,55 @@ class SKVC {
840
840
  stringMap = new SHashMap();
841
841
  constructor() { }
842
842
  }
843
+ /**函数组合器 */
844
+ class Composer {
845
+ /**组合函数列表 */
846
+ funcs = [];
847
+ constructor() { }
848
+ ;
849
+ static push(...funcs) {
850
+ const newComposer = new Composer();
851
+ newComposer.funcs = [...funcs];
852
+ return newComposer;
853
+ }
854
+ push(...funcs) {
855
+ const newComposer = new Composer();
856
+ newComposer.funcs = [...this.funcs, ...funcs];
857
+ return newComposer;
858
+ }
859
+ /**组合函数 */
860
+ compose() {
861
+ return (arg) => this.funcs.reduceRight((value, func) => func(value), arg);
862
+ }
863
+ /**直接调用 */
864
+ invoke(arg) {
865
+ return this.funcs.reduceRight((value, func) => func(value), arg);
866
+ }
867
+ }
868
+ exports.Composer = Composer;
869
+ /**函数管道器 */
870
+ class Piper {
871
+ /**管道函数列表 */
872
+ funcs = [];
873
+ constructor() { }
874
+ ;
875
+ static pipe(...funcs) {
876
+ const newPiper = new Piper();
877
+ newPiper.funcs = [...funcs];
878
+ return newPiper;
879
+ }
880
+ pipe(...funcs) {
881
+ const newPiper = new Piper();
882
+ newPiper.funcs = [...this.funcs, ...funcs];
883
+ return newPiper;
884
+ }
885
+ /**管道函数 */
886
+ pipeline() {
887
+ return (arg) => this.funcs.reduce((value, func) => func(value), arg);
888
+ }
889
+ /**直接调用 */
890
+ invoke(arg) {
891
+ return this.funcs.reduce((value, func) => func(value), arg);
892
+ }
893
+ }
894
+ exports.Piper = Piper;
package/dist/UtilCom.js CHANGED
@@ -22,7 +22,7 @@ var UtilCom;
22
22
  if (hasTimeLimit)
23
23
  timeLimit *= 1000;
24
24
  const jsonStr = (0, UtilInterfaces_1.stringifyJToken)(json);
25
- const funcName = "s" + posttype + "Psot";
25
+ const funcName = `s${posttype}Psot`;
26
26
  return new Promise((resolve, rejecte) => {
27
27
  const resFunc = (res) => {
28
28
  try {
@@ -30,7 +30,7 @@ var UtilCom;
30
30
  if (hasTimeLimit) {
31
31
  res.setTimeout(timeLimit, () => {
32
32
  //res.abort();
33
- UtilLogger_1.SLogger.warn(funcName + " 接收反馈超时: " + timeLimit + " ms");
33
+ UtilLogger_1.SLogger.warn(`${funcName} 接收反馈超时: ${timeLimit} ms`);
34
34
  resolve(null);
35
35
  return;
36
36
  });
@@ -39,7 +39,7 @@ var UtilCom;
39
39
  res.setEncoding('utf8');
40
40
  res.on('data', (chunk) => resdata += chunk);
41
41
  res.on('error', (e) => {
42
- UtilLogger_1.SLogger.warn(funcName + " 接收反馈错误:" + e);
42
+ UtilLogger_1.SLogger.warn(`${funcName} 接收反馈错误:${e}`);
43
43
  resolve(null);
44
44
  return;
45
45
  });
@@ -56,7 +56,7 @@ var UtilCom;
56
56
  return;
57
57
  }
58
58
  catch (e) {
59
- UtilLogger_1.SLogger.warn(funcName + " 接收反馈错误:" + e + "\n原始字符串:" + resdata);
59
+ UtilLogger_1.SLogger.warn(`${funcName} 接收反馈错误:${e}\n原始字符串:${resdata}`);
60
60
  resolve(null);
61
61
  return;
62
62
  }
@@ -77,12 +77,12 @@ var UtilCom;
77
77
  //请求超时
78
78
  if (hasTimeLimit) {
79
79
  req.setTimeout(timeLimit, () => {
80
- UtilLogger_1.SLogger.warn(funcName + " 发送请求超时: " + timeLimit + " ms");
80
+ UtilLogger_1.SLogger.warn(`${funcName} 发送请求超时: ${timeLimit} ms`);
81
81
  req.destroy();
82
82
  });
83
83
  }
84
84
  req.on('error', (e) => {
85
- UtilLogger_1.SLogger.warn(funcName + " 发送请求错误:" + e);
85
+ UtilLogger_1.SLogger.warn(`${funcName} 发送请求错误:${e}`);
86
86
  resolve(null);
87
87
  });
88
88
  req.write(jsonStr);
@@ -1,16 +1,12 @@
1
- /**用于打印方法的调用
2
- * @returns {void}
3
- */
1
+ /**用于打印方法的调用 */
4
2
  export declare function DLogger(): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
5
- /**用于打印异步方法的调用
6
- * @returns {void}
7
- */
3
+ /**用于打印异步方法的调用 */
8
4
  export declare function DLoggerAsync(): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
9
- /**用于捕获方法中的错误
10
- * @returns {void}
11
- */
5
+ /**try-finally包装 */
6
+ export declare function Defer(deferLogic: () => void): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
7
+ /**异步的try-finally包装 */
8
+ export declare function DeferAsync(deferLogic: () => void): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
9
+ /**用于捕获方法中的错误 */
12
10
  export declare function DCatchErrors(): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
13
- /**用于捕获异步方法中的错误
14
- * @returns {void}
15
- */
11
+ /**用于捕获异步方法中的错误 */
16
12
  export declare function DCatchErrorsAsync(): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
@@ -1,10 +1,17 @@
1
1
  "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DCatchErrorsAsync = exports.DCatchErrors = exports.DLoggerAsync = exports.DLogger = void 0;
12
+ exports.DCatchErrorsAsync = exports.DCatchErrors = exports.DeferAsync = exports.Defer = exports.DLoggerAsync = exports.DLogger = void 0;
4
13
  const UtilLogger_1 = require("./UtilLogger");
5
- /**用于打印方法的调用
6
- * @returns {void}
7
- */
14
+ /**用于打印方法的调用 */
8
15
  function DLogger() {
9
16
  return function (target, propertyKey, descriptor) {
10
17
  const originalMethod = descriptor.value;
@@ -16,9 +23,7 @@ function DLogger() {
16
23
  };
17
24
  }
18
25
  exports.DLogger = DLogger;
19
- /**用于打印异步方法的调用
20
- * @returns {void}
21
- */
26
+ /**用于打印异步方法的调用 */
22
27
  function DLoggerAsync() {
23
28
  return function (target, propertyKey, descriptor) {
24
29
  const originalMethod = descriptor.value;
@@ -30,9 +35,45 @@ function DLoggerAsync() {
30
35
  };
31
36
  }
32
37
  exports.DLoggerAsync = DLoggerAsync;
33
- /**用于捕获方法中的错误
34
- * @returns {void}
35
- */
38
+ /**try-finally包装 */
39
+ function Defer(deferLogic) {
40
+ return function (target, propertyKey, descriptor) {
41
+ const originalMethod = descriptor.value;
42
+ descriptor.value = function (...args) {
43
+ try {
44
+ const result = originalMethod.apply(this, args);
45
+ deferLogic();
46
+ return result;
47
+ }
48
+ catch (e) {
49
+ deferLogic();
50
+ throw e;
51
+ }
52
+ };
53
+ return descriptor;
54
+ };
55
+ }
56
+ exports.Defer = Defer;
57
+ /**异步的try-finally包装 */
58
+ function DeferAsync(deferLogic) {
59
+ return function (target, propertyKey, descriptor) {
60
+ const originalMethod = descriptor.value;
61
+ descriptor.value = async function (...args) {
62
+ try {
63
+ const result = await originalMethod.apply(this, args);
64
+ deferLogic();
65
+ return result;
66
+ }
67
+ catch (e) {
68
+ deferLogic();
69
+ throw e;
70
+ }
71
+ };
72
+ return descriptor;
73
+ };
74
+ }
75
+ exports.DeferAsync = DeferAsync;
76
+ /**用于捕获方法中的错误 */
36
77
  function DCatchErrors() {
37
78
  return function (target, propertyKey, descriptor) {
38
79
  const originalMethod = descriptor.value;
@@ -47,9 +88,7 @@ function DCatchErrors() {
47
88
  };
48
89
  }
49
90
  exports.DCatchErrors = DCatchErrors;
50
- /**用于捕获异步方法中的错误
51
- * @returns {void}
52
- */
91
+ /**用于捕获异步方法中的错误 */
53
92
  function DCatchErrorsAsync() {
54
93
  return function (target, propertyKey, descriptor) {
55
94
  const originalMethod = descriptor.value;
@@ -64,3 +103,26 @@ function DCatchErrorsAsync() {
64
103
  };
65
104
  }
66
105
  exports.DCatchErrorsAsync = DCatchErrorsAsync;
106
+ function AddNumberDecorator(n) {
107
+ return function (target, propertyKey, descriptor) {
108
+ const originalMethod = descriptor.value;
109
+ descriptor.value = function (num) {
110
+ const result = originalMethod.apply(this, [num]);
111
+ return result + n;
112
+ };
113
+ return descriptor;
114
+ };
115
+ }
116
+ class Example {
117
+ myMethod(num) {
118
+ return num;
119
+ }
120
+ }
121
+ __decorate([
122
+ AddNumberDecorator(10),
123
+ __metadata("design:type", Function),
124
+ __metadata("design:paramtypes", [Number]),
125
+ __metadata("design:returntype", Number)
126
+ ], Example.prototype, "myMethod", null);
127
+ //let a = new Example();
128
+ //a.myMethod(10);//?
@@ -76,7 +76,17 @@ export declare namespace UtilFT {
76
76
  /**搜索路径符合正则表达式的文件
77
77
  * @param folder - 文件夹路径
78
78
  * @param traitRegex - 正则表达式
79
- * @returns {Record<string, string>} 文件名与路径的映射 Record<fileName,filePath>
79
+ * @returns 文件名路径数组
80
80
  */
81
- function fileSearch(folder: string, traitRegex: string): Record<string, string>;
81
+ function fileSearchRegex(folder: string, traitRegex: string): string[];
82
+ /**搜索符合Glob匹配的文件
83
+ * @param globPattern - glob匹配
84
+ * @param ignore - 忽略的文件
85
+ * @returns 文件绝对路径数组
86
+ */
87
+ function fileSearchGlob(globPattern: string | string[], ignore?: string | string[]): string[];
88
+ /**
89
+ * @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
90
+ */
91
+ function fileSearch(...patams: any[]): void;
82
92
  }
@@ -6,6 +6,7 @@ const path = require("path");
6
6
  const UtilInterfaces_1 = require("./UtilInterfaces");
7
7
  const UtilLogger_1 = require("./UtilLogger");
8
8
  const JSON5 = require("json5");
9
+ const glob_1 = require("glob");
9
10
  /**文件工具 */
10
11
  var UtilFT;
11
12
  (function (UtilFT) {
@@ -168,27 +169,40 @@ var UtilFT;
168
169
  /**搜索路径符合正则表达式的文件
169
170
  * @param folder - 文件夹路径
170
171
  * @param traitRegex - 正则表达式
171
- * @returns {Record<string, string>} 文件名与路径的映射 Record<fileName,filePath>
172
+ * @returns 文件名路径数组
172
173
  */
173
- function fileSearch(folder, traitRegex) {
174
- let outMap = {};
174
+ function fileSearchRegex(folder, traitRegex) {
175
+ let outArray = [];
175
176
  let subFiles = fs.readdirSync(folder);
176
177
  let regex = new RegExp(traitRegex);
177
178
  for (let subFile of subFiles) {
178
179
  let subFilePath = path.join(folder, subFile);
179
- subFilePath = subFilePath.replace(/\\/g, "/");
180
180
  let stat = fs.lstatSync(subFilePath);
181
181
  //判断是否是文件夹,递归调用
182
182
  if (stat.isDirectory()) {
183
- let subMap = fileSearch(path.join(subFilePath, path.sep), traitRegex);
184
- for (let key in subMap)
185
- outMap[key] = subMap[key];
183
+ outArray.push(...fileSearchRegex(path.join(subFilePath, path.sep), traitRegex));
186
184
  continue;
187
185
  }
188
186
  if (regex.test(subFilePath))
189
- outMap[subFile] = subFilePath;
187
+ outArray.push(subFilePath);
190
188
  }
191
- return outMap;
189
+ return outArray;
190
+ }
191
+ UtilFT.fileSearchRegex = fileSearchRegex;
192
+ /**搜索符合Glob匹配的文件
193
+ * @param globPattern - glob匹配
194
+ * @param ignore - 忽略的文件
195
+ * @returns 文件绝对路径数组
196
+ */
197
+ function fileSearchGlob(globPattern, ignore) {
198
+ return (0, glob_1.globSync)(globPattern, { ignore, absolute: true });
199
+ }
200
+ UtilFT.fileSearchGlob = fileSearchGlob;
201
+ /**
202
+ * @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
203
+ */
204
+ function fileSearch(...patams) {
205
+ throw "请使用 fileSearchRegex 或 fileSearchGlob";
192
206
  }
193
207
  UtilFT.fileSearch = fileSearch;
194
208
  })(UtilFT = exports.UtilFT || (exports.UtilFT = {}));
@@ -1,11 +1,11 @@
1
- import { JToken, PromiseProcFn, PromiseVerifyFn } from "./UtilInterfaces";
1
+ import { ComposedClassMult, ComposedPartClass as ComposedPartClass, JToken, PromiseProcFn, PromiseVerifyFn } from "./UtilInterfaces";
2
2
  /**常用函数 */
3
3
  export declare namespace UtilFunc {
4
4
  /**获取当前时间戳
5
5
  * number ()
6
6
  * @returns {number} 时间戳
7
7
  */
8
- function getTime(): number;
8
+ export function getTime(): number;
9
9
  /**初始化对象的字段
10
10
  * void (Object,string,any)
11
11
  * @param {Record<string,T>} obj - 所要初始化的对象
@@ -13,51 +13,111 @@ export declare namespace UtilFunc {
13
13
  * @param {T} defaultVal - 默认值
14
14
  * @returns {T} - 最终值
15
15
  */
16
- function initField<T>(obj: Record<string, T>, field: string, defaultVal: T): T;
16
+ export function initField<T>(obj: Record<string, T>, field: string, defaultVal: T): T;
17
17
  /**生成一串uuid
18
18
  * string ()
19
19
  * @returns {string} uuid
20
20
  */
21
- function genUUID(): string;
21
+ export function genUUID(): string;
22
22
  /**计算Hash
23
23
  * string ()
24
24
  * @param {string} str - 待计算的字符串
25
25
  * @returns {string} hash
26
26
  */
27
- function calcHash(str: string): string;
27
+ export function calcHash(str: string): string;
28
28
  /**深克隆 序列化并反序列化
29
29
  * @template {T} T - JToken类型的泛型
30
30
  * @param {T} obj - 克隆目标
31
31
  * @returns {T} 克隆结果
32
32
  */
33
- function deepClone<T extends JToken>(obj: T): T;
33
+ export function deepClone<T extends JToken>(obj: T): T;
34
34
  /**是否为安全的数字
35
35
  * @param {number} num - 所要检测的数字
36
36
  * @returns {boolean} 是否安全
37
37
  */
38
- function isSafeNumber(num: number): boolean;
38
+ export function isSafeNumber(num: number): boolean;
39
39
  /**等待 timeMs 毫秒
40
40
  * @async
41
41
  * @param {number} timeMs - 等待的毫秒数
42
42
  * @returns {Promise<boolean>}
43
43
  */
44
- function sleep(timeMs: number): Promise<boolean>;
44
+ export function sleep(timeMs: number): Promise<boolean>;
45
45
  /**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
46
46
  * @param {string} command 指令文本
47
47
  */
48
- function exec(command: string): Promise<{
48
+ export function exec(command: string): Promise<{
49
49
  stdout: string;
50
50
  stderr: string;
51
51
  }>;
52
52
  /**获得一个永不完成的Promise单例 */
53
- function getNeverResolvedPromise<T>(): Promise<T>;
53
+ export function getNeverResolvedPromise<T>(): Promise<T>;
54
54
  /**重复尝试promise
55
55
  * @async
56
56
  * @param {PromiseProcFn<T>} [procFn] - 发起函数
57
57
  * @param {PromiseVerifyFn<T>} [verifyFn] - 验证函数
58
- * @param {number} [repeatCount] - 重试次数
59
- * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
60
- * @returns {Promise<T|null>} - 结果 null 为全部失败/超时
58
+ * @param {number} [repeatCount] - 重试次数
59
+ * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
60
+ * @returns {Promise<T|null>} - 结果 null 为全部失败/超时
61
61
  */
62
- function repeatPromise<T>(procFn: PromiseProcFn<T>, verifyFn?: PromiseVerifyFn<T>, repeatCount?: number, repeatTime?: number): Promise<T | null>;
62
+ export function repeatPromise<T>(procFn: PromiseProcFn<T>, verifyFn?: PromiseVerifyFn<T>, repeatCount?: number, repeatTime?: number): Promise<T | null>;
63
+ /**柯里化函数类型 */
64
+ type CurryFunc<T, PrevArgs extends any[] = []> = T extends (...args: infer Args) => infer Result ? Args extends [infer Arg, ...infer RestArgs] ? RestArgs extends [] ? ((...args: [...PrevArgs, Arg]) => Result) : ((...args: [...PrevArgs, Arg]) => CurryFunc<(...rest: RestArgs) => Result>) & (CurryFunc<(...args: RestArgs) => Result, [...PrevArgs, Arg]>) : Args extends [] ? () => Result : never : never;
65
+ /**柯里化转换
66
+ * @param {T} fn - 将要转换的函数
67
+ * @returns {CurryFunc<T>} 柯里化的函数
68
+ */
69
+ export function curry<T extends (...args: any[]) => any>(fn: T): CurryFunc<T>;
70
+ /**
71
+ let sumvoid = ()=>10;
72
+ let a = curry(sumvoid);//?
73
+ console.log(a());
74
+ let sum = (a:number,b:string,c:number,d:boolean)=>a+b+c+d;
75
+ let sumCu = curry(sum);//?
76
+ let suma = sumCu(1)("ss");//?
77
+ let sumb = sumCu(1,"1")(2);//?
78
+ let sumc = sumCu(4);//?
79
+ let sumz = sumCu(1,"b",3)(false);//?
80
+ console.log(suma(2,true));
81
+ console.log(sumb(true));
82
+ console.log(sumc("s",3,false));
83
+ */
84
+ /**可组合的函数 */
85
+ type CompFunc<T = any, R = any> = (arg: T) => R;
86
+ /**函数组合 从右到左执行 */
87
+ export function compose<T>(...fs: CompFunc<T, T>[]): CompFunc<T, T>;
88
+ export function compose<T, R>(f1: CompFunc<T, R>): CompFunc<T, R>;
89
+ export function compose<T, R, R1>(f2: CompFunc<R, R1>, f1: CompFunc<T, R>): CompFunc<T, R1>;
90
+ export function compose<T, R, R1, R2>(f3: CompFunc<R1, R2>, f2: CompFunc<R, R1>, f1: CompFunc<T, R>): CompFunc<T, R2>;
91
+ export function compose<T, R, R1, R2, R3>(f4: CompFunc<R2, R3>, f3: CompFunc<R1, R2>, f2: CompFunc<R, R1>, f1: CompFunc<T, R>): CompFunc<T, R3>;
92
+ export function compose<T, R, R1, R2, R3, R4>(f5: CompFunc<R3, R4>, f4: CompFunc<R2, R3>, f3: CompFunc<R1, R2>, f2: CompFunc<R, R1>, f1: CompFunc<T, R>): CompFunc<T, R4>;
93
+ /**函数管道 从左到右执行 */
94
+ export function pipeline<T>(...fs: CompFunc<T, T>[]): CompFunc<T, T>;
95
+ export function pipeline<T, R>(f1: CompFunc<T, R>): CompFunc<T, R>;
96
+ export function pipeline<T, R, R1>(f1: CompFunc<T, R>, f2: CompFunc<R, R1>): CompFunc<T, R1>;
97
+ export function pipeline<T, R, R1, R2>(f1: CompFunc<T, R>, f2: CompFunc<R, R1>, f3: CompFunc<R1, R2>): CompFunc<T, R2>;
98
+ export function pipeline<T, R, R1, R2, R3>(f1: CompFunc<T, R>, f2: CompFunc<R, R1>, f3: CompFunc<R1, R2>, f4: CompFunc<R2, R3>): CompFunc<T, R3>;
99
+ export function pipeline<T, R, R1, R2, R3, R4>(f1: CompFunc<T, R>, f2: CompFunc<R, R1>, f3: CompFunc<R1, R2>, f4: CompFunc<R2, R3>, f5: CompFunc<R3, R4>): CompFunc<T, R4>;
100
+ /**部分类组合
101
+ * 将mixin的部分字段混入base
102
+ * @param base - 基础类
103
+ * @param mixin - 目标类
104
+ * @param fields - 需要混入的字段
105
+ * @returns - 混合完成的类
106
+ */
107
+ export function composePartClass<Base extends object, Mixin extends object>(base: Base, mixin: Mixin, ...fields: (keyof Mixin)[]): ComposedPartClass<Base, Mixin, keyof Mixin>;
108
+ /**类组合
109
+ * 将mixinList每个成员的字段混入base
110
+ * @param base - 基础类
111
+ * @param mixinList - 目标类
112
+ * @returns - 混合完成的类
113
+ */
114
+ export function composeClass<Base extends object, MixinList extends object[]>(base: Base, ...mixinList: MixinList): ComposedClassMult<Base, MixinList>;
115
+ /**对对象的每个属性应用映射函数,并返回一个新的对象。
116
+ * @template T - 对象的类型
117
+ * @param obj - 要处理的对象
118
+ * @param mapper - 映射函数,接受一个值和一个键,返回一个新的值
119
+ * @returns - 一个新的对象,它的属性是原对象的属性经过映射函数处理后的结果
120
+ */
121
+ export function mapObject<T extends Object>(obj: T, mapper: (key: keyof T, value: T[keyof T]) => T[keyof T]): T;
122
+ export {};
63
123
  }
@@ -93,6 +93,7 @@ var UtilFunc;
93
93
  });
94
94
  }
95
95
  UtilFunc.exec = exec;
96
+ /**永不完成的Promise单例 */
96
97
  const NeverResolvedPromise = new Promise(() => { });
97
98
  /**获得一个永不完成的Promise单例 */
98
99
  function getNeverResolvedPromise() {
@@ -103,9 +104,9 @@ var UtilFunc;
103
104
  * @async
104
105
  * @param {PromiseProcFn<T>} [procFn] - 发起函数
105
106
  * @param {PromiseVerifyFn<T>} [verifyFn] - 验证函数
106
- * @param {number} [repeatCount] - 重试次数
107
- * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
108
- * @returns {Promise<T|null>} - 结果 null 为全部失败/超时
107
+ * @param {number} [repeatCount] - 重试次数
108
+ * @param {number} [repeatTime] - 超时时间/秒 最小为10秒
109
+ * @returns {Promise<T|null>} - 结果 null 为全部失败/超时
109
110
  */
110
111
  async function repeatPromise(procFn, verifyFn, repeatCount = 3, repeatTime = 180) {
111
112
  //计时
@@ -199,4 +200,107 @@ var UtilFunc;
199
200
  }
200
201
  }
201
202
  UtilFunc.repeatPromise = repeatPromise;
203
+ /**柯里化转换
204
+ * @param {T} fn - 将要转换的函数
205
+ * @returns {CurryFunc<T>} 柯里化的函数
206
+ */
207
+ function curry(fn) {
208
+ return (function curried(...args) {
209
+ if (args.length >= fn.length)
210
+ return fn(...args);
211
+ return (...restArgs) => curried(...args, ...restArgs);
212
+ });
213
+ }
214
+ UtilFunc.curry = curry;
215
+ /**函数组合
216
+ * 从右到左执行
217
+ * @param {Function[]} funcs - 待组合的函数
218
+ * @returns {(arg: any)=>any} 组合完成的函数
219
+ */
220
+ function compose(...funcs) {
221
+ return (arg) => funcs.reduceRight((value, func) => func(value), arg);
222
+ }
223
+ UtilFunc.compose = compose;
224
+ /**函数管道
225
+ * 从左到右执行
226
+ * @param {((arg: T) => T)} funcs - 待组合的函数
227
+ * @returns {((arg: T) => T)} 组合完成的函数
228
+ */
229
+ function pipeline(...funcs) {
230
+ return (arg) => {
231
+ return funcs.reduce((value, func) => func(value), arg);
232
+ };
233
+ }
234
+ UtilFunc.pipeline = pipeline;
235
+ /**部分类组合
236
+ * 将mixin的部分字段混入base
237
+ * @param base - 基础类
238
+ * @param mixin - 目标类
239
+ * @param fields - 需要混入的字段
240
+ * @returns - 混合完成的类
241
+ */
242
+ function composePartClass(base, mixin, ...fields) {
243
+ const compObj = base;
244
+ for (const fd of fields) {
245
+ Object.defineProperty(compObj, fd, {
246
+ get: () => mixin[fd],
247
+ set: (value) => { mixin[fd] = value; },
248
+ enumerable: true,
249
+ //writable: true ,
250
+ configurable: true
251
+ });
252
+ //(compObj as any)[fd] = (mixin[fd] as any).bind(mixin);
253
+ }
254
+ return compObj;
255
+ }
256
+ UtilFunc.composePartClass = composePartClass;
257
+ /**类组合
258
+ * 将mixinList每个成员的字段混入base
259
+ * @param base - 基础类
260
+ * @param mixinList - 目标类
261
+ * @returns - 混合完成的类
262
+ */
263
+ function composeClass(base, ...mixinList) {
264
+ let obj = base;
265
+ for (let mixin of mixinList) {
266
+ let propks = Object.getOwnPropertyNames(mixin.constructor.prototype);
267
+ for (const key of propks) {
268
+ if (key != "constructor") {
269
+ Object.defineProperty(obj, key, {
270
+ get: () => mixin[key],
271
+ set: (value) => { mixin[key] = value; },
272
+ enumerable: true,
273
+ //writable: true,
274
+ configurable: true
275
+ });
276
+ //obj[key] = (mixin as any)[key];
277
+ }
278
+ }
279
+ for (const key in mixin) {
280
+ Object.defineProperty(obj, key, {
281
+ get: () => mixin[key],
282
+ set: (value) => { mixin[key] = value; },
283
+ enumerable: true,
284
+ //writable: true ,
285
+ configurable: true
286
+ });
287
+ //obj[key] = (mixin as any)[key];
288
+ }
289
+ }
290
+ return obj;
291
+ }
292
+ UtilFunc.composeClass = composeClass;
293
+ /**对对象的每个属性应用映射函数,并返回一个新的对象。
294
+ * @template T - 对象的类型
295
+ * @param obj - 要处理的对象
296
+ * @param mapper - 映射函数,接受一个值和一个键,返回一个新的值
297
+ * @returns - 一个新的对象,它的属性是原对象的属性经过映射函数处理后的结果
298
+ */
299
+ function mapObject(obj, mapper) {
300
+ return Object.entries(obj).reduce((result, [key, value]) => {
301
+ result[key] = mapper(key, value);
302
+ return result;
303
+ }, {});
304
+ }
305
+ UtilFunc.mapObject = mapObject;
202
306
  })(UtilFunc = exports.UtilFunc || (exports.UtilFunc = {}));
@@ -63,4 +63,26 @@ export type PromiseStat = "Completed" | "Failed" | "Terminated";
63
63
  export type PromiseVerifyFn<T> = (obj: T) => Promise<PromiseStat> | PromiseStat;
64
64
  /**发起promise的函数 */
65
65
  export type PromiseProcFn<T> = () => Promise<T>;
66
+ /**类型中任意函数的字符串名称 */
67
+ export type FuncPropNames<T> = {
68
+ [K in keyof T]: T[K] extends (...args: any[]) => any ? K : never;
69
+ }[keyof T];
70
+ /**部分组合的类
71
+ * @template {Base} - 基类
72
+ * @template {Mixin} - 待混入的类
73
+ * @template {PropKeys} - 需要混入的成员key
74
+ */
75
+ export type ComposedPartClass<Base extends object, Mixin extends object, PropKeys extends keyof Mixin> = Base & Pick<Mixin, PropKeys>;
76
+ /**组合的类 嵌套变体 */
77
+ export type ComposedPartClassMult<Base extends object, Mixin1 extends object = {}, PropKeys1 extends keyof Mixin1 = keyof Mixin1, Mixin2 extends object = {}, PropKeys2 extends keyof Mixin2 = keyof Mixin2, Mixin3 extends object = {}, PropKeys3 extends keyof Mixin3 = keyof Mixin3, Mixin4 extends object = {}, PropKeys4 extends keyof Mixin4 = keyof Mixin4, Mixin5 extends object = {}, PropKeys5 extends keyof Mixin5 = keyof Mixin5, Mixin6 extends object = {}, PropKeys6 extends keyof Mixin6 = keyof Mixin6> = ComposedPartClass<ComposedPartClass<ComposedPartClass<ComposedPartClass<ComposedPartClass<ComposedPartClass<Base, Mixin1, PropKeys1>, Mixin2, PropKeys2>, Mixin3, PropKeys3>, Mixin4, PropKeys4>, Mixin5, PropKeys5>, Mixin6, PropKeys6>;
78
+ /**组合的类
79
+ * @template {Base} - 基类
80
+ * @template {Mixin} - 待混入的类
81
+ */
82
+ export type ComposedClass<Base extends object, Mixin extends object> = Base & Mixin;
83
+ /**组合的类 多组合变体
84
+ * @template {Base} - 基类
85
+ * @template {MixinList}- 待混入的类型数组
86
+ */
87
+ export type ComposedClassMult<Base extends object, MixinList extends object[]> = MixinList extends [infer Mixin, ...infer Rest] ? Mixin extends object ? Rest extends object[] ? ComposedClassMult<ComposedClass<Base, Mixin>, Rest> : Base : Base : Base;
66
88
  export {};