@zwa73/utils 1.0.109 → 1.0.111

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,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
  }
@@ -30,6 +30,7 @@ const UtilLogger_1 = require("./UtilLogger");
30
30
  const JSON5 = __importStar(require("json5"));
31
31
  const glob_1 = require("glob");
32
32
  const UtilFunctions_1 = require("./UtilFunctions");
33
+ const QuickFunction_1 = require("./QuickFunction");
33
34
  /**文件工具 */
34
35
  var UtilFT;
35
36
  (function (UtilFT) {
@@ -221,9 +222,11 @@ var UtilFT;
221
222
  return outArray.map((filePath) => {
222
223
  if (opt?.normalize === undefined)
223
224
  return filePath;
224
- return path[opt.normalize].normalize(filePath);
225
+ return (0, QuickFunction_1.matchProc)(opt.normalize, {
226
+ posix: (nor) => path[nor].normalize(filePath.replaceAll("\\", "/")),
227
+ win32: (nor) => path[nor].normalize(filePath.replaceAll("/", "\\")),
228
+ });
225
229
  });
226
- ;
227
230
  }
228
231
  UtilFT.fileSearchRegex = fileSearchRegex;
229
232
  /**搜索符合Glob匹配的文件
@@ -236,12 +239,16 @@ var UtilFT;
236
239
  */
237
240
  function fileSearchGlob(dir, globPattern, opt) {
238
241
  const fixedPath = typeof globPattern === "string"
239
- ? path.join(dir, path.posix.normalize(globPattern))
240
- : globPattern.map((p) => path.join(dir, path.posix.normalize(p)));
241
- return (0, glob_1.globSync)(fixedPath, { ignore: opt?.ingore, absolute: true }).map((filePath) => {
242
+ ? path.join(dir, globPattern).replaceAll("\\", "/")
243
+ : globPattern.map((p) => path.join(dir, p).replaceAll("\\", "/"));
244
+ return (0, glob_1.globSync)(fixedPath, { ignore: opt?.ingore, absolute: true })
245
+ .map((filePath) => {
242
246
  if (opt?.normalize === undefined)
243
247
  return filePath;
244
- return path[opt.normalize].normalize(filePath);
248
+ return (0, QuickFunction_1.matchProc)(opt.normalize, {
249
+ posix: (nor) => path[nor].normalize(filePath.replaceAll("\\", "/")),
250
+ win32: (nor) => path[nor].normalize(filePath.replaceAll("/", "\\")),
251
+ });
245
252
  });
246
253
  }
247
254
  UtilFT.fileSearchGlob = fileSearchGlob;
@@ -1,6 +1,8 @@
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];
4
6
  type SuccessOut<T> = Outcome<Success, T>;
5
7
  type TimeoutOut<T> = Outcome<Timeout, Promise<T>>;
6
8
  /**常用函数 */
@@ -30,10 +32,11 @@ export declare class UtilFunc {
30
32
  */
31
33
  static genUUID(): string;
32
34
  /**计算Hash
33
- * @param str - 待计算的字符串
35
+ * @param str - 待计算的字符串
36
+ * @param method - hash算法
34
37
  * @returns hash
35
38
  */
36
- static calcHash(str: string): string;
39
+ static calcHash(str: string, method?: HashMethod): string;
37
40
  /**等待 timeMs 毫秒
38
41
  * @async
39
42
  * @param timeMs - 等待的毫秒数
@@ -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
  /**常用函数 */
@@ -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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.109",
3
+ "version": "1.0.111",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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}});
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
  }
@@ -5,6 +5,7 @@ import { SLogger } from "@src/UtilLogger";
5
5
  import * as JSON5 from 'json5';
6
6
  import { globSync } from "glob";
7
7
  import { UtilFunc } from "./UtilFunctions";
8
+ import { matchProc } from "./QuickFunction";
8
9
 
9
10
  /**创建文件选项 */
10
11
  type CreatePathOpt = Partial<{
@@ -242,22 +243,26 @@ export async function writeJSONFile(
242
243
  export function fileSearchRegex(dir: string, traitRegex: string, opt?:FileSearchRegexOpt) {
243
244
  const relative = opt?.relative ?? true;
244
245
  const outArray: string[] = [];
245
- const subFiles = fs.readdirSync(dir,{withFileTypes:true});
246
+ const subFiles = fs.readdirSync(dir, { withFileTypes: true });
246
247
  //如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
247
248
  const regex = new RegExp(traitRegex);
248
249
  for (const subFile of subFiles) {
249
250
  const subFilePath = path.join(dir, subFile.name);
250
251
  //判断是否是文件夹,递归调用
251
252
  if (subFile.isDirectory()) {
252
- if(relative) outArray.push(...fileSearchRegex(path.join(subFilePath, path.sep), traitRegex));
253
+ if (relative)
254
+ outArray.push(...fileSearchRegex(path.join(subFilePath, path.sep), traitRegex));
253
255
  continue;
254
256
  }
255
257
  if (regex.test(subFilePath)) outArray.push(subFilePath);
256
258
  }
257
- return outArray.map((filePath)=>{
258
- if(opt?.normalize===undefined) return filePath;
259
- return path[opt.normalize].normalize(filePath);
260
- });;
259
+ return outArray.map((filePath) => {
260
+ if (opt?.normalize === undefined) return filePath;
261
+ return matchProc(opt.normalize, {
262
+ posix: (nor) => path[nor].normalize(filePath.replaceAll("\\", "/")),
263
+ win32: (nor) => path[nor].normalize(filePath.replaceAll("/", "\\")),
264
+ });
265
+ });
261
266
  }
262
267
  /**搜索符合Glob匹配的文件
263
268
  * @param dir - 起始目录
@@ -269,11 +274,15 @@ export function fileSearchRegex(dir: string, traitRegex: string, opt?:FileSearch
269
274
  */
270
275
  export function fileSearchGlob(dir: string, globPattern:string|string[],opt?:FileSearchGlobOpt){
271
276
  const fixedPath = typeof globPattern === "string"
272
- ? path.join(dir,path.posix.normalize(globPattern))
273
- : globPattern.map((p)=>path.join(dir,path.posix.normalize(p)));
274
- return globSync(fixedPath,{ignore:opt?.ingore,absolute:true}).map((filePath)=>{
275
- if(opt?.normalize===undefined) return filePath;
276
- return path[opt.normalize].normalize(filePath);
277
+ ? path.join(dir,globPattern).replaceAll("\\","/")
278
+ : globPattern.map((p)=>path.join(dir,p).replaceAll("\\","/"));
279
+ return globSync(fixedPath, { ignore: opt?.ingore, absolute: true })
280
+ .map((filePath) => {
281
+ if (opt?.normalize === undefined) return filePath;
282
+ return matchProc(opt.normalize, {
283
+ posix: (nor) => path[nor].normalize(filePath.replaceAll("\\", "/")),
284
+ win32: (nor) => path[nor].normalize(filePath.replaceAll("/", "\\")),
285
+ });
277
286
  });
278
287
  }
279
288
  /**
@@ -5,6 +5,8 @@ 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];
8
10
 
9
11
  type SuccessOut<T> = Outcome<Success,T>;
10
12
  type TimeoutOut<T> = Outcome<Timeout,Promise<T>>;
@@ -71,12 +73,14 @@ static initObject<T extends JObject>
71
73
  static genUUID() {
72
74
  return crypto.randomBytes(16).toString("hex");
73
75
  }
76
+
74
77
  /**计算Hash
75
- * @param str - 待计算的字符串
78
+ * @param str - 待计算的字符串
79
+ * @param method - hash算法
76
80
  * @returns hash
77
81
  */
78
- static calcHash(str:string) {
79
- return crypto.createHash('md5').update(str).digest('hex');
82
+ static calcHash(str:string,method:HashMethod='md5') {
83
+ return crypto.createHash(method).update(str).digest('hex');
80
84
  }
81
85
 
82
86