@zwa73/utils 1.0.97 → 1.0.100

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,2 +1,2 @@
1
1
  import { UtilFunc } from "./UtilFunctions";
2
- export declare const outcome: typeof UtilFunc.outcome, matchProc: typeof UtilFunc.matchProc, sucesProc: typeof UtilFunc.sucesProc, isFailed: typeof UtilFunc.isFailed, isSafeNumber: typeof UtilFunc.isSafeNumber, isSuccess: typeof UtilFunc.isSuccess, likeNone: typeof UtilFunc.likeNone, expect: typeof UtilFunc.expect, assertType: typeof UtilFunc.assertType, assertLiteral: typeof UtilFunc.assertLiteral, deepClone: typeof UtilFunc.deepClone, sleep: typeof UtilFunc.sleep, stringifyJToken: typeof UtilFunc.stringifyJToken, getTime: typeof UtilFunc.getTime, mapEntries: typeof UtilFunc.mapEntries, composeMixinable: typeof UtilFunc.composeMixinable, dedent: typeof UtilFunc.dedent;
2
+ export declare const outcome: typeof UtilFunc.outcome, matchProc: typeof UtilFunc.matchProc, sucesProc: typeof UtilFunc.sucesProc, isFailed: typeof UtilFunc.isFailed, isSafeNumber: typeof UtilFunc.isSafeNumber, isSuccess: typeof UtilFunc.isSuccess, likeNone: typeof UtilFunc.likeNone, expect: typeof UtilFunc.expect, assertType: typeof UtilFunc.assertType, assertLiteral: typeof UtilFunc.assertLiteral, deepClone: typeof UtilFunc.deepClone, sleep: typeof UtilFunc.sleep, stringifyJToken: typeof UtilFunc.stringifyJToken, getTime: typeof UtilFunc.getTime, mapEntries: typeof UtilFunc.mapEntries, composeMixinable: typeof UtilFunc.composeMixinable, dedent: typeof UtilFunc.dedent, throwError: typeof UtilFunc.throwError;
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dedent = exports.composeMixinable = exports.mapEntries = exports.getTime = exports.stringifyJToken = exports.sleep = exports.deepClone = exports.assertLiteral = exports.assertType = exports.expect = exports.likeNone = exports.isSuccess = exports.isSafeNumber = exports.isFailed = exports.sucesProc = exports.matchProc = exports.outcome = void 0;
3
+ exports.throwError = exports.dedent = exports.composeMixinable = exports.mapEntries = exports.getTime = exports.stringifyJToken = exports.sleep = exports.deepClone = exports.assertLiteral = exports.assertType = exports.expect = exports.likeNone = exports.isSuccess = exports.isSafeNumber = exports.isFailed = exports.sucesProc = exports.matchProc = exports.outcome = void 0;
4
4
  const UtilFunctions_1 = require("./UtilFunctions");
5
5
  const UtilSymbol_1 = require("./UtilSymbol");
6
- exports.outcome = UtilFunctions_1.UtilFunc.outcome, exports.matchProc = UtilFunctions_1.UtilFunc.matchProc, exports.sucesProc = UtilFunctions_1.UtilFunc.sucesProc, exports.isFailed = UtilFunctions_1.UtilFunc.isFailed, exports.isSafeNumber = UtilFunctions_1.UtilFunc.isSafeNumber, exports.isSuccess = UtilFunctions_1.UtilFunc.isSuccess, exports.likeNone = UtilFunctions_1.UtilFunc.likeNone, exports.expect = UtilFunctions_1.UtilFunc.expect, exports.assertType = UtilFunctions_1.UtilFunc.assertType, exports.assertLiteral = UtilFunctions_1.UtilFunc.assertLiteral, exports.deepClone = UtilFunctions_1.UtilFunc.deepClone, exports.sleep = UtilFunctions_1.UtilFunc.sleep, exports.stringifyJToken = UtilFunctions_1.UtilFunc.stringifyJToken, exports.getTime = UtilFunctions_1.UtilFunc.getTime, exports.mapEntries = UtilFunctions_1.UtilFunc.mapEntries, exports.composeMixinable = UtilFunctions_1.UtilFunc.composeMixinable, exports.dedent = UtilFunctions_1.UtilFunc.dedent;
6
+ exports.outcome = UtilFunctions_1.UtilFunc.outcome, exports.matchProc = UtilFunctions_1.UtilFunc.matchProc, exports.sucesProc = UtilFunctions_1.UtilFunc.sucesProc, exports.isFailed = UtilFunctions_1.UtilFunc.isFailed, exports.isSafeNumber = UtilFunctions_1.UtilFunc.isSafeNumber, exports.isSuccess = UtilFunctions_1.UtilFunc.isSuccess, exports.likeNone = UtilFunctions_1.UtilFunc.likeNone, exports.expect = UtilFunctions_1.UtilFunc.expect, exports.assertType = UtilFunctions_1.UtilFunc.assertType, exports.assertLiteral = UtilFunctions_1.UtilFunc.assertLiteral, exports.deepClone = UtilFunctions_1.UtilFunc.deepClone, exports.sleep = UtilFunctions_1.UtilFunc.sleep, exports.stringifyJToken = UtilFunctions_1.UtilFunc.stringifyJToken, exports.getTime = UtilFunctions_1.UtilFunc.getTime, exports.mapEntries = UtilFunctions_1.UtilFunc.mapEntries, exports.composeMixinable = UtilFunctions_1.UtilFunc.composeMixinable, exports.dedent = UtilFunctions_1.UtilFunc.dedent, exports.throwError = UtilFunctions_1.UtilFunc.throwError;
7
7
  if (false) {
8
8
  let aaser = (0, exports.assertLiteral)({ a: 1 });
9
9
  let a = null;
@@ -1,21 +1,3 @@
1
- /**函数组合器 */
2
- export declare class Composer<Result, PreArg = Result> {
3
- /**组合函数列表 */
4
- private funcs;
5
- private constructor();
6
- /**添加单个参数与返回值不同的函数 */
7
- static push<Result, Arg>(func: (arg: Arg) => Result): Composer<Result, Arg>;
8
- /**添加多个参数与返回值相同的函数 */
9
- static push<Result>(func: ((arg: Result) => Result), ...funcs: ((arg: Result) => Result)[]): Composer<Result>;
10
- /**添加单个参数与返回值不同的函数 */
11
- push<T>(func: (arg: T) => PreArg): Composer<Result, T>;
12
- /**添加多个参数与返回值相同的函数 */
13
- push(func: ((arg: Result) => Result), ...funcs: ((arg: PreArg) => PreArg)[]): Composer<Result, PreArg>;
14
- /**组合函数 */
15
- compose(): (arg: PreArg) => Result;
16
- /**直接调用 */
17
- invoke(arg: PreArg): Result;
18
- }
19
1
  /**函数管道器 */
20
2
  export declare class Piper<Arg, Result = Arg> {
21
3
  /**管道函数列表 */
package/dist/UtilClass.js CHANGED
@@ -1,32 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Stream = exports.Piper = exports.Composer = void 0;
4
- /**函数组合器 */
5
- class Composer {
6
- /**组合函数列表 */
7
- funcs = [];
8
- constructor() { }
9
- ;
10
- static push(...funcs) {
11
- const newComposer = new Composer();
12
- newComposer.funcs = [...funcs];
13
- return newComposer;
14
- }
15
- push(...funcs) {
16
- const newComposer = new Composer();
17
- newComposer.funcs = [...this.funcs, ...funcs];
18
- return newComposer;
19
- }
20
- /**组合函数 */
21
- compose() {
22
- return (arg) => this.funcs.reduceRight((value, func) => func(value), arg);
23
- }
24
- /**直接调用 */
25
- invoke(arg) {
26
- return this.funcs.reduceRight((value, func) => func(value), arg);
27
- }
28
- }
29
- exports.Composer = Composer;
3
+ exports.Stream = exports.Piper = void 0;
30
4
  /**函数管道器 */
31
5
  class Piper {
32
6
  /**管道函数列表 */
@@ -84,8 +84,7 @@ function LogErr() {
84
84
  return result;
85
85
  }
86
86
  catch (err) {
87
- UtilLogger_1.SLogger.warn(`函数出现错误: ${propertyKey}(${args}): ${err}`);
88
- throw err;
87
+ UtilFunctions_1.UtilFunc.throwError(`函数出现错误: ${propertyKey}(${args}): ${err}`, 'warn');
89
88
  }
90
89
  };
91
90
  return descriptor;
@@ -102,8 +101,7 @@ function LogErrAsync() {
102
101
  return result;
103
102
  }
104
103
  catch (err) {
105
- UtilLogger_1.SLogger.warn(`函数出现错误: ${propertyKey}(${args}): ${err}`);
106
- throw err;
104
+ UtilFunctions_1.UtilFunc.throwError(`函数出现错误: ${propertyKey}(${args}): ${err}`, 'warn');
107
105
  }
108
106
  };
109
107
  return descriptor;
@@ -217,14 +217,17 @@ var UtilFT;
217
217
  * @returns 文件绝对路径数组
218
218
  */
219
219
  function fileSearchGlob(globPattern, ignore) {
220
- return (0, glob_1.globSync)(globPattern, { ignore, absolute: true });
220
+ const fixedPath = typeof globPattern === "string"
221
+ ? path.posix.normalize(globPattern)
222
+ : globPattern.map((p) => path.posix.normalize(p));
223
+ return (0, glob_1.globSync)(fixedPath, { ignore, absolute: true });
221
224
  }
222
225
  UtilFT.fileSearchGlob = fileSearchGlob;
223
226
  /**
224
227
  * @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
225
228
  */
226
229
  function fileSearch(...patams) {
227
- throw "请使用 fileSearchRegex 或 fileSearchGlob";
230
+ UtilFunctions_1.UtilFunc.throwError("请使用 fileSearchRegex 或 fileSearchGlob", 'fatal');
228
231
  }
229
232
  UtilFT.fileSearch = fileSearch;
230
233
  /**是一个有效的文件路径
@@ -1,4 +1,5 @@
1
1
  import { AnyFunc, ComposedClass, ComposedMixinable, ExtractOutcome, IJData, JObject, JToken, Keyable, LiteralCheck, Matchable, MatchableFlag, Mixinable, Outcome, PromiseVerifyFn, UnionToIntersection } from "./UtilInterfaces";
2
+ import { LogLevel } from "./UtilLogger";
2
3
  import { FailedLike, None, StatusSymbol, Success, SuccessLike, Timeout } from "./UtilSymbol";
3
4
  type SuccessOut<T> = Outcome<Success, T>;
4
5
  type TimeoutOut<T> = Outcome<Timeout, Promise<T>>;
@@ -190,5 +191,11 @@ export declare class UtilFunc {
190
191
  * // World!
191
192
  */
192
193
  static dedent(strings: TemplateStringsArray, ...values: any[]): string;
194
+ /**抛出错误
195
+ * @param message - 错误信息
196
+ * @param lvl - 日志等级
197
+ * @param opt - 额外参数
198
+ */
199
+ static throwError(message: string, lvl?: LogLevel, opt?: {}): never;
193
200
  }
194
201
  export {};
@@ -389,9 +389,7 @@ class UtilFunc {
389
389
  }
390
390
  else if (procObj[t.status])
391
391
  return procObj[t.status](t.status, t.result);
392
- const err = new Error(`matchProc 传入了一个预料之外的值: ${String(t)}`);
393
- UtilLogger_1.SLogger.fatal(err);
394
- throw err;
392
+ UtilFunc.throwError(`matchProc 传入了一个预料之外的值: ${String(t)}`, 'fatal');
395
393
  }
396
394
  /**根据典型的成功或失败状态运行函数
397
395
  * @param t - 目标值
@@ -449,7 +447,7 @@ class UtilFunc {
449
447
  */
450
448
  static expect(t, errLog) {
451
449
  if (t === UtilSymbol_1.None)
452
- throw new Error(errLog);
450
+ UtilFunc.throwError(errLog ?? "expect验证了一个None值", "error");
453
451
  return t;
454
452
  }
455
453
  /**验证一个变量的类型是否为 T
@@ -517,6 +515,22 @@ class UtilFunc {
517
515
  const minIndent = Math.min(...lines.filter(line => line.trim() !== '').map(line => line.search(/\S/)));
518
516
  return lines.map(line => line.slice(minIndent)).join('\n');
519
517
  }
518
+ /**抛出错误
519
+ * @param message - 错误信息
520
+ * @param lvl - 日志等级
521
+ * @param opt - 额外参数
522
+ */
523
+ static throwError(message, lvl, opt) {
524
+ const e = new Error(message);
525
+ const stackLines = e.stack.split('\n');
526
+ e.stack = [stackLines[0]].concat(stackLines.slice(2)).join('\n'); // 移除第一行的堆栈信息
527
+ Object.assign(e, opt);
528
+ if (lvl) {
529
+ UtilLogger_1.SLogger.log(lvl, message);
530
+ UtilLogger_1.SLogger.log(lvl, e.stack);
531
+ }
532
+ throw e;
533
+ }
520
534
  }
521
535
  exports.UtilFunc = UtilFunc;
522
536
  __decorate([
@@ -234,7 +234,7 @@ class SLogger {
234
234
  * @returns 格式化的时间字符串
235
235
  */
236
236
  timeEnd(flag, level = "info") {
237
- let start = SLogger.timeTable[flag];
237
+ const start = SLogger.timeTable[flag];
238
238
  if (start == null) {
239
239
  this.warn("SLogger.timeEnd 错误 flag:" + flag + " 不存在");
240
240
  return null;
@@ -304,7 +304,7 @@ class SLogger {
304
304
  * @returns 自身
305
305
  */
306
306
  static log(level, ...messages) {
307
- level = level || "silly";
307
+ level = level ?? "silly";
308
308
  this.defaultInstance.log(level, ...messages);
309
309
  return this.defaultInstance;
310
310
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.97",
3
+ "version": "1.0.100",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -20,6 +20,7 @@ export const {
20
20
  mapEntries,
21
21
  composeMixinable,
22
22
  dedent,
23
+ throwError,
23
24
  } = UtilFunc;
24
25
 
25
26
  if(false){
package/src/UtilClass.ts CHANGED
@@ -1,36 +1,3 @@
1
- /**函数组合器 */
2
- export class Composer<Result,PreArg = Result> {
3
- /**组合函数列表 */
4
- private funcs: Function[] = [];
5
- private constructor(){};
6
- /**添加单个参数与返回值不同的函数 */
7
- public static push<Result,Arg>(func: (arg: Arg) => Result): Composer<Result,Arg>;
8
- /**添加多个参数与返回值相同的函数 */
9
- public static push<Result>(func:((arg: Result) => Result), ...funcs: ((arg: Result) => Result)[]): Composer<Result>;
10
- public static push(...funcs: Function[]){
11
- const newComposer = new Composer<any>();
12
- newComposer.funcs = [...funcs];
13
- return newComposer;
14
- }
15
-
16
- /**添加单个参数与返回值不同的函数 */
17
- public push<T>(func: (arg: T) => PreArg): Composer<Result,T>;
18
- /**添加多个参数与返回值相同的函数 */
19
- public push(func:((arg: Result) => Result), ...funcs: ((arg: PreArg) => PreArg)[]): Composer<Result,PreArg>;
20
- public push(...funcs: Function[]): Composer<Result,any> {
21
- const newComposer = new Composer<Result>();
22
- newComposer.funcs = [...this.funcs, ...funcs];
23
- return newComposer;
24
- }
25
- /**组合函数 */
26
- public compose():(arg:PreArg)=>Result {
27
- return (arg:PreArg) => this.funcs.reduceRight((value, func) => func(value), arg) as any as Result;
28
- }
29
- /**直接调用 */
30
- public invoke(arg:PreArg):Result {
31
- return this.funcs.reduceRight((value, func) => func(value), arg) as any as Result;
32
- }
33
- }
34
1
  /**函数管道器 */
35
2
  export class Piper<Arg, Result = Arg> {
36
3
  /**管道函数列表 */
@@ -78,8 +78,7 @@ export function LogErr():DTg{
78
78
  const result = originalMethod.apply(this, args);
79
79
  return result;
80
80
  } catch(err) {
81
- SLogger.warn(`函数出现错误: ${propertyKey}(${args}): ${err}`);
82
- throw err;
81
+ UtilFunc.throwError(`函数出现错误: ${propertyKey}(${args}): ${err}`,'warn');
83
82
  }
84
83
  }
85
84
  return descriptor;
@@ -95,8 +94,7 @@ export function LogErrAsync():DTg{
95
94
  const result = await originalMethod.apply(this, args);
96
95
  return result;
97
96
  } catch(err) {
98
- SLogger.warn(`函数出现错误: ${propertyKey}(${args}): ${err}`);
99
- throw err;
97
+ UtilFunc.throwError(`函数出现错误: ${propertyKey}(${args}): ${err}`,'warn');
100
98
  }
101
99
  }
102
100
  return descriptor;
@@ -222,13 +222,16 @@ export function fileSearchRegex(folder: string, traitRegex: string) {
222
222
  * @returns 文件绝对路径数组
223
223
  */
224
224
  export function fileSearchGlob(globPattern:string|string[],ignore?:string|string[]){
225
- return globSync(globPattern,{ignore,absolute:true});
225
+ const fixedPath = typeof globPattern === "string"
226
+ ? path.posix.normalize(globPattern)
227
+ : globPattern.map((p)=>path.posix.normalize(p));
228
+ return globSync(fixedPath,{ignore,absolute:true});
226
229
  }
227
230
  /**
228
231
  * @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
229
232
  */
230
233
  export function fileSearch(...patams:any[]){
231
- throw "请使用 fileSearchRegex 或 fileSearchGlob"
234
+ UtilFunc.throwError("请使用 fileSearchRegex 或 fileSearchGlob",'fatal');
232
235
  }
233
236
  /**是一个有效的文件路径
234
237
  * @param filePath - 需要验证的文件路径
@@ -1,7 +1,7 @@
1
1
  import * as crypto from "crypto";
2
2
  import { AnyFunc, ComposedClass, ComposedMixinable, ExtractOutcome, IJData, JObject, JToken, Keyable, LiteralCheck, Matchable, MatchableFlag, Mixinable, Outcome, PromiseStat, PromiseVerifyFn, UnionToIntersection } from "@src/UtilInterfaces";
3
3
  import * as cp from "child_process";
4
- import { SLogger } from "@src/UtilLogger";
4
+ import { LogLevel, SLogger } from "@src/UtilLogger";
5
5
  import { Completed, Failed, FailedLike, None, StatusSymbol, Success, SuccessLike, Terminated, Timeout } from "./UtilSymbol";
6
6
  import { Catch, DeferAsync, LogTimeAsync } from "./UtilDecorators";
7
7
 
@@ -437,10 +437,7 @@ static matchProc<
437
437
  }
438
438
  else if ((procObj as any)[t.status])
439
439
  return (procObj as any)[t.status](t.status,(t as any).result);
440
-
441
- const err = new Error(`matchProc 传入了一个预料之外的值: ${String(t)}`);
442
- SLogger.fatal(err);
443
- throw err;
440
+ UtilFunc.throwError(`matchProc 传入了一个预料之外的值: ${String(t)}`,'fatal');
444
441
  }
445
442
 
446
443
  /**根据典型的成功或失败状态运行函数
@@ -509,7 +506,7 @@ static likeNone(t:unknown,strictLog=true):t is None{
509
506
  * @returns 排除None的原值
510
507
  */
511
508
  static expect<T>(t:T,errLog?:string):Exclude<T,None>{
512
- if(t===None) throw new Error(errLog);
509
+ if(t===None) UtilFunc.throwError(errLog??"expect验证了一个None值","error");
513
510
  return t as Exclude<T,None>;
514
511
  }
515
512
 
@@ -584,6 +581,21 @@ static dedent(strings: TemplateStringsArray, ...values: any[]): string {
584
581
  );
585
582
  return lines.map(line => line.slice(minIndent)).join('\n');
586
583
  }
587
-
584
+ /**抛出错误
585
+ * @param message - 错误信息
586
+ * @param lvl - 日志等级
587
+ * @param opt - 额外参数
588
+ */
589
+ static throwError(message:string,lvl?:LogLevel,opt?:{}):never{
590
+ const e = new Error(message);
591
+ const stackLines = e.stack!.split('\n');
592
+ e.stack = [stackLines[0]].concat(stackLines.slice(2)).join('\n'); // 移除第一行的堆栈信息
593
+ Object.assign(e, opt);
594
+ if(lvl){
595
+ SLogger.log(lvl,message);
596
+ SLogger.log(lvl,e.stack);
597
+ }
598
+ throw e;
599
+ }
588
600
 
589
601
  }
@@ -214,4 +214,3 @@ export type ExtractOutcome<T,K extends Keyable> =
214
214
  * 输出schema后替换为 ^.*$ 的 string 匹配
215
215
  */
216
216
  export type SchemaString = `${string}SchemaString`;
217
-
package/src/UtilLogger.ts CHANGED
@@ -226,7 +226,7 @@ export class SLogger{
226
226
  * @returns 格式化的时间字符串
227
227
  */
228
228
  timeEnd(flag:string,level:LogLevel|null="info"):string|null{
229
- let start = SLogger.timeTable[flag];
229
+ const start = SLogger.timeTable[flag];
230
230
  if(start==null){
231
231
  this.warn("SLogger.timeEnd 错误 flag:"+flag+" 不存在");
232
232
  return null;
@@ -304,7 +304,7 @@ export class SLogger{
304
304
  * @returns 自身
305
305
  */
306
306
  static log(level: LogLevel, ...messages:Array<any>):SLogger{
307
- level = level||"silly";
307
+ level = level??"silly";
308
308
  this.defaultInstance.log(level,...messages);
309
309
  return this.defaultInstance;
310
310
  }