@zwa73/utils 1.0.110 → 1.0.112

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, throwError: typeof UtilFunc.throwError;
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, 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.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;
3
+ exports.throwError = exports.dedent = 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, exports.throwError = UtilFunctions_1.UtilFunc.throwError;
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.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,3 +1,4 @@
1
+ import { LiteralCheck } from "./UtilInterfaces";
1
2
  /**函数管道器 */
2
3
  export declare class Piper<Arg, Result = Arg> {
3
4
  /**管道函数列表 */
@@ -55,6 +56,11 @@ export declare class Stream<T> implements Iterable<T> {
55
56
  * @returns 数组
56
57
  */
57
58
  toArray(): Promise<Array<T>>;
59
+ /**应用加工 并排除某个字面量
60
+ * @param value - 排除的值
61
+ * @returns 自身
62
+ */
63
+ exclude<E>(value: LiteralCheck<E>): Promise<Stream<Exclude<T, E>>>;
58
64
  /**应用加工 并过滤
59
65
  * @param func - 过滤函数
60
66
  * @returns 自身
package/dist/UtilClass.js CHANGED
@@ -155,6 +155,14 @@ class Stream {
155
155
  await this.append();
156
156
  return [...this];
157
157
  }
158
+ /**应用加工 并排除某个字面量
159
+ * @param value - 排除的值
160
+ * @returns 自身
161
+ */
162
+ async exclude(value) {
163
+ await this.append();
164
+ return new Stream(this._list.filter((v) => v !== value));
165
+ }
158
166
  /**应用加工 并过滤
159
167
  * @param func - 过滤函数
160
168
  * @returns 自身
@@ -37,9 +37,9 @@ const UtilClass_1 = require("./UtilClass");
37
37
  class SFfmpegTool {
38
38
  /**静态构造函数 */
39
39
  static init() {
40
- let ffmpegPath = process.env.FFMPEG_PATH;
40
+ const ffmpegPath = process.env.FFMPEG_PATH;
41
41
  if (ffmpegPath != null) {
42
- let exepath = path.join(ffmpegPath, "ffmpeg.exe");
42
+ const exepath = path.join(ffmpegPath, "ffmpeg.exe");
43
43
  SFfmpegTool.setFfmpegPath(exepath);
44
44
  }
45
45
  }
@@ -239,8 +239,8 @@ var UtilFT;
239
239
  */
240
240
  function fileSearchGlob(dir, globPattern, opt) {
241
241
  const fixedPath = typeof globPattern === "string"
242
- ? path.join(dir, path.posix.normalize(globPattern))
243
- : globPattern.map((p) => path.join(dir, path.posix.normalize(p)));
242
+ ? path.join(dir, globPattern).replaceAll("\\", "/")
243
+ : globPattern.map((p) => path.join(dir, p).replaceAll("\\", "/"));
244
244
  return (0, glob_1.globSync)(fixedPath, { ignore: opt?.ingore, absolute: true })
245
245
  .map((filePath) => {
246
246
  if (opt?.normalize === undefined)
@@ -1,6 +1,13 @@
1
1
  import { AnyFunc, ComposedClass, ComposedMixinable, ComposedRefMixinable, ExtractOutcome, IJData, JObject, JToken, Keyable, LiteralCheck, Matchable, MatchableFlag, Mixinable, Outcome, PromiseVerifyFn, RefMixinable, UnionToIntersection } from "./UtilInterfaces";
2
2
  import { LogLevel } from "./UtilLogger";
3
3
  import { FailedLike, None, StatusSymbol, Success, SuccessLike, Timeout } from "./UtilSymbol";
4
+ declare const HashMethodList: readonly ["md5", "sha1", "sha256", "sha512", "sha3", "blake2", "blake3"];
5
+ type HashMethod = typeof HashMethodList[number];
6
+ /**执行选项 */
7
+ type ExecOpt = Partial<{
8
+ outlvl: LogLevel;
9
+ errlvl: LogLevel;
10
+ }>;
4
11
  type SuccessOut<T> = Outcome<Success, T>;
5
12
  type TimeoutOut<T> = Outcome<Timeout, Promise<T>>;
6
13
  /**常用函数 */
@@ -12,7 +19,7 @@ export declare class UtilFunc {
12
19
  /**初始化对象的字段
13
20
  * 会改变obj
14
21
  * @param obj - 所要初始化的对象
15
- * @param field - 所要初始化的字段
22
+ * @param field - 所要初始化的字段
16
23
  * @param defaultVal - 默认值
17
24
  * @returns 最终值
18
25
  */
@@ -30,10 +37,11 @@ export declare class UtilFunc {
30
37
  */
31
38
  static genUUID(): string;
32
39
  /**计算Hash
33
- * @param str - 待计算的字符串
40
+ * @param str - 待计算的字符串
41
+ * @param method - hash算法
34
42
  * @returns hash
35
43
  */
36
- static calcHash(str: string): string;
44
+ static calcHash(str: string, method?: HashMethod): string;
37
45
  /**等待 timeMs 毫秒
38
46
  * @async
39
47
  * @param timeMs - 等待的毫秒数
@@ -47,12 +55,13 @@ export declare class UtilFunc {
47
55
  * @returns
48
56
  */
49
57
  static sleep<T>(timeMs: number, result: T): Promise<T>;
50
- /**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
58
+ /**封装的 cp.spawn 执行一段指令,指令运行时实时返回输出
51
59
  * @param command - 指令文本
52
- * @param outlvl - 普通输出的日志等级
53
- * @param errlvl - 错误的日志等级
60
+ * @param opt - 可选参数
61
+ * @param opt.outlvl - 普通输出的日志等级
62
+ * @param opt.errlvl - 错误的日志等级
54
63
  */
55
- static exec(command: string, outlvl?: LogLevel, errlvl?: LogLevel): Promise<{
64
+ static exec(command: string, opt?: ExecOpt): Promise<{
56
65
  stdout: string;
57
66
  stderr: string;
58
67
  }>;
@@ -38,6 +38,7 @@ const cp = __importStar(require("child_process"));
38
38
  const UtilLogger_1 = require("./UtilLogger");
39
39
  const UtilSymbol_1 = require("./UtilSymbol");
40
40
  const UtilDecorators_1 = require("./UtilDecorators");
41
+ const HashMethodList = ["md5", "sha1", "sha256", "sha512", "sha3", "blake2", "blake3"];
41
42
  /**永不完成的Promise单例 */
42
43
  const NeverResolvedPromise = new Promise(() => { });
43
44
  /**常用函数 */
@@ -51,7 +52,7 @@ class UtilFunc {
51
52
  /**初始化对象的字段
52
53
  * 会改变obj
53
54
  * @param obj - 所要初始化的对象
54
- * @param field - 所要初始化的字段
55
+ * @param field - 所要初始化的字段
55
56
  * @param defaultVal - 默认值
56
57
  * @returns 最终值
57
58
  */
@@ -88,11 +89,12 @@ class UtilFunc {
88
89
  return crypto.randomBytes(16).toString("hex");
89
90
  }
90
91
  /**计算Hash
91
- * @param str - 待计算的字符串
92
+ * @param str - 待计算的字符串
93
+ * @param method - hash算法
92
94
  * @returns hash
93
95
  */
94
- static calcHash(str) {
95
- return crypto.createHash('md5').update(str).digest('hex');
96
+ static calcHash(str, method = 'md5') {
97
+ return crypto.createHash(method).update(str).digest('hex');
96
98
  }
97
99
  static async sleep(timeMs, result) {
98
100
  return new Promise(function (resolve, rejecte) {
@@ -101,23 +103,32 @@ class UtilFunc {
101
103
  }, timeMs);
102
104
  });
103
105
  }
104
- /**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
106
+ /**封装的 cp.spawn 执行一段指令,指令运行时实时返回输出
105
107
  * @param command - 指令文本
106
- * @param outlvl - 普通输出的日志等级
107
- * @param errlvl - 错误的日志等级
108
+ * @param opt - 可选参数
109
+ * @param opt.outlvl - 普通输出的日志等级
110
+ * @param opt.errlvl - 错误的日志等级
108
111
  */
109
- static exec(command, outlvl, errlvl) {
112
+ static exec(command, opt) {
110
113
  return new Promise((resolve, reject) => {
111
- cp.exec(command, (error, stdout, stderr) => {
112
- if (error)
113
- reject(error);
114
- else {
115
- if (stdout && outlvl)
116
- UtilLogger_1.SLogger.log(outlvl, stdout);
117
- if (stderr && errlvl)
118
- UtilLogger_1.SLogger.log(errlvl, stderr);
119
- resolve({ stdout, stderr });
120
- }
114
+ const child = cp.spawn(command, { shell: true });
115
+ let stdout = '';
116
+ let stderr = '';
117
+ child.stdout.on('data', (data) => {
118
+ stdout += data;
119
+ if (opt?.outlvl)
120
+ UtilLogger_1.SLogger.log(opt?.outlvl, data.toString());
121
+ });
122
+ child.stderr.on('data', (data) => {
123
+ stderr += data;
124
+ if (opt?.errlvl)
125
+ UtilLogger_1.SLogger.log(opt?.errlvl, data.toString());
126
+ });
127
+ child.on('error', reject);
128
+ child.on('close', (code) => {
129
+ if (code !== 0)
130
+ UtilLogger_1.SLogger.warn(`UtilFunc.exec 命令:"${command}" 结束 代码为:${code}`);
131
+ resolve({ stdout, stderr });
121
132
  });
122
133
  });
123
134
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.110",
3
+ "version": "1.0.112",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -32,7 +32,7 @@
32
32
  "devDependencies": {
33
33
  "@types/jest": "^29.5.12",
34
34
  "@types/node": "^18.16.3",
35
- "@zwa73/dev-utils": "^1.0.13",
35
+ "@zwa73/dev-utils": "^1.0.22",
36
36
  "jest": "^29.7.0",
37
37
  "ts-jest": "^29.1.2",
38
38
  "tsc-alias": "^1.8.8",
@@ -45,7 +45,7 @@ function showUpgradeMessages(prevVersion, currentVersion) {
45
45
  async function main(){
46
46
  const packageTable = await UtilFT.loadJSONFile(PACKAGE_PATH);
47
47
  const currentVersion = packageTable.version;
48
- await UtilFT.ensurePathExists(versionTable);
48
+ await UtilFT.ensurePathExists(VERSION_PATH);
49
49
  const versionTable = Object.assign({},
50
50
  await UtilFT.loadJSONFile(VERSION_PATH,{}),
51
51
  {utils:{version:currentVersion}});
@@ -18,7 +18,6 @@ export const {
18
18
  stringifyJToken,
19
19
  getTime,
20
20
  mapEntries,
21
- composeMixinable,
22
21
  dedent,
23
22
  throwError,
24
23
  } = UtilFunc;
package/src/UtilClass.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { matchProc } from "./QuickFunction";
2
+ import { LiteralCheck } from "./UtilInterfaces";
2
3
 
3
4
  /**函数管道器 */
4
5
  export class Piper<Arg, Result = Arg> {
@@ -160,6 +161,14 @@ export class Stream<T> implements Iterable<T>{
160
161
  await this.append();
161
162
  return [...this];
162
163
  }
164
+ /**应用加工 并排除某个字面量
165
+ * @param value - 排除的值
166
+ * @returns 自身
167
+ */
168
+ async exclude<E>(value:LiteralCheck<E>): Promise<Stream<Exclude<T,E>>>{
169
+ await this.append();
170
+ return new Stream(this._list.filter((v)=>v!==value as any) as Exclude<T,E>[]);
171
+ }
163
172
  /**应用加工 并过滤
164
173
  * @param func - 过滤函数
165
174
  * @returns 自身
@@ -15,9 +15,9 @@ export type IOMap = { [key: string]: string };
15
15
  class SFfmpegTool {
16
16
  /**静态构造函数 */
17
17
  static init() {
18
- let ffmpegPath = process.env.FFMPEG_PATH;
18
+ const ffmpegPath = process.env.FFMPEG_PATH;
19
19
  if(ffmpegPath!=null){
20
- let exepath = path.join(ffmpegPath,"ffmpeg.exe");
20
+ const exepath = path.join(ffmpegPath,"ffmpeg.exe");
21
21
  SFfmpegTool.setFfmpegPath(exepath);
22
22
  }
23
23
  }
@@ -274,8 +274,8 @@ export function fileSearchRegex(dir: string, traitRegex: string, opt?:FileSearch
274
274
  */
275
275
  export function fileSearchGlob(dir: string, globPattern:string|string[],opt?:FileSearchGlobOpt){
276
276
  const fixedPath = typeof globPattern === "string"
277
- ? path.join(dir,path.posix.normalize(globPattern))
278
- : globPattern.map((p)=>path.join(dir,path.posix.normalize(p)));
277
+ ? path.join(dir,globPattern).replaceAll("\\","/")
278
+ : globPattern.map((p)=>path.join(dir,p).replaceAll("\\","/"));
279
279
  return globSync(fixedPath, { ignore: opt?.ingore, absolute: true })
280
280
  .map((filePath) => {
281
281
  if (opt?.normalize === undefined) return filePath;
@@ -5,6 +5,14 @@ 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
 
8
+ const HashMethodList = ["md5","sha1","sha256","sha512","sha3","blake2","blake3"] as const;
9
+ type HashMethod = typeof HashMethodList[number];
10
+
11
+ /**执行选项 */
12
+ type ExecOpt = Partial<{
13
+ outlvl:LogLevel,
14
+ errlvl:LogLevel,
15
+ }>
8
16
 
9
17
  type SuccessOut<T> = Outcome<Success,T>;
10
18
  type TimeoutOut<T> = Outcome<Timeout,Promise<T>>;
@@ -31,7 +39,7 @@ static getTime(): number {
31
39
  /**初始化对象的字段
32
40
  * 会改变obj
33
41
  * @param obj - 所要初始化的对象
34
- * @param field - 所要初始化的字段
42
+ * @param field - 所要初始化的字段
35
43
  * @param defaultVal - 默认值
36
44
  * @returns 最终值
37
45
  */
@@ -71,12 +79,14 @@ static initObject<T extends JObject>
71
79
  static genUUID() {
72
80
  return crypto.randomBytes(16).toString("hex");
73
81
  }
82
+
74
83
  /**计算Hash
75
- * @param str - 待计算的字符串
84
+ * @param str - 待计算的字符串
85
+ * @param method - hash算法
76
86
  * @returns hash
77
87
  */
78
- static calcHash(str:string) {
79
- return crypto.createHash('md5').update(str).digest('hex');
88
+ static calcHash(str:string,method:HashMethod='md5') {
89
+ return crypto.createHash(method).update(str).digest('hex');
80
90
  }
81
91
 
82
92
 
@@ -101,26 +111,35 @@ static async sleep<T>(timeMs: number, result?:T): Promise<T|undefined> {
101
111
  });
102
112
  }
103
113
 
104
- /**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
114
+ /**封装的 cp.spawn 执行一段指令,指令运行时实时返回输出
105
115
  * @param command - 指令文本
106
- * @param outlvl - 普通输出的日志等级
107
- * @param errlvl - 错误的日志等级
116
+ * @param opt - 可选参数
117
+ * @param opt.outlvl - 普通输出的日志等级
118
+ * @param opt.errlvl - 错误的日志等级
108
119
  */
109
- static exec(command: string,outlvl?:LogLevel,errlvl?:LogLevel) {
110
- return new Promise<{ stdout:string, stderr:string }>
111
- ((resolve, reject) => {
112
- cp.exec(command, (error, stdout, stderr) => {
113
- if (error)
114
- reject(error);
115
- else{
116
- if(stdout && outlvl) SLogger.log(outlvl,stdout);
117
- if(stderr && errlvl) SLogger.log(errlvl,stderr);
118
- resolve({ stdout, stderr });
119
- }
120
- });
121
- });
122
- }
120
+ static exec(command: string, opt?:ExecOpt) {
121
+ return new Promise<{ stdout: string, stderr: string }>((resolve, reject) => {
122
+ const child = cp.spawn(command,{shell:true});
123
+
124
+ let stdout = '';
125
+ let stderr = '';
123
126
 
127
+ child.stdout.on('data', (data) => {
128
+ stdout += data;
129
+ if (opt?.outlvl) SLogger.log(opt?.outlvl, data.toString());
130
+ });
131
+
132
+ child.stderr.on('data', (data) => {
133
+ stderr += data;
134
+ if (opt?.errlvl) SLogger.log(opt?.errlvl, data.toString());
135
+ });
136
+ child.on('error', reject);
137
+ child.on('close', (code) => {
138
+ if (code!==0) SLogger.warn(`UtilFunc.exec 命令:"${command}" 结束 代码为:${code}`);
139
+ resolve({ stdout, stderr });
140
+ });
141
+ });
142
+ }
124
143
 
125
144
  /**获得一个永不完成的Promise单例 */
126
145
  static getNeverResolvedPromise<T>():Promise<T>{
@@ -149,7 +168,7 @@ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:PromiseVerifyFn<T>
149
168
  verifyFn = ()=>Success;
150
169
 
151
170
  //进行中的请求
152
- const plist:Promise<
171
+ const plist:Promise<
153
172
  SuccessOut<PromiseResult<T>>|
154
173
  TimeoutOut<PromiseResult<T>>
155
174
  >[] = [];
@@ -373,9 +392,9 @@ static mapEntries<T extends object>
373
392
  * @returns 转换完成的字符串
374
393
  */
375
394
  static stringifyJToken(token:JToken|IJData,space:string|number|null|undefined="\t"){
376
- if(space==null)
377
- space=undefined;
378
- return JSON.stringify(token,null,space);
395
+ if(space==null)
396
+ space=undefined;
397
+ return JSON.stringify(token,null,space);
379
398
  }
380
399
 
381
400
  /**代办表 用于队列处理等待 */