@zwa73/utils 1.0.76 → 1.0.78

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.
@@ -0,0 +1,15 @@
1
+ import { ExtractOutcome, Keyable, Outcome } from "./UtilInterfaces";
2
+ /**创建一个outcome */
3
+ export declare function outcome<K extends Keyable, V>(key: K, value: V): Outcome<K, V>;
4
+ /**处理联合 简单值
5
+ * @param t - 目标值
6
+ * @param procObj - 所有可能的id组成的处理函数映射
7
+ * @returns 任意处理函数的返回值
8
+ */
9
+ export declare function match<T extends Keyable | Outcome<Keyable, unknown>, P extends (T extends Keyable ? {
10
+ [K in T]: (k: K) => unknown;
11
+ } : T extends Outcome<Keyable, unknown> ? {
12
+ [K in T['status']]: (k: K, v: ExtractOutcome<T, K>['result']) => unknown;
13
+ } : never)>(t: T, procObj: P): P extends Record<any, (...args: any) => any> ? {
14
+ [K in keyof P]: ReturnType<P[K]>;
15
+ }[keyof P] : never;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.match = exports.outcome = void 0;
4
+ /**创建一个outcome */
5
+ function outcome(key, value) {
6
+ return { status: key, result: value };
7
+ }
8
+ exports.outcome = outcome;
9
+ /**处理联合 简单值
10
+ * @param t - 目标值
11
+ * @param procObj - 所有可能的id组成的处理函数映射
12
+ * @returns 任意处理函数的返回值
13
+ */
14
+ function match(t, procObj) {
15
+ if (typeof t === 'string' || typeof t === 'number' || typeof t === 'symbol')
16
+ return procObj[t](t);
17
+ else
18
+ return procObj[t.status](t.status, t.result);
19
+ }
20
+ exports.match = match;
21
+ if (false) {
22
+ let a = null;
23
+ const r = match(a, {
24
+ "asd": (a) => "ssa",
25
+ "dsa": (b) => "ssb",
26
+ });
27
+ }
package/dist/UtilCom.js CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.UtilCom = void 0;
4
- const UtilInterfaces_1 = require("./UtilInterfaces");
5
4
  const https = require("https");
6
5
  const http = require("http");
7
6
  const UtilLogger_1 = require("./UtilLogger");
@@ -21,7 +20,7 @@ var UtilCom;
21
20
  const hasTimeLimit = (timeLimit >= 10);
22
21
  if (hasTimeLimit)
23
22
  timeLimit *= 1000;
24
- const jsonStr = (0, UtilInterfaces_1.stringifyJToken)(json);
23
+ const jsonStr = UtilFunctions_1.UtilFunc.stringifyJToken(json);
25
24
  const funcName = `s${posttype}Psot`;
26
25
  return new Promise((resolve, rejecte) => {
27
26
  const resFunc = (res) => {
@@ -51,7 +50,7 @@ var UtilCom;
51
50
  }
52
51
  try {
53
52
  let obj = JSON.parse(resdata);
54
- UtilLogger_1.SLogger.http(funcName + " 接受信息:", (0, UtilInterfaces_1.stringifyJToken)(obj));
53
+ UtilLogger_1.SLogger.http(funcName + " 接受信息:", UtilFunctions_1.UtilFunc.stringifyJToken(obj));
55
54
  resolve(obj);
56
55
  return;
57
56
  }
@@ -1,5 +1,15 @@
1
1
  type TDTg<T> = (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>;
2
2
  type DTg = (target: Object, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
3
+ /**用于打印方法运行时间
4
+ * @param flag - 时间标签
5
+ * @param suffixUID - 是否尾随uid
6
+ */
7
+ export declare function LogTime(flag: string, suffixUID?: boolean): DTg;
8
+ /**用于打印异步方法运行时间
9
+ * @param flag - 时间标签
10
+ * @param suffixUID - 是否尾随uid
11
+ */
12
+ export declare function LogTimeAsync(flag: string, suffixUID?: boolean): DTg;
3
13
  /**用于打印方法的调用 */
4
14
  export declare function LogCall(): DTg;
5
15
  /**用于打印异步方法的调用 */
@@ -9,8 +9,45 @@ var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.CatchAsync = exports.DCatch = exports.DeferAsync = exports.Defer = exports.LogErrAsync = exports.LogErr = exports.LogCallAsync = exports.LogCall = void 0;
12
+ exports.CatchAsync = exports.DCatch = exports.DeferAsync = exports.Defer = exports.LogErrAsync = exports.LogErr = exports.LogCallAsync = exports.LogCall = exports.LogTimeAsync = exports.LogTime = void 0;
13
+ const UtilFunctions_1 = require("./UtilFunctions");
13
14
  const UtilLogger_1 = require("./UtilLogger");
15
+ /**用于打印方法运行时间
16
+ * @param flag - 时间标签
17
+ * @param suffixUID - 是否尾随uid
18
+ */
19
+ function LogTime(flag, suffixUID) {
20
+ return function (target, propertyKey, descriptor) {
21
+ const originalMethod = descriptor.value;
22
+ descriptor.value = function (...args) {
23
+ const uid = suffixUID ? UtilFunctions_1.UtilFunc.genUUID() : "";
24
+ UtilLogger_1.SLogger.time(flag + uid);
25
+ let result = originalMethod.apply(this, args);
26
+ UtilLogger_1.SLogger.timeEnd(flag + uid);
27
+ return result;
28
+ };
29
+ return descriptor;
30
+ };
31
+ }
32
+ exports.LogTime = LogTime;
33
+ /**用于打印异步方法运行时间
34
+ * @param flag - 时间标签
35
+ * @param suffixUID - 是否尾随uid
36
+ */
37
+ function LogTimeAsync(flag, suffixUID) {
38
+ return function (target, propertyKey, descriptor) {
39
+ const originalMethod = descriptor.value;
40
+ descriptor.value = async function (...args) {
41
+ const uid = suffixUID ? UtilFunctions_1.UtilFunc.genUUID() : "";
42
+ UtilLogger_1.SLogger.time(flag + uid);
43
+ let result = await originalMethod.apply(this, args);
44
+ UtilLogger_1.SLogger.timeEnd(flag + uid);
45
+ return result;
46
+ };
47
+ return descriptor;
48
+ };
49
+ }
50
+ exports.LogTimeAsync = LogTimeAsync;
14
51
  /**用于打印方法的调用 */
15
52
  function LogCall() {
16
53
  return function (target, propertyKey, descriptor) {
@@ -158,7 +195,7 @@ class Example {
158
195
  myMethod(num) {
159
196
  return num;
160
197
  }
161
- async myMethod1(num, ss) {
198
+ static async myMethod1(num, ss) {
162
199
  return 312;
163
200
  }
164
201
  }
@@ -174,7 +211,7 @@ __decorate([
174
211
  __metadata("design:type", Function),
175
212
  __metadata("design:paramtypes", [Number, String]),
176
213
  __metadata("design:returntype", Promise)
177
- ], Example.prototype, "myMethod1", null);
214
+ ], Example, "myMethod1", null);
178
215
  //let e = new Example();
179
216
  //e.myMethod1(1,"");
180
217
  //console.log(123);
package/dist/UtilFP.js CHANGED
@@ -16,12 +16,14 @@ var UtilFP;
16
16
  });
17
17
  }
18
18
  UtilFP.curry = curry;
19
+ //#endregion
19
20
  function flow(...fs) {
20
21
  return (arg) => {
21
22
  return fs.reduce((value, func) => func(value), arg);
22
23
  };
23
24
  }
24
25
  UtilFP.flow = flow;
26
+ //#endregion
25
27
  function pipe(input, ...fs) {
26
28
  return fs.reduce((value, func) => func(value), input);
27
29
  }
@@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.UtilFT = void 0;
4
4
  const fs = require("fs");
5
5
  const path = require("path");
6
- const UtilInterfaces_1 = require("./UtilInterfaces");
7
6
  const UtilLogger_1 = require("./UtilLogger");
8
7
  const JSON5 = require("json5");
9
8
  const glob_1 = require("glob");
9
+ const UtilFunctions_1 = require("./UtilFunctions");
10
10
  /**文件工具 */
11
11
  var UtilFT;
12
12
  (function (UtilFT) {
@@ -149,7 +149,7 @@ var UtilFT;
149
149
  * @param token - 所要写入的JToken
150
150
  */
151
151
  async function writeJSONFile(filePath, token) {
152
- let str = (0, UtilInterfaces_1.stringifyJToken)(token);
152
+ let str = UtilFunctions_1.UtilFunc.stringifyJToken(token);
153
153
  if (path.extname(filePath) !== '.json')
154
154
  filePath += '.json';
155
155
  // 判断文件路径是否存在 不存在则创建
@@ -1,10 +1,13 @@
1
- import { ComposedClass, ComposedMixinable, JObject, JToken, Mixinable, PromiseProcFn, PromiseVerifyFn } from "./UtilInterfaces";
1
+ import { ComposedClass, ComposedMixinable, IJData, JObject, JToken, Mixinable, Outcome, PromiseVerifyFn } from "./UtilInterfaces";
2
+ import { Completed, Timeout } from "./UtilSymbol";
3
+ type CompleteCome<T> = Outcome<Completed, T>;
4
+ type TimeoutCome<T> = Outcome<Timeout, Promise<T>>;
2
5
  /**常用函数 */
3
- export declare namespace UtilFunc {
6
+ export declare class UtilFunc {
4
7
  /**获取当前时间戳
5
8
  * @returns 时间戳
6
9
  */
7
- function getTime(): number;
10
+ static getTime(): number;
8
11
  /**初始化对象的字段
9
12
  * 会改变obj
10
13
  * @param obj - 所要初始化的对象
@@ -12,59 +15,72 @@ export declare namespace UtilFunc {
12
15
  * @param defaultVal - 默认值
13
16
  * @returns 最终值
14
17
  */
15
- function initField<T extends object, K extends keyof T>(obj: T, field: K, defaultVal: T[K]): T[K];
18
+ static initField<T extends object, K extends keyof T>(obj: T, field: K, defaultVal: T[K]): T[K];
16
19
  /**初始化一个数据对象
17
20
  * @param obj - 目标对象
18
21
  * @param checkObj - 用于检测的对象 在对应key缺失时赋予对应值 如果值为函数, 则赋予执行结果 如果结果为 undefined 则不赋值
19
22
  * @returns 完成初始化的对象
20
23
  */
21
- function initObject<T extends JObject>(obj: T, checkObj: {
24
+ static initObject<T extends JObject>(obj: T, checkObj: {
22
25
  [P in keyof T]: T[P] | (() => void | T[P]);
23
26
  }): any;
24
27
  /**生成一串uuid
25
28
  * @returns uuid
26
29
  */
27
- function genUUID(): string;
30
+ static genUUID(): string;
28
31
  /**计算Hash
29
32
  * @param str - 待计算的字符串
30
33
  * @returns hash
31
34
  */
32
- function calcHash(str: string): string;
35
+ static calcHash(str: string): string;
33
36
  /**深克隆 序列化并反序列化
34
37
  * @template T - JToken类型的泛型
35
38
  * @param obj - 克隆目标
36
39
  * @returns 克隆结果
37
40
  */
38
- function deepClone<T extends JToken>(obj: T): T;
41
+ static deepClone<T extends JToken>(obj: T): T;
39
42
  /**是否为安全的数字
40
43
  * @param num - 所要检测的数字
41
44
  * @returns 是否安全
42
45
  */
43
- function isSafeNumber(num: number): boolean;
46
+ static isSafeNumber(num: number): boolean;
44
47
  /**等待 timeMs 毫秒
45
48
  * @async
46
49
  * @param timeMs - 等待的毫秒数
47
50
  * @returns
48
51
  */
49
- function sleep(timeMs: number): Promise<boolean>;
52
+ static sleep<T>(timeMs: number): Promise<void>;
53
+ /**等待 timeMs 毫秒
54
+ * @async
55
+ * @param timeMs - 等待的毫秒数
56
+ * @param result - 结果
57
+ * @returns
58
+ */
59
+ static sleep<T>(timeMs: number, result: T): Promise<T>;
50
60
  /**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
51
61
  * @param command 指令文本
52
62
  */
53
- function exec(command: string): Promise<{
63
+ static exec(command: string): Promise<{
54
64
  stdout: string;
55
65
  stderr: string;
56
66
  }>;
57
67
  /**获得一个永不完成的Promise单例 */
58
- function getNeverResolvedPromise<T>(): Promise<T>;
68
+ static getNeverResolvedPromise<T>(): Promise<T>;
59
69
  /**重复尝试promise
60
70
  * @async
61
71
  * @param procFn - 发起函数
62
72
  * @param verifyFn - 验证函数
63
73
  * @param repeatCount - 重试次数
64
74
  * @param repeatTime - 超时时间/秒 最小为10秒
65
- * @returns - 结果 null 为全部失败/超时
75
+ * @returns 结果 null 为全部失败/超时
76
+ */
77
+ static repeatPromise<T>(procFn: () => Promise<T>, verifyFn?: PromiseVerifyFn<T>, repeatCount?: number, repeatTime?: number): Promise<T | null>;
78
+ /**创建一个限时的Promise
79
+ * @param func - 处理函数
80
+ * @param timeLimit - 毫秒限时
81
+ * @returns 超时则返回 处理函数委托 完成则返回结果
66
82
  */
67
- function repeatPromise<T>(procFn: PromiseProcFn<T>, verifyFn?: PromiseVerifyFn<T>, repeatCount?: number, repeatTime?: number): Promise<T | null>;
83
+ static timelimitPromise<T>(func: () => Promise<T> | T, timeLimit?: number): Promise<CompleteCome<T> | TimeoutCome<T>>;
68
84
  /**部分类组合
69
85
  * 将mixin的部分字段混入base
70
86
  * @param base - 基础类
@@ -73,14 +89,21 @@ export declare namespace UtilFunc {
73
89
  * @param fields - 需要混入的字段
74
90
  * @returns 混合完成的类
75
91
  */
76
- function composeClassPart<Base extends object, Mixin extends object, Field extends keyof Mixin>(base: Base, mixin: Mixin, key: string, ...fields: Field[]): ComposedClass<Base, Mixin, typeof key, Field>;
92
+ static composeClassPart<Base extends object, Mixin extends object, Field extends keyof Mixin>(base: Base, mixin: Mixin, key: string, ...fields: Field[]): ComposedClass<Base, Mixin, typeof key, Field>;
77
93
  /**根据 MIXIN_FIELDS 自动混入 */
78
- function composeMixinable<Base extends object, Mixins extends Mixinable<any>[]>(base: Base, ...mixins: Mixins): ComposedMixinable<Base, Mixins>;
94
+ static composeMixinable<Base extends object, Mixins extends Mixinable<any>[]>(base: Base, ...mixins: Mixins): ComposedMixinable<Base, Mixins>;
79
95
  /**对对象的每个属性应用映射函数,并返回一个新的对象。
80
96
  * @template T - 对象的类型
81
97
  * @param obj - 要处理的对象
82
98
  * @param mapper - 映射函数,接受一个值和一个键,返回一个新的值
83
99
  * @returns - 一个新的对象,它的属性是原对象的属性经过映射函数处理后的结果
84
100
  */
85
- function mapEntries<T extends Object>(obj: T, mapper: (key: keyof T, value: T[keyof T]) => T[keyof T]): T;
101
+ static mapEntries<T extends Object>(obj: T, mapper: (key: keyof T, value: T[keyof T]) => T[keyof T]): T;
102
+ /**将JToken转换为字符串
103
+ * @param token - 待转换的Token
104
+ * @param space - 插入的空格 数字为空格数量 默认为制表符\t
105
+ * @returns 转换完成的字符串
106
+ */
107
+ static stringifyJToken(token: JToken | IJData, space?: string | number | null | undefined): string;
86
108
  }
109
+ export {};