@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.
- package/dist/QuickFunction.d.ts +15 -0
- package/dist/QuickFunction.js +27 -0
- package/dist/UtilCom.js +2 -3
- package/dist/UtilDecorators.d.ts +10 -0
- package/dist/UtilDecorators.js +40 -3
- package/dist/UtilFP.js +2 -0
- package/dist/UtilFileTools.js +2 -2
- package/dist/UtilFunctions.d.ts +40 -17
- package/dist/UtilFunctions.js +119 -96
- package/dist/UtilInterfaces.d.ts +43 -13
- package/dist/UtilInterfaces.js +0 -12
- package/dist/UtilSymbol.d.ts +15 -0
- package/dist/UtilSymbol.js +13 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/package.json +1 -1
- package/src/QuickFunction.ts +36 -0
- package/src/UtilCom.ts +3 -3
- package/src/UtilDecorators.ts +37 -1
- package/src/UtilFP.ts +5 -0
- package/src/UtilFileTools.ts +3 -2
- package/src/UtilFunctions.ts +143 -91
- package/src/UtilInterfaces.ts +57 -17
- package/src/UtilSymbol.ts +15 -0
- package/src/index.ts +3 -1
- package/src/test/composeTest.ts +28 -1
package/dist/UtilFunctions.js
CHANGED
|
@@ -1,19 +1,31 @@
|
|
|
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
12
|
exports.UtilFunc = void 0;
|
|
4
13
|
const crypto = require("crypto");
|
|
5
14
|
const cp = require("child_process");
|
|
6
15
|
const UtilLogger_1 = require("./UtilLogger");
|
|
16
|
+
const UtilSymbol_1 = require("./UtilSymbol");
|
|
17
|
+
const QuickFunction_1 = require("./QuickFunction");
|
|
18
|
+
const UtilDecorators_1 = require("./UtilDecorators");
|
|
19
|
+
/**永不完成的Promise单例 */
|
|
20
|
+
const NeverResolvedPromise = new Promise(() => { });
|
|
7
21
|
/**常用函数 */
|
|
8
|
-
|
|
9
|
-
(function (UtilFunc) {
|
|
22
|
+
class UtilFunc {
|
|
10
23
|
/**获取当前时间戳
|
|
11
24
|
* @returns 时间戳
|
|
12
25
|
*/
|
|
13
|
-
|
|
26
|
+
static getTime() {
|
|
14
27
|
return new Date().getTime();
|
|
15
28
|
}
|
|
16
|
-
UtilFunc.getTime = getTime;
|
|
17
29
|
/**初始化对象的字段
|
|
18
30
|
* 会改变obj
|
|
19
31
|
* @param obj - 所要初始化的对象
|
|
@@ -21,18 +33,17 @@ var UtilFunc;
|
|
|
21
33
|
* @param defaultVal - 默认值
|
|
22
34
|
* @returns 最终值
|
|
23
35
|
*/
|
|
24
|
-
|
|
36
|
+
static initField(obj, field, defaultVal) {
|
|
25
37
|
if (!(field in obj))
|
|
26
38
|
obj[field] = defaultVal;
|
|
27
39
|
return obj[field];
|
|
28
40
|
}
|
|
29
|
-
UtilFunc.initField = initField;
|
|
30
41
|
/**初始化一个数据对象
|
|
31
42
|
* @param obj - 目标对象
|
|
32
43
|
* @param checkObj - 用于检测的对象 在对应key缺失时赋予对应值 如果值为函数, 则赋予执行结果 如果结果为 undefined 则不赋值
|
|
33
44
|
* @returns 完成初始化的对象
|
|
34
45
|
*/
|
|
35
|
-
|
|
46
|
+
static initObject(obj, checkObj) {
|
|
36
47
|
const fixobj = obj;
|
|
37
48
|
for (const key in checkObj) {
|
|
38
49
|
if (obj[key] !== undefined)
|
|
@@ -48,60 +59,49 @@ var UtilFunc;
|
|
|
48
59
|
}
|
|
49
60
|
return fixobj;
|
|
50
61
|
}
|
|
51
|
-
UtilFunc.initObject = initObject;
|
|
52
62
|
/**生成一串uuid
|
|
53
63
|
* @returns uuid
|
|
54
64
|
*/
|
|
55
|
-
|
|
65
|
+
static genUUID() {
|
|
56
66
|
return crypto.randomBytes(16).toString("hex");
|
|
57
67
|
}
|
|
58
|
-
UtilFunc.genUUID = genUUID;
|
|
59
68
|
/**计算Hash
|
|
60
69
|
* @param str - 待计算的字符串
|
|
61
70
|
* @returns hash
|
|
62
71
|
*/
|
|
63
|
-
|
|
72
|
+
static calcHash(str) {
|
|
64
73
|
return crypto.createHash('md5').update(str).digest('hex');
|
|
65
74
|
}
|
|
66
|
-
UtilFunc.calcHash = calcHash;
|
|
67
75
|
/**深克隆 序列化并反序列化
|
|
68
76
|
* @template T - JToken类型的泛型
|
|
69
77
|
* @param obj - 克隆目标
|
|
70
78
|
* @returns 克隆结果
|
|
71
79
|
*/
|
|
72
|
-
|
|
80
|
+
static deepClone(obj) {
|
|
73
81
|
return JSON.parse(JSON.stringify(obj));
|
|
74
82
|
}
|
|
75
|
-
UtilFunc.deepClone = deepClone;
|
|
76
83
|
/**是否为安全的数字
|
|
77
84
|
* @param num - 所要检测的数字
|
|
78
85
|
* @returns 是否安全
|
|
79
86
|
*/
|
|
80
|
-
|
|
87
|
+
static isSafeNumber(num) {
|
|
81
88
|
if (num === undefined || num == null || isNaN(num))
|
|
82
89
|
return false;
|
|
83
90
|
if (typeof num === 'number')
|
|
84
91
|
return true;
|
|
85
92
|
return false;
|
|
86
93
|
}
|
|
87
|
-
|
|
88
|
-
/**等待 timeMs 毫秒
|
|
89
|
-
* @async
|
|
90
|
-
* @param timeMs - 等待的毫秒数
|
|
91
|
-
* @returns
|
|
92
|
-
*/
|
|
93
|
-
async function sleep(timeMs) {
|
|
94
|
+
static async sleep(timeMs, result) {
|
|
94
95
|
return new Promise(function (resolve, rejecte) {
|
|
95
96
|
let timer = setTimeout(function () {
|
|
96
|
-
resolve(
|
|
97
|
+
resolve(result);
|
|
97
98
|
}, timeMs);
|
|
98
99
|
});
|
|
99
100
|
}
|
|
100
|
-
UtilFunc.sleep = sleep;
|
|
101
101
|
/**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
|
|
102
102
|
* @param command 指令文本
|
|
103
103
|
*/
|
|
104
|
-
|
|
104
|
+
static exec(command) {
|
|
105
105
|
return new Promise((resolve, reject) => {
|
|
106
106
|
cp.exec(command, (error, stdout, stderr) => {
|
|
107
107
|
if (error)
|
|
@@ -111,98 +111,85 @@ var UtilFunc;
|
|
|
111
111
|
});
|
|
112
112
|
});
|
|
113
113
|
}
|
|
114
|
-
UtilFunc.exec = exec;
|
|
115
|
-
/**永不完成的Promise单例 */
|
|
116
|
-
const NeverResolvedPromise = new Promise(() => { });
|
|
117
114
|
/**获得一个永不完成的Promise单例 */
|
|
118
|
-
|
|
115
|
+
static getNeverResolvedPromise() {
|
|
119
116
|
return NeverResolvedPromise;
|
|
120
117
|
}
|
|
121
|
-
UtilFunc.getNeverResolvedPromise = getNeverResolvedPromise;
|
|
122
118
|
/**重复尝试promise
|
|
123
119
|
* @async
|
|
124
120
|
* @param procFn - 发起函数
|
|
125
121
|
* @param verifyFn - 验证函数
|
|
126
122
|
* @param repeatCount - 重试次数
|
|
127
123
|
* @param repeatTime - 超时时间/秒 最小为10秒
|
|
128
|
-
* @returns
|
|
124
|
+
* @returns 结果 null 为全部失败/超时
|
|
129
125
|
*/
|
|
130
|
-
async
|
|
131
|
-
|
|
132
|
-
const timeflag = "repeatPromise " + UtilFunc.genUUID();
|
|
133
|
-
UtilLogger_1.SLogger.time(timeflag);
|
|
134
|
-
//转换为毫秒
|
|
126
|
+
static async repeatPromise(procFn, verifyFn, repeatCount = 3, repeatTime = 180) {
|
|
127
|
+
/**是否含有超时时间 */
|
|
135
128
|
const hasRepeatTime = (repeatTime >= 10);
|
|
129
|
+
//转换为毫秒
|
|
136
130
|
if (hasRepeatTime)
|
|
137
131
|
repeatTime *= 1000;
|
|
138
132
|
//验证处理函数
|
|
139
133
|
if (verifyFn === undefined)
|
|
140
|
-
verifyFn = () =>
|
|
141
|
-
//计时器
|
|
142
|
-
let timer = null;
|
|
143
|
-
let timerP = null;
|
|
144
|
-
let resolveFn = null;
|
|
145
|
-
/**清理计时器 */
|
|
146
|
-
const clearTimer = () => {
|
|
147
|
-
if (timer != null)
|
|
148
|
-
clearInterval(timer);
|
|
149
|
-
if (resolveFn != null)
|
|
150
|
-
resolveFn("Timeout");
|
|
151
|
-
timerP = null;
|
|
152
|
-
timer = null;
|
|
153
|
-
resolveFn = null;
|
|
154
|
-
};
|
|
134
|
+
verifyFn = () => UtilSymbol_1.Completed;
|
|
155
135
|
//进行中的请求
|
|
156
136
|
const plist = [];
|
|
157
137
|
//开始处理
|
|
158
138
|
try {
|
|
139
|
+
//根据最大重试次数限制进行循环
|
|
159
140
|
for (let i = 0; i < repeatCount;) {
|
|
160
141
|
UtilLogger_1.SLogger.info(`开始第 ${i + 1} 次 repeatPromise`);
|
|
161
|
-
|
|
142
|
+
//如果 plist 中当前下标的任务还未创建 则 创建当前任务
|
|
162
143
|
if (plist.length < i + 1) {
|
|
163
|
-
plist.push(
|
|
144
|
+
plist.push(UtilFunc.timelimitPromise(async () => {
|
|
145
|
+
const result = await procFn();
|
|
146
|
+
const stat = await verifyFn(result);
|
|
147
|
+
return { result, stat, index: i };
|
|
148
|
+
}));
|
|
164
149
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
timerP = new Promise(function (resolve, rejecte) {
|
|
168
|
-
resolveFn = resolve;
|
|
169
|
-
timer = setTimeout(() => resolve("Timeout"), hasRepeatTime ? repeatTime : Infinity); //无限制则无限时间
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
//等待完成
|
|
173
|
-
const currObj = await Promise.race([...plist, timerP]);
|
|
150
|
+
//等待任意任务 或当前计时器完成
|
|
151
|
+
const currObj = await Promise.race([...plist]);
|
|
174
152
|
//超时处理
|
|
175
|
-
if (currObj
|
|
153
|
+
if (currObj.status === UtilSymbol_1.Timeout) {
|
|
154
|
+
//解除timeout替换原参数
|
|
155
|
+
plist[i] = new Promise(async (rslove) => {
|
|
156
|
+
const res = await currObj.result;
|
|
157
|
+
rslove((0, QuickFunction_1.outcome)(UtilSymbol_1.Completed, res));
|
|
158
|
+
});
|
|
176
159
|
UtilLogger_1.SLogger.warn(`第 ${i + 1} 次 repeatPromise 超时 ${repeatTime} ms 开始重试`);
|
|
177
|
-
clearTimer();
|
|
178
160
|
i++;
|
|
179
161
|
continue;
|
|
180
162
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
163
|
+
//路由请求状态
|
|
164
|
+
const postresult = currObj.result;
|
|
165
|
+
const result = (0, QuickFunction_1.match)(postresult.stat, {
|
|
166
|
+
[UtilSymbol_1.Completed]() {
|
|
167
|
+
UtilLogger_1.SLogger.info(`第 ${postresult.index + 1} 次 repeatPromise 成功`);
|
|
185
168
|
//非当前
|
|
186
|
-
if (
|
|
169
|
+
if (postresult.index != i)
|
|
187
170
|
UtilLogger_1.SLogger.info(`成功的 promise 非当前 promise 考虑增大重试间隔\n当前index: ${i}\n当前间隔: ${repeatTime}`);
|
|
188
|
-
return
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
171
|
+
return postresult.result;
|
|
172
|
+
},
|
|
173
|
+
[UtilSymbol_1.Terminated]() {
|
|
174
|
+
UtilLogger_1.SLogger.warn(`第 ${postresult.index + 1} 次 repeatPromise 终止 停止重试`);
|
|
175
|
+
return postresult.result;
|
|
176
|
+
},
|
|
177
|
+
[UtilSymbol_1.Failed]() {
|
|
193
178
|
//抛弃失败
|
|
194
|
-
plist[
|
|
179
|
+
plist[postresult.index] = UtilFunc.getNeverResolvedPromise();
|
|
195
180
|
//是当前
|
|
196
|
-
if (
|
|
197
|
-
UtilLogger_1.SLogger.warn(`第 ${
|
|
198
|
-
clearTimer();
|
|
181
|
+
if (postresult.index == i) {
|
|
182
|
+
UtilLogger_1.SLogger.warn(`第 ${postresult.index + 1} 次 repeatPromise 失败 开始重试`);
|
|
199
183
|
i++;
|
|
200
|
-
|
|
184
|
+
return UtilSymbol_1.None;
|
|
201
185
|
}
|
|
202
186
|
//非当前
|
|
203
|
-
UtilLogger_1.SLogger.warn(`第 ${
|
|
204
|
-
|
|
205
|
-
|
|
187
|
+
UtilLogger_1.SLogger.warn(`第 ${postresult.index + 1} 次 repeatPromise 失败`);
|
|
188
|
+
return UtilSymbol_1.None;
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
if (result !== UtilSymbol_1.None)
|
|
192
|
+
return result;
|
|
206
193
|
}
|
|
207
194
|
//全部失败或超时则返回null
|
|
208
195
|
UtilLogger_1.SLogger.warn(`${repeatCount} 次 repeatPromise 尝试均失败`);
|
|
@@ -212,13 +199,35 @@ var UtilFunc;
|
|
|
212
199
|
UtilLogger_1.SLogger.warn(`repeatPromise 发生错误`, err);
|
|
213
200
|
return null;
|
|
214
201
|
}
|
|
215
|
-
finally {
|
|
216
|
-
//清理
|
|
217
|
-
clearTimer();
|
|
218
|
-
UtilLogger_1.SLogger.timeEnd(timeflag);
|
|
219
|
-
}
|
|
220
202
|
}
|
|
221
|
-
|
|
203
|
+
/**创建一个限时的Promise
|
|
204
|
+
* @param func - 处理函数
|
|
205
|
+
* @param timeLimit - 毫秒限时
|
|
206
|
+
* @returns 超时则返回 处理函数委托 完成则返回结果
|
|
207
|
+
*/
|
|
208
|
+
static timelimitPromise(func, timeLimit) {
|
|
209
|
+
return new Promise((reslove) => {
|
|
210
|
+
let clearTimer = null;
|
|
211
|
+
let proc = (async () => await func())();
|
|
212
|
+
const procerP = new Promise(async (resolve) => {
|
|
213
|
+
const res = await proc;
|
|
214
|
+
if (clearTimer)
|
|
215
|
+
clearTimer();
|
|
216
|
+
resolve((0, QuickFunction_1.outcome)(UtilSymbol_1.Completed, res));
|
|
217
|
+
});
|
|
218
|
+
const timerP = timeLimit
|
|
219
|
+
? new Promise((resolve) => {
|
|
220
|
+
const timer = setTimeout(() => resolve((0, QuickFunction_1.outcome)(UtilSymbol_1.Timeout, proc)), timeLimit); //无限制则无限时间
|
|
221
|
+
clearTimer = () => {
|
|
222
|
+
resolve((0, QuickFunction_1.outcome)(UtilSymbol_1.Timeout, proc));
|
|
223
|
+
clearInterval(timer);
|
|
224
|
+
};
|
|
225
|
+
})
|
|
226
|
+
: UtilFunc.getNeverResolvedPromise();
|
|
227
|
+
const result = Promise.race([procerP, timerP]);
|
|
228
|
+
reslove(result);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
222
231
|
/**部分类组合
|
|
223
232
|
* 将mixin的部分字段混入base
|
|
224
233
|
* @param base - 基础类
|
|
@@ -227,7 +236,7 @@ var UtilFunc;
|
|
|
227
236
|
* @param fields - 需要混入的字段
|
|
228
237
|
* @returns 混合完成的类
|
|
229
238
|
*/
|
|
230
|
-
|
|
239
|
+
static composeClassPart(base, mixin, key, ...fields) {
|
|
231
240
|
const compObj = base;
|
|
232
241
|
if (compObj[key] !== undefined)
|
|
233
242
|
UtilLogger_1.SLogger.warn(`key: ${key} 已存在于基础类中, 混入可能导致类型问题`);
|
|
@@ -259,9 +268,8 @@ var UtilFunc;
|
|
|
259
268
|
}
|
|
260
269
|
return compObj;
|
|
261
270
|
}
|
|
262
|
-
UtilFunc.composeClassPart = composeClassPart;
|
|
263
271
|
/**根据 MIXIN_FIELDS 自动混入 */
|
|
264
|
-
|
|
272
|
+
static composeMixinable(base, ...mixins) {
|
|
265
273
|
let out = base;
|
|
266
274
|
const fieldsSet = new Set();
|
|
267
275
|
for (const rawmixin of mixins) {
|
|
@@ -273,22 +281,37 @@ var UtilFunc;
|
|
|
273
281
|
else
|
|
274
282
|
fieldsSet.add(fixField);
|
|
275
283
|
}
|
|
276
|
-
out = composeClassPart(base, mixin, mixin.MIXIN_KEY, ...mixin.MIXIN_FIELDS);
|
|
284
|
+
out = UtilFunc.composeClassPart(base, mixin, mixin.MIXIN_KEY, ...mixin.MIXIN_FIELDS);
|
|
277
285
|
}
|
|
278
286
|
return out;
|
|
279
287
|
}
|
|
280
|
-
UtilFunc.composeMixinable = composeMixinable;
|
|
281
288
|
/**对对象的每个属性应用映射函数,并返回一个新的对象。
|
|
282
289
|
* @template T - 对象的类型
|
|
283
290
|
* @param obj - 要处理的对象
|
|
284
291
|
* @param mapper - 映射函数,接受一个值和一个键,返回一个新的值
|
|
285
292
|
* @returns - 一个新的对象,它的属性是原对象的属性经过映射函数处理后的结果
|
|
286
293
|
*/
|
|
287
|
-
|
|
294
|
+
static mapEntries(obj, mapper) {
|
|
288
295
|
return Object.entries(obj).reduce((result, [key, value]) => {
|
|
289
296
|
result[key] = mapper(key, value);
|
|
290
297
|
return result;
|
|
291
298
|
}, {});
|
|
292
299
|
}
|
|
293
|
-
|
|
294
|
-
|
|
300
|
+
/**将JToken转换为字符串
|
|
301
|
+
* @param token - 待转换的Token
|
|
302
|
+
* @param space - 插入的空格 数字为空格数量 默认为制表符\t
|
|
303
|
+
* @returns 转换完成的字符串
|
|
304
|
+
*/
|
|
305
|
+
static stringifyJToken(token, space = "\t") {
|
|
306
|
+
if (space == null)
|
|
307
|
+
space = undefined;
|
|
308
|
+
return JSON.stringify(token, null, space);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
__decorate([
|
|
312
|
+
(0, UtilDecorators_1.LogTimeAsync)("repeatPromise ", true),
|
|
313
|
+
__metadata("design:type", Function),
|
|
314
|
+
__metadata("design:paramtypes", [Function, Function, Number, Number]),
|
|
315
|
+
__metadata("design:returntype", Promise)
|
|
316
|
+
], UtilFunc, "repeatPromise", null);
|
|
317
|
+
exports.UtilFunc = UtilFunc;
|
package/dist/UtilInterfaces.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Completed, Failed, Terminated } from "./UtilSymbol";
|
|
1
2
|
/**可以序列化为JSON文件的对象 */
|
|
2
3
|
export type JToken = JObject | JArray | JValue | IJData;
|
|
3
4
|
/**在stringify输出时 undefine 会被转为 null */
|
|
@@ -14,12 +15,8 @@ export interface IJData {
|
|
|
14
15
|
*/
|
|
15
16
|
toJSON(): JToken;
|
|
16
17
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
* @param space - 插入的空格 数字为空格数量 默认为制表符\t
|
|
20
|
-
* @returns 转换完成的字符串
|
|
21
|
-
*/
|
|
22
|
-
export declare function stringifyJToken(token: JToken | IJData, space?: string | number | null | undefined): string;
|
|
18
|
+
/**任意可作为键值的类型 */
|
|
19
|
+
export type Keyable = string | number | symbol;
|
|
23
20
|
/**转为可写的 */
|
|
24
21
|
export type Writeable<T> = {
|
|
25
22
|
-readonly [P in keyof T]: T[P];
|
|
@@ -55,24 +52,23 @@ export type ExclusiveRecord<B, T, K extends string[], TMP = ExclusiveRecursive<B
|
|
|
55
52
|
/**符合JObject约束的互斥表 */
|
|
56
53
|
export type ExclusiveJObject<B extends JObject, T extends JToken, K extends string[], TMP extends JArray = ExclusiveRecursive<B, T, K>> = TMP[number];
|
|
57
54
|
/**请求完成状态 成功/失败/终止
|
|
58
|
-
*
|
|
55
|
+
* 成功 将直接返回 结果
|
|
56
|
+
* 终止 将直接返回 null
|
|
59
57
|
* 失败 将重试
|
|
60
58
|
*/
|
|
61
|
-
export type PromiseStat =
|
|
59
|
+
export type PromiseStat = Completed | Failed | Terminated;
|
|
62
60
|
/**promise验证函数 */
|
|
63
61
|
export type PromiseVerifyFn<T> = (obj: T) => Promise<PromiseStat> | PromiseStat;
|
|
64
|
-
/**发起promise的函数 */
|
|
65
|
-
export type PromiseProcFn<T> = () => Promise<T>;
|
|
66
62
|
/**类型中任意函数的字符串名称 */
|
|
67
63
|
export type FuncPropNames<T> = {
|
|
68
|
-
[K in keyof T]: T[K] extends (...args:
|
|
64
|
+
[K in keyof T]: T[K] extends (...args: unknown[]) => unknown ? K : never;
|
|
69
65
|
}[keyof T];
|
|
70
66
|
/**部分组合的类
|
|
71
67
|
* @template Base - 基类
|
|
72
68
|
* @template Mixin - 待混入的类
|
|
73
69
|
* @template PropKeys - 需要混入的成员key
|
|
74
70
|
*/
|
|
75
|
-
export type ComposedClass<Base
|
|
71
|
+
export type ComposedClass<Base, Mixin, Key extends string, PropKeys extends keyof Mixin> = Base & Pick<Mixin, PropKeys> & {
|
|
76
72
|
[P in Key]: Mixin;
|
|
77
73
|
};
|
|
78
74
|
/**可自动混入的类型 */
|
|
@@ -87,5 +83,39 @@ export type Mixinable<Mixin> = {
|
|
|
87
83
|
readonly MIXIN_FIELDS: readonly (keyof Mixin)[];
|
|
88
84
|
};
|
|
89
85
|
/**自动组合可混入的类 */
|
|
90
|
-
export type ComposedMixinable<B
|
|
86
|
+
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;
|
|
87
|
+
/** extends封装
|
|
88
|
+
* @template B - 基础类型
|
|
89
|
+
* @template T - 判断的目标类型
|
|
90
|
+
* @template Y - 如果为真的返回值
|
|
91
|
+
* @template N - 如果为假的返回值
|
|
92
|
+
*/
|
|
93
|
+
export type ExtendThen<B, T, Y, N = never> = B extends T ? Y : N;
|
|
94
|
+
/** 一个联合类型 B 中如果包含 T
|
|
95
|
+
* @template B - 基础类型
|
|
96
|
+
* @template T - 判断的目标类型
|
|
97
|
+
* @template Y - 如果为真的返回值
|
|
98
|
+
* @template N - 如果为假的返回值
|
|
99
|
+
*/
|
|
100
|
+
export type UnionInclude<B, T, Y, N = never> = B extends Exclude<B, T> ? Y : N;
|
|
101
|
+
/**排除可选字段 */
|
|
102
|
+
export type RequiredOnly<T> = {
|
|
103
|
+
[K in keyof T as UnionInclude<T[K], undefined, never, K>]: T[K];
|
|
104
|
+
};
|
|
105
|
+
/**添加前缀 */
|
|
106
|
+
export type WithPrefix<T, P extends string> = {
|
|
107
|
+
[K in (keyof T) as K extends string ? `${P}_${K}` : K]: T[K];
|
|
108
|
+
};
|
|
109
|
+
/**返回结果
|
|
110
|
+
* @template K - 类型键值
|
|
111
|
+
* @template V - 值
|
|
112
|
+
*/
|
|
113
|
+
export type Outcome<K extends Keyable, V> = {
|
|
114
|
+
/**状态类型 */
|
|
115
|
+
status: K;
|
|
116
|
+
/**值 */
|
|
117
|
+
result: V;
|
|
118
|
+
};
|
|
119
|
+
/**从联合 Outcome 中 根据 id 提取对应 value 的 type */
|
|
120
|
+
export type ExtractOutcome<T, K extends Keyable> = T extends Outcome<K, unknown> ? T : never;
|
|
91
121
|
export {};
|
package/dist/UtilInterfaces.js
CHANGED
|
@@ -1,14 +1,2 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.stringifyJToken = void 0;
|
|
4
|
-
/**将JToken转换为字符串
|
|
5
|
-
* @param token - 待转换的Token
|
|
6
|
-
* @param space - 插入的空格 数字为空格数量 默认为制表符\t
|
|
7
|
-
* @returns 转换完成的字符串
|
|
8
|
-
*/
|
|
9
|
-
function stringifyJToken(token, space = "\t") {
|
|
10
|
-
if (space == null)
|
|
11
|
-
space = undefined;
|
|
12
|
-
return JSON.stringify(token, null, space);
|
|
13
|
-
}
|
|
14
|
-
exports.stringifyJToken = stringifyJToken;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**完成 */
|
|
2
|
+
export declare const Completed: unique symbol;
|
|
3
|
+
export type Completed = typeof Completed;
|
|
4
|
+
/**失败 */
|
|
5
|
+
export declare const Failed: unique symbol;
|
|
6
|
+
export type Failed = typeof Failed;
|
|
7
|
+
/**终止 */
|
|
8
|
+
export declare const Terminated: unique symbol;
|
|
9
|
+
export type Terminated = typeof Terminated;
|
|
10
|
+
/**不存在 */
|
|
11
|
+
export declare const None: unique symbol;
|
|
12
|
+
export type None = typeof None;
|
|
13
|
+
/**超时 */
|
|
14
|
+
export declare const Timeout: unique symbol;
|
|
15
|
+
export type Timeout = typeof Timeout;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Timeout = exports.None = exports.Terminated = exports.Failed = exports.Completed = void 0;
|
|
4
|
+
/**完成 */
|
|
5
|
+
exports.Completed = Symbol("Completed");
|
|
6
|
+
/**失败 */
|
|
7
|
+
exports.Failed = Symbol("Failed");
|
|
8
|
+
/**终止 */
|
|
9
|
+
exports.Terminated = Symbol("Terminated");
|
|
10
|
+
/**不存在 */
|
|
11
|
+
exports.None = Symbol("None");
|
|
12
|
+
/**超时 */
|
|
13
|
+
exports.Timeout = Symbol("Timeout");
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from './UtilFunctions';
|
|
2
2
|
export * from './UtilInterfaces';
|
|
3
|
+
export * from './UtilSymbol';
|
|
3
4
|
export * from './UtilClass';
|
|
4
5
|
export * from './UtilCom';
|
|
5
6
|
export * from './UtilCodecs';
|
|
@@ -8,3 +9,4 @@ export * from './UtilDecorators';
|
|
|
8
9
|
export * from './UtilFileTools';
|
|
9
10
|
export * from './UtilLogger';
|
|
10
11
|
export * from './UtilFP';
|
|
12
|
+
export * from './QuickFunction';
|
package/dist/index.js
CHANGED
|
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./UtilFunctions"), exports);
|
|
18
18
|
__exportStar(require("./UtilInterfaces"), exports);
|
|
19
|
+
__exportStar(require("./UtilSymbol"), exports);
|
|
19
20
|
__exportStar(require("./UtilClass"), exports);
|
|
20
21
|
__exportStar(require("./UtilCom"), exports);
|
|
21
22
|
__exportStar(require("./UtilCodecs"), exports);
|
|
@@ -24,3 +25,4 @@ __exportStar(require("./UtilDecorators"), exports);
|
|
|
24
25
|
__exportStar(require("./UtilFileTools"), exports);
|
|
25
26
|
__exportStar(require("./UtilLogger"), exports);
|
|
26
27
|
__exportStar(require("./UtilFP"), exports);
|
|
28
|
+
__exportStar(require("./QuickFunction"), exports);
|
package/package.json
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ExtractOutcome, Keyable, Outcome } from "./UtilInterfaces";
|
|
2
|
+
|
|
3
|
+
/**创建一个outcome */
|
|
4
|
+
export function outcome<K extends Keyable,V> (key:K,value:V):Outcome<K,V>{
|
|
5
|
+
return {status:key,result:value}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**处理联合 简单值
|
|
9
|
+
* @param t - 目标值
|
|
10
|
+
* @param procObj - 所有可能的id组成的处理函数映射
|
|
11
|
+
* @returns 任意处理函数的返回值
|
|
12
|
+
*/
|
|
13
|
+
export function match<
|
|
14
|
+
T extends Keyable | Outcome<Keyable, unknown>,
|
|
15
|
+
P extends (T extends Keyable
|
|
16
|
+
? {[K in T]:(k:K)=>unknown}
|
|
17
|
+
: T extends Outcome<Keyable,unknown>
|
|
18
|
+
? {[K in T['status']]:(k:K,v:ExtractOutcome<T,K>['result'])=>unknown}
|
|
19
|
+
: never)>
|
|
20
|
+
(t:T, procObj:P):
|
|
21
|
+
P extends Record<any,(...args:any)=>any>
|
|
22
|
+
? {[K in keyof P]: ReturnType<P[K]>}[keyof P]
|
|
23
|
+
: never {
|
|
24
|
+
if (typeof t === 'string' || typeof t === 'number' || typeof t === 'symbol')
|
|
25
|
+
return (procObj as any)[t](t);
|
|
26
|
+
else
|
|
27
|
+
return (procObj as any)[t.status](t.status,t.result);
|
|
28
|
+
}
|
|
29
|
+
if(false){
|
|
30
|
+
let a:"asd"|"dsa" = null as any;
|
|
31
|
+
const r = match(a,{
|
|
32
|
+
"asd":(a)=>"ssa" as const,
|
|
33
|
+
"dsa":(b)=>"ssb" as const,
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
|
package/src/UtilCom.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { JObject, PromiseVerifyFn
|
|
1
|
+
import { JObject, PromiseVerifyFn } from "@src/UtilInterfaces";
|
|
2
2
|
import * as https from 'https';
|
|
3
3
|
import * as http from 'http';
|
|
4
4
|
import { SLogger } from "@src/UtilLogger";
|
|
@@ -21,7 +21,7 @@ function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:num
|
|
|
21
21
|
if(hasTimeLimit)
|
|
22
22
|
timeLimit*=1000
|
|
23
23
|
|
|
24
|
-
const jsonStr = stringifyJToken(json);
|
|
24
|
+
const jsonStr = UtilFunc.stringifyJToken(json);
|
|
25
25
|
const funcName = `s${posttype}Psot`;
|
|
26
26
|
|
|
27
27
|
return new Promise((resolve, rejecte)=>{
|
|
@@ -55,7 +55,7 @@ function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:num
|
|
|
55
55
|
}
|
|
56
56
|
try{
|
|
57
57
|
let obj = JSON.parse(resdata);
|
|
58
|
-
SLogger.http(funcName+" 接受信息:",stringifyJToken(obj));
|
|
58
|
+
SLogger.http(funcName+" 接受信息:",UtilFunc.stringifyJToken(obj));
|
|
59
59
|
resolve(obj);
|
|
60
60
|
return;
|
|
61
61
|
}
|
package/src/UtilDecorators.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { UtilFunc } from "./UtilFunctions";
|
|
1
2
|
import { SLogger } from "./UtilLogger";
|
|
2
3
|
|
|
3
4
|
type TDTg<T> =
|
|
@@ -6,6 +7,41 @@ type TDTg<T> =
|
|
|
6
7
|
type DTg = (target:Object, propertyKey:string, descriptor:PropertyDescriptor)=>
|
|
7
8
|
PropertyDescriptor;
|
|
8
9
|
|
|
10
|
+
/**用于打印方法运行时间
|
|
11
|
+
* @param flag - 时间标签
|
|
12
|
+
* @param suffixUID - 是否尾随uid
|
|
13
|
+
*/
|
|
14
|
+
export function LogTime(flag:string,suffixUID?:boolean):DTg{
|
|
15
|
+
return function (target, propertyKey, descriptor){
|
|
16
|
+
const originalMethod = descriptor.value;
|
|
17
|
+
descriptor.value = function(...args:any[]){
|
|
18
|
+
const uid = suffixUID ? UtilFunc.genUUID() : "";
|
|
19
|
+
SLogger.time(flag+uid);
|
|
20
|
+
let result = originalMethod.apply(this, args);
|
|
21
|
+
SLogger.timeEnd(flag+uid);
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
return descriptor;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**用于打印异步方法运行时间
|
|
28
|
+
* @param flag - 时间标签
|
|
29
|
+
* @param suffixUID - 是否尾随uid
|
|
30
|
+
*/
|
|
31
|
+
export function LogTimeAsync(flag:string,suffixUID?:boolean):DTg{
|
|
32
|
+
return function (target, propertyKey, descriptor){
|
|
33
|
+
const originalMethod = descriptor.value;
|
|
34
|
+
descriptor.value = async function(...args:any[]){
|
|
35
|
+
const uid = suffixUID ? UtilFunc.genUUID() : "";
|
|
36
|
+
SLogger.time(flag+uid);
|
|
37
|
+
let result = await originalMethod.apply(this, args);
|
|
38
|
+
SLogger.timeEnd(flag+uid);
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
return descriptor;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
9
45
|
/**用于打印方法的调用 */
|
|
10
46
|
export function LogCall():DTg{
|
|
11
47
|
return function (target, propertyKey, descriptor){
|
|
@@ -162,7 +198,7 @@ class Example {
|
|
|
162
198
|
}
|
|
163
199
|
@LogCall()
|
|
164
200
|
@Defer((a,b)=>null)
|
|
165
|
-
async myMethod1(num: number,ss:string) {
|
|
201
|
+
static async myMethod1(num: number,ss:string) {
|
|
166
202
|
return 312;
|
|
167
203
|
}
|
|
168
204
|
}
|