@zwa73/utils 1.0.167 → 1.0.169

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/UtilClass.js CHANGED
@@ -210,11 +210,10 @@ class Hbs {
210
210
  return '';
211
211
  });
212
212
  this.hbs.registerHelper('defli', (name, options) => {
213
- const rawvalue = options.fn();
213
+ const rawvalue = options.fn(this.context);
214
214
  const values = rawvalue
215
215
  .split('[[br]]')
216
- .map(value => value.trim())
217
- .map(value => this.hbs.compile(value)(this.context));
216
+ .map(value => value.trim());
218
217
  this.context[name] = values;
219
218
  return '';
220
219
  });
package/dist/UtilCom.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AnyString, JObject, ReqVerifyFn } from "./UtilInterfaces";
2
- import { RepeatPromiseOpt } from "./UtilFunctions";
2
+ import { RepeatPromiseOpt, RepeatPromiseResult } from "./UtilFunctions";
3
3
  export type ComPostOption = {
4
4
  /**请求域名 */
5
5
  hostname: string;
@@ -30,7 +30,7 @@ export declare namespace UtilCom {
30
30
  * @param timeLimit - 超时时间/秒 最小为10秒
31
31
  * @returns 结果 null 为未能成功接收
32
32
  */
33
- function shttpsPost(json: JObject, options: ComPostOption, timeLimit?: number): Promise<JObject | null>;
33
+ function httpsPost(json: JObject, options: ComPostOption, timeLimit?: number): Promise<JObject | null>;
34
34
  /**发送一个 http POST请求并接受数据
35
35
  * @async
36
36
  * @param json - 数据对象
@@ -38,7 +38,7 @@ export declare namespace UtilCom {
38
38
  * @param timeLimit - 超时时间/秒 最小为10秒
39
39
  * @returns 结果 null 为未能成功接收
40
40
  */
41
- function shttpPost(json: JObject, options: ComPostOption, timeLimit?: number): Promise<JObject | null>;
41
+ function httpPost(json: JObject, options: ComPostOption, timeLimit?: number): Promise<JObject | null>;
42
42
  /**重复一个 https POST请求并接受数据
43
43
  * @async
44
44
  * @param json - 数据对象
@@ -51,7 +51,7 @@ export declare namespace UtilCom {
51
51
  * @param opt.try_delay - 重试间隔 秒 默认0
52
52
  * @returns 结果 null 为未能成功接收
53
53
  */
54
- function shttpsRepeatPost(json: JObject, options: ComPostOption, verifyFn?: ReqVerifyFn<JObject | null>, opt?: RepeatPostOpt): Promise<JObject | null>;
54
+ function httpsRepeatPost(json: JObject, options: ComPostOption, verifyFn?: ReqVerifyFn<JObject | null>, opt?: RepeatPostOpt): Promise<RepeatPromiseResult<JObject | null> | null>;
55
55
  /**重复一个 http POST请求并接受数据
56
56
  * Object ()
57
57
  * @async
@@ -65,6 +65,6 @@ export declare namespace UtilCom {
65
65
  * @param opt.try_delay - 重试间隔 秒 默认0
66
66
  * @returns 结果 null 为未能成功接收
67
67
  */
68
- function shttpRepeatPost(json: JObject, options: ComPostOption, verifyFn?: ReqVerifyFn<JObject | null>, opt?: RepeatPostOpt): Promise<JObject | null>;
68
+ function httpRepeatPost(json: JObject, options: ComPostOption, verifyFn?: ReqVerifyFn<JObject | null>, opt?: RepeatPostOpt): Promise<RepeatPromiseResult<JObject | null> | null>;
69
69
  }
70
70
  export {};
package/dist/UtilCom.js CHANGED
@@ -38,7 +38,7 @@ var UtilCom;
38
38
  * @param timeLimit - 超时时间/秒 最小为10秒
39
39
  * @returns 结果 null 为未能成功接收
40
40
  */
41
- function sPost(posttype, json, options, timeLimit) {
41
+ function post(posttype, json, options, timeLimit) {
42
42
  //转换为毫秒
43
43
  const hasTimeLimit = (timeLimit ? timeLimit >= 10 : false);
44
44
  if (hasTimeLimit && timeLimit != undefined)
@@ -119,10 +119,10 @@ var UtilCom;
119
119
  * @param timeLimit - 超时时间/秒 最小为10秒
120
120
  * @returns 结果 null 为未能成功接收
121
121
  */
122
- function shttpsPost(json, options, timeLimit) {
123
- return sPost("https", json, options, timeLimit);
122
+ function httpsPost(json, options, timeLimit) {
123
+ return post("https", json, options, timeLimit);
124
124
  }
125
- UtilCom.shttpsPost = shttpsPost;
125
+ UtilCom.httpsPost = httpsPost;
126
126
  /**发送一个 http POST请求并接受数据
127
127
  * @async
128
128
  * @param json - 数据对象
@@ -130,10 +130,10 @@ var UtilCom;
130
130
  * @param timeLimit - 超时时间/秒 最小为10秒
131
131
  * @returns 结果 null 为未能成功接收
132
132
  */
133
- function shttpPost(json, options, timeLimit) {
134
- return sPost("http", json, options, timeLimit);
133
+ function httpPost(json, options, timeLimit) {
134
+ return post("http", json, options, timeLimit);
135
135
  }
136
- UtilCom.shttpPost = shttpPost;
136
+ UtilCom.httpPost = httpPost;
137
137
  /**通用重复post处理
138
138
  * @async
139
139
  * @param posttype - post类型
@@ -147,11 +147,11 @@ var UtilCom;
147
147
  * @param opt.try_delay - 重试间隔 秒 默认0
148
148
  * @returns 结果 null 为未能成功接收
149
149
  */
150
- async function sRepeatPost(posttype, json, options, verifyFn, opt = {}) {
150
+ async function repeatPost(posttype, json, options, verifyFn, opt = {}) {
151
151
  opt.count = opt.count ?? 3;
152
152
  opt.tryInterval = opt.tryInterval ?? 180;
153
153
  opt.tryDelay = opt.tryDelay ?? 1;
154
- const procFn = () => sPost(posttype, json, options, opt.postTimeLimit);
154
+ const procFn = () => post(posttype, json, options, opt.postTimeLimit);
155
155
  return UtilFunctions_1.UtilFunc.repeatPromise(procFn, verifyFn, opt);
156
156
  }
157
157
  /**重复一个 https POST请求并接受数据
@@ -166,10 +166,10 @@ var UtilCom;
166
166
  * @param opt.try_delay - 重试间隔 秒 默认0
167
167
  * @returns 结果 null 为未能成功接收
168
168
  */
169
- function shttpsRepeatPost(json, options, verifyFn, opt) {
170
- return sRepeatPost("https", json, options, verifyFn, opt);
169
+ function httpsRepeatPost(json, options, verifyFn, opt) {
170
+ return repeatPost("https", json, options, verifyFn, opt);
171
171
  }
172
- UtilCom.shttpsRepeatPost = shttpsRepeatPost;
172
+ UtilCom.httpsRepeatPost = httpsRepeatPost;
173
173
  /**重复一个 http POST请求并接受数据
174
174
  * Object ()
175
175
  * @async
@@ -183,8 +183,8 @@ var UtilCom;
183
183
  * @param opt.try_delay - 重试间隔 秒 默认0
184
184
  * @returns 结果 null 为未能成功接收
185
185
  */
186
- function shttpRepeatPost(json, options, verifyFn, opt) {
187
- return sRepeatPost("http", json, options, verifyFn, opt);
186
+ function httpRepeatPost(json, options, verifyFn, opt) {
187
+ return repeatPost("http", json, options, verifyFn, opt);
188
188
  }
189
- UtilCom.shttpRepeatPost = shttpRepeatPost;
189
+ UtilCom.httpRepeatPost = httpRepeatPost;
190
190
  })(UtilCom || (exports.UtilCom = UtilCom = {}));
@@ -32,6 +32,11 @@ 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
+ export type RepeatPromiseResult<T> = {
37
+ completed: T;
38
+ pending: Promise<T | None>[];
39
+ };
35
40
  /**遍历对象的回调函数
36
41
  * @param key - 字段名
37
42
  * @param value - 字段值
@@ -105,12 +110,12 @@ export declare class UtilFunc {
105
110
  * @param procFn - 发起函数
106
111
  * @param verifyFn - 验证函数
107
112
  * @param opt - 可选参数
108
- * @param opt.count - 重试次数 默认3
109
- * @param opt.timeout - 超时时间/秒 默认180 最小为5秒
110
- * @param opt.interval - 重试间隔时间 默认0
113
+ * @param opt.count - 重试次数 默认3
114
+ * @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
115
+ * @param opt.tryDelay - 重试间隔时间 默认0
111
116
  * @returns 结果 null 为全部失败/超时
112
117
  */
113
- static repeatPromise<T>(procFn: () => Promise<T>, verifyFn?: ReqVerifyFn<T>, opt?: RepeatPromiseOpt): Promise<T | null>;
118
+ static repeatPromise<T>(procFn: () => Promise<T>, verifyFn?: ReqVerifyFn<T>, opt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<T> | null>;
114
119
  /**创建一个限时的Promise
115
120
  * @param func - 处理函数
116
121
  * @param timeLimit - 毫秒限时
@@ -170,9 +170,9 @@ class UtilFunc {
170
170
  * @param procFn - 发起函数
171
171
  * @param verifyFn - 验证函数
172
172
  * @param opt - 可选参数
173
- * @param opt.count - 重试次数 默认3
174
- * @param opt.timeout - 超时时间/秒 默认180 最小为5秒
175
- * @param opt.interval - 重试间隔时间 默认0
173
+ * @param opt.count - 重试次数 默认3
174
+ * @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
175
+ * @param opt.tryDelay - 重试间隔时间 默认0
176
176
  * @returns 结果 null 为全部失败/超时
177
177
  */
178
178
  static async repeatPromise(procFn, verifyFn, opt = {}) {
@@ -247,7 +247,26 @@ class UtilFunc {
247
247
  },
248
248
  });
249
249
  if (result !== UtilSymbol_1.None)
250
- return result;
250
+ return {
251
+ completed: result,
252
+ pending: plist
253
+ .filter((p, i) => i != postresult.index)
254
+ .filter(p => p != UtilFunc.getNeverResolvedPromise())
255
+ .map(async (p) => {
256
+ const curObj = await p;
257
+ if (curObj.status == UtilSymbol_1.Success) {
258
+ const outres = curObj.result;
259
+ if (outres.stat == UtilSymbol_1.Success)
260
+ return outres.result;
261
+ }
262
+ if (curObj.status == UtilSymbol_1.Timeout) {
263
+ const outres = await curObj.result;
264
+ if (outres.stat == UtilSymbol_1.Success)
265
+ return outres.result;
266
+ }
267
+ return UtilSymbol_1.None;
268
+ }),
269
+ };
251
270
  }
252
271
  //全部失败或超时则返回null
253
272
  UtilLogger_1.SLogger.warn(`${count} 次 repeatPromise 尝试均失败`);
@@ -273,17 +292,19 @@ class UtilFunc {
273
292
  if (clearTimer)
274
293
  clearTimer();
275
294
  });
276
- const timerP = timeLimit
277
- ? new Promise((resolve) => {
278
- const timer = setTimeout(() => resolve(UtilFunc.outcome(UtilSymbol_1.Timeout, procer)), timeLimit); //无限制则无限时间
295
+ if (timeLimit) {
296
+ const timerP = new Promise((timeResolve) => {
297
+ const timer = setTimeout(() => timeResolve(UtilFunc.outcome(UtilSymbol_1.Timeout, procer)), timeLimit);
279
298
  clearTimer = () => {
280
- resolve(UtilFunc.outcome(UtilSymbol_1.Timeout, procer));
299
+ timeResolve(UtilFunc.outcome(UtilSymbol_1.Timeout, procer));
281
300
  clearInterval(timer);
282
301
  };
283
- }) //未定义时间限制则无限
284
- : UtilFunc.getNeverResolvedPromise();
285
- const result = Promise.race([procerP, timerP]);
286
- reslove(result);
302
+ });
303
+ reslove(Promise.race([procerP, timerP]));
304
+ }
305
+ else {
306
+ reslove(procerP);
307
+ }
287
308
  });
288
309
  }
289
310
  /**对对象的每个属性应用映射函数,并返回一个新的对象。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.167",
3
+ "version": "1.0.169",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/UtilClass.ts CHANGED
@@ -67,7 +67,7 @@ export class Stream<T> implements Iterable<T>{
67
67
  //轮询平均分
68
68
  'average':()=>{
69
69
  for (let i = 0; i < count; i++) {
70
- const clist = [];
70
+ const clist:T[] = [];
71
71
  for (let j = i; j < size; j += count)
72
72
  clist.push(this._list[j]);
73
73
  result.push(clist);
@@ -218,12 +218,11 @@ export class Hbs<T extends Record<Keyable,any> = Record<Keyable,any>>{
218
218
  });
219
219
 
220
220
  this.hbs.registerHelper('defli', (name,options)=> {
221
- const rawvalue = options.fn() as string;
221
+ const rawvalue = options.fn(this.context) as string;
222
222
 
223
223
  const values = rawvalue
224
224
  .split('[[br]]')
225
- .map(value => value.trim())
226
- .map(value => this.hbs.compile(value)(this.context));
225
+ .map(value => value.trim());
227
226
  (this.context as any)[name] = values;
228
227
  return '';
229
228
  });
package/src/UtilCom.ts CHANGED
@@ -2,7 +2,7 @@ import { AnyString, JObject, ReqVerifyFn } from "@src/UtilInterfaces";
2
2
  import * as https from 'https';
3
3
  import * as http from 'http';
4
4
  import { SLogger } from "@src/UtilLogger";
5
- import { RepeatPromiseOpt, UtilFunc } from "@src/UtilFunctions";
5
+ import { RepeatPromiseOpt, RepeatPromiseResult, UtilFunc } from "@src/UtilFunctions";
6
6
 
7
7
  export type ComPostOption = {
8
8
  /**请求域名 */
@@ -37,7 +37,7 @@ export namespace UtilCom{
37
37
  * @param timeLimit - 超时时间/秒 最小为10秒
38
38
  * @returns 结果 null 为未能成功接收
39
39
  */
40
- function sPost(posttype:"http"|"https",json:JObject,options:ComPostOption,timeLimit?:number):Promise<JObject|null>{
40
+ function post(posttype:"http"|"https",json:JObject,options:ComPostOption,timeLimit?:number):Promise<JObject|null>{
41
41
  //转换为毫秒
42
42
  const hasTimeLimit = (timeLimit ? timeLimit>=10 : false );
43
43
  if(hasTimeLimit && timeLimit!=undefined) timeLimit*=1000;
@@ -125,8 +125,8 @@ function sPost(posttype:"http"|"https",json:JObject,options:ComPostOption,timeLi
125
125
  * @param timeLimit - 超时时间/秒 最小为10秒
126
126
  * @returns 结果 null 为未能成功接收
127
127
  */
128
- export function shttpsPost(json:JObject,options:ComPostOption,timeLimit?:number):Promise<JObject|null>{
129
- return sPost("https",json,options,timeLimit);
128
+ export function httpsPost(json:JObject,options:ComPostOption,timeLimit?:number):Promise<JObject|null>{
129
+ return post("https",json,options,timeLimit);
130
130
  }
131
131
 
132
132
  /**发送一个 http POST请求并接受数据
@@ -136,8 +136,8 @@ export function shttpsPost(json:JObject,options:ComPostOption,timeLimit?:number)
136
136
  * @param timeLimit - 超时时间/秒 最小为10秒
137
137
  * @returns 结果 null 为未能成功接收
138
138
  */
139
- export function shttpPost(json:JObject,options:ComPostOption,timeLimit?:number):Promise<JObject|null>{
140
- return sPost("http",json,options,timeLimit);
139
+ export function httpPost(json:JObject,options:ComPostOption,timeLimit?:number):Promise<JObject|null>{
140
+ return post("http",json,options,timeLimit);
141
141
  }
142
142
 
143
143
 
@@ -155,12 +155,13 @@ export function shttpPost(json:JObject,options:ComPostOption,timeLimit?:number):
155
155
  * @param opt.try_delay - 重试间隔 秒 默认0
156
156
  * @returns 结果 null 为未能成功接收
157
157
  */
158
- async function sRepeatPost(posttype:"http"|"https",json:JObject,options:ComPostOption,verifyFn?:ReqVerifyFn<JObject|null>,opt:RepeatPostOpt={}):Promise<JObject|null>{
158
+ async function repeatPost(posttype:"http"|"https",json:JObject,options:ComPostOption,verifyFn?:ReqVerifyFn<JObject|null>,opt:RepeatPostOpt={}):
159
+ Promise<RepeatPromiseResult<JObject|null>|null>{
159
160
  opt.count = opt.count??3;
160
161
  opt.tryInterval = opt.tryInterval??180;
161
162
  opt.tryDelay = opt.tryDelay??1;
162
163
 
163
- const procFn = ()=>sPost(posttype,json,options,opt.postTimeLimit);
164
+ const procFn = ()=>post(posttype,json,options,opt.postTimeLimit);
164
165
  return UtilFunc.repeatPromise(procFn,verifyFn,opt);
165
166
  }
166
167
 
@@ -177,8 +178,9 @@ async function sRepeatPost(posttype:"http"|"https",json:JObject,options:ComPostO
177
178
  * @param opt.try_delay - 重试间隔 秒 默认0
178
179
  * @returns 结果 null 为未能成功接收
179
180
  */
180
- export function shttpsRepeatPost(json:JObject,options:ComPostOption,verifyFn?:ReqVerifyFn<JObject|null>,opt?:RepeatPostOpt):Promise<JObject|null>{
181
- return sRepeatPost("https",json,options,verifyFn,opt);
181
+ export function httpsRepeatPost(json:JObject,options:ComPostOption,verifyFn?:ReqVerifyFn<JObject|null>,opt?:RepeatPostOpt):
182
+ Promise<RepeatPromiseResult<JObject|null>|null>{
183
+ return repeatPost("https",json,options,verifyFn,opt);
182
184
  }
183
185
 
184
186
  /**重复一个 http POST请求并接受数据
@@ -194,8 +196,9 @@ export function shttpsRepeatPost(json:JObject,options:ComPostOption,verifyFn?:Re
194
196
  * @param opt.try_delay - 重试间隔 秒 默认0
195
197
  * @returns 结果 null 为未能成功接收
196
198
  */
197
- export function shttpRepeatPost(json:JObject,options:ComPostOption,verifyFn?:ReqVerifyFn<JObject|null>,opt?:RepeatPostOpt):Promise<JObject|null>{
198
- return sRepeatPost("http",json,options,verifyFn,opt);
199
+ export function httpRepeatPost(json:JObject,options:ComPostOption,verifyFn?:ReqVerifyFn<JObject|null>,opt?:RepeatPostOpt):
200
+ Promise<RepeatPromiseResult<JObject|null>|null>{
201
+ return repeatPost("http",json,options,verifyFn,opt);
199
202
  }
200
203
 
201
204
  }
@@ -56,6 +56,13 @@ type PromiseResult<T> = {
56
56
  index:number;
57
57
  };
58
58
 
59
+ /**完成的重试请求 */
60
+ export type RepeatPromiseResult<T> = {
61
+ completed:T;
62
+ pending:Promise<T|None>[];
63
+ }
64
+
65
+
59
66
  /**遍历对象的回调函数
60
67
  * @param key - 字段名
61
68
  * @param value - 字段值
@@ -216,13 +223,14 @@ static getNeverResolvedPromise<T>():Promise<T>{
216
223
  * @param procFn - 发起函数
217
224
  * @param verifyFn - 验证函数
218
225
  * @param opt - 可选参数
219
- * @param opt.count - 重试次数 默认3
220
- * @param opt.timeout - 超时时间/秒 默认180 最小为5秒
221
- * @param opt.interval - 重试间隔时间 默认0
226
+ * @param opt.count - 重试次数 默认3
227
+ * @param opt.tryInterval - 超时时间/秒 默认180 最小为5秒
228
+ * @param opt.tryDelay - 重试间隔时间 默认0
222
229
  * @returns 结果 null 为全部失败/超时
223
230
  */
224
231
  @LogTimeAsync("repeatPromise ",true)
225
- static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,opt:RepeatPromiseOpt = {}):Promise<T|null>{
232
+ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,opt:RepeatPromiseOpt = {}):
233
+ Promise<RepeatPromiseResult<T>|null>{
226
234
  opt.count = opt.count??3;
227
235
  opt.tryInterval = opt.tryInterval??180;
228
236
  let {count,tryInterval} = opt;
@@ -300,7 +308,24 @@ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,opt
300
308
  return None;
301
309
  },
302
310
  });
303
- if(result !== None) return result;
311
+ if(result !== None) return {
312
+ completed:result,
313
+ pending:plist
314
+ .filter((p,i)=>i!=postresult.index)
315
+ .filter(p=>p!=UtilFunc.getNeverResolvedPromise())
316
+ .map(async p=>{
317
+ const curObj = await p;
318
+ if(curObj.status==Success){
319
+ const outres = curObj.result;
320
+ if(outres.stat==Success) return outres.result;
321
+ }
322
+ if(curObj.status==Timeout){
323
+ const outres = await curObj.result;
324
+ if(outres.stat==Success) return outres.result;
325
+ }
326
+ return None;
327
+ }),
328
+ };
304
329
  }
305
330
  //全部失败或超时则返回null
306
331
  SLogger.warn(`${count} 次 repeatPromise 尝试均失败`);
@@ -329,21 +354,21 @@ static timelimitPromise<T>
329
354
  if(clearTimer) clearTimer();
330
355
  });
331
356
 
332
- const timerP = timeLimit
333
- ? new Promise<TimeoutOut<T>>((resolve)=>{
357
+ if(timeLimit){
358
+ const timerP = new Promise<TimeoutOut<T>>((timeResolve)=>{
334
359
  const timer = setTimeout(()=>
335
- resolve(UtilFunc.outcome(Timeout,procer))
336
- ,timeLimit);//无限制则无限时间
360
+ timeResolve(UtilFunc.outcome(Timeout,procer)), timeLimit);
337
361
 
338
362
  clearTimer = ()=>{
339
- resolve(UtilFunc.outcome(Timeout,procer))
363
+ timeResolve(UtilFunc.outcome(Timeout,procer))
340
364
  clearInterval(timer)
341
365
  }
342
- })//未定义时间限制则无限
343
- : UtilFunc.getNeverResolvedPromise<TimeoutOut<T>>();
366
+ });
344
367
 
345
- const result = Promise.race([procerP,timerP]);
346
- reslove(result);
368
+ reslove(Promise.race([procerP,timerP]));
369
+ }else{
370
+ reslove(procerP);
371
+ }
347
372
  })
348
373
  }
349
374