@zwa73/utils 1.0.146 → 1.0.147

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.
File without changes
@@ -0,0 +1 @@
1
+ "use strict";
@@ -37,7 +37,7 @@ const UtilClass_1 = require("./UtilClass");
37
37
  class SFfmpegTool {
38
38
  /**静态构造函数 */
39
39
  static init() {
40
- const ffmpegPath = process.env.FFMPEG_PATH;
40
+ const ffmpegPath = process?.env?.FFMPEG_PATH;
41
41
  if (ffmpegPath != null) {
42
42
  const exepath = pathe_1.default.join(ffmpegPath, "ffmpeg.exe");
43
43
  SFfmpegTool.setFfmpegPath(exepath);
@@ -1,6 +1,7 @@
1
1
  import { AnyFunc, ComposedClass, ComposedMixinable, ComposedRefMixinable, ExtractOutcome, IJData, JObject, JToken, Keyable, Literal, Matchable, MatchableFlag, Mixinable, Outcome, ReqVerifyFn, ProperSubsetCheck, RefMixinable, UnionToIntersection, Await, AnyRecord } from "./UtilInterfaces";
2
2
  import { LogLevel } from "./UtilLogger";
3
3
  import { Failed, FailedLike, None, StatusSymbol, Success, SuccessLike, Timeout } from "./UtilSymbol";
4
+ import { PRecord } from "..";
4
5
  declare const HashMethodList: readonly ["md5", "sha1", "sha256", "sha512", "sha3", "blake2", "blake3"];
5
6
  type HashMethod = typeof HashMethodList[number];
6
7
  /**执行选项 */
@@ -165,6 +166,16 @@ export declare class UtilFunc {
165
166
  }>>(t: T, procObj: P): P extends Record<any, AnyFunc> ? {
166
167
  [K in keyof P]: ReturnType<P[K]>;
167
168
  }[keyof P] : never;
169
+ /**处理部分联合值
170
+ * @param t - 目标值
171
+ * @param procObj - 所有可能的id组成的处理函数映射 (status, result)=>any
172
+ * @returns 任意处理函数的返回值
173
+ */
174
+ static matchPartialProc<T extends Matchable<Keyable>, P extends Partial<{
175
+ [K in MatchableFlag<T>]: (k: K, v: ExtractOutcome<T, MatchableFlag<K>>['result']) => unknown;
176
+ }>>(t: T, procObj: P): (P extends PRecord<Keyable, AnyFunc> ? {
177
+ [K in keyof P]: P[K] extends AnyFunc ? ReturnType<P[K]> : undefined;
178
+ }[keyof P] : never) | undefined;
168
179
  /**根据典型的成功或失败状态运行函数
169
180
  * @param t - 目标值
170
181
  * @param sucessFunc - 成功则运行的函数 (result)=>any
@@ -392,19 +392,20 @@ class UtilFunc {
392
392
  space = space === null ? undefined : space;
393
393
  if (!compress)
394
394
  return JSON.stringify(token, null, space);
395
+ const ec = '\uF121\uF122\uF123';
395
396
  const compressReplacer = (key, value) => {
396
397
  if (Array.isArray(value) && value.every(item => typeof item === 'number' || typeof item === 'string' ||
397
398
  typeof item === 'boolean' || item == null))
398
- return `@@@${JSON.stringify(value)}@@@`;
399
+ return `${ec}${JSON.stringify(value)}${ec}`;
399
400
  const str = JSON.stringify(value);
400
401
  if (typeof str != 'string')
401
402
  return value;
402
403
  if (str.length <= 100)
403
- return `@@@${str}@@@`;
404
+ return `${ec}${str}${ec}`;
404
405
  return value;
405
406
  };
406
407
  return JSON.stringify(token, compressReplacer, space)
407
- .replace(/"@@@(.*?)@@@"/g, (match, p1) => p1.replace(/\\([\\"])/g, '$1'));
408
+ .replace(new RegExp(`"${ec}(.*?)${ec}"`, 'g'), (match, p1) => p1.replace(/\\([\\"])/g, '$1'));
408
409
  }
409
410
  /**代办表 用于队列处理等待 */
410
411
  static pendingMap = {};
@@ -486,6 +487,23 @@ class UtilFunc {
486
487
  return procObj[t.status](t.status, t.result);
487
488
  UtilFunc.throwError(`matchProc 传入了一个预料之外的值: ${String(t)}`, 'fatal');
488
489
  }
490
+ /**处理部分联合值
491
+ * @param t - 目标值
492
+ * @param procObj - 所有可能的id组成的处理函数映射 (status, result)=>any
493
+ * @returns 任意处理函数的返回值
494
+ */
495
+ static matchPartialProc(t, procObj) {
496
+ if (typeof t === 'string' || typeof t === 'number' || typeof t === 'symbol') {
497
+ if (procObj[t])
498
+ return procObj[t](t);
499
+ else
500
+ return undefined;
501
+ }
502
+ else if (procObj[t.status])
503
+ return procObj[t.status](t.status, t.result);
504
+ UtilLogger_1.SLogger.warn(`matchPartialProc 传入了一个预料之外的值, 已返回undefined: ${String(t)}`);
505
+ return undefined;
506
+ }
489
507
  /**根据典型的成功或失败状态运行函数
490
508
  * @param t - 目标值
491
509
  * @param sucessFunc - 成功则运行的函数 (result)=>any
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.146",
3
+ "version": "1.0.147",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -17,6 +17,7 @@
17
17
  "author": "zwa73",
18
18
  "license": "ISC",
19
19
  "dependencies": {
20
+ "@deepkit/type": "^1.0.1-alpha.153",
20
21
  "fluent-ffmpeg": "2.1.2",
21
22
  "glob": "^10.4.1",
22
23
  "html-entities": "^2.3.3",
@@ -32,11 +33,10 @@
32
33
  "devDependencies": {
33
34
  "@types/fluent-ffmpeg": "^2.1.21",
34
35
  "@types/jest": "^29.5.12",
35
- "@types/node": "^18.16.3",
36
+ "@types/node": "^20.14.11",
36
37
  "@zwa73/dev-utils": "^1.0.58",
37
38
  "jest": "^29.7.0",
38
39
  "ts-jest": "^29.1.2",
39
- "ts-morph": "^23.0.0",
40
40
  "tsc-alias": "^1.8.8",
41
41
  "typescript": "^5.3.3"
42
42
  }
@@ -1,5 +1,5 @@
1
1
  const path = require('path');
2
- const { UtilFT, SLogger } = require("../index");
2
+ const { UtilFT } = require("../index");
3
3
  const readline = require('readline');
4
4
 
5
5
  const PACKAGE_PATH = path.join(__dirname,'..','package.json');
@@ -17,6 +17,7 @@ const INFO_TABLE = {
17
17
  "1.0.128":"fileSearch 添加Sync版本, 默认改为异步函数",
18
18
  "1.0.131":"repeatPromise 与 repeatPost 改为传入可选参数表模式",
19
19
  }
20
+ const PKG_NAME = "utils";
20
21
 
21
22
  // 将版本号转换为可以比较的数字
22
23
  function versionToNumber(version) {
@@ -44,19 +45,18 @@ function showUpgradeMessages(prevVersion, currentVersion) {
44
45
  }
45
46
 
46
47
 
47
-
48
48
  async function main(){
49
49
  const packageTable = await UtilFT.loadJSONFile(PACKAGE_PATH);
50
50
  const currentVersion = packageTable.version;
51
51
  await UtilFT.ensurePathExists(VERSION_PATH);
52
- const versionTable = await UtilFT.loadJSONFile(VERSION_PATH,{})
53
- const prevVersion = versionTable.utils.version;
52
+ const versionTable = await UtilFT.loadJSONFile(VERSION_PATH,{default:{}});
53
+ const prevVersion = versionTable[PKG_NAME]?.version ?? "0.0.0";
54
54
 
55
55
  console.log(`${currentVersion} 版本已安装`);
56
56
  // 使用这个函数来显示升级信息
57
57
  const hasOut = showUpgradeMessages(prevVersion, currentVersion);
58
58
 
59
- const ntable = Object.assign({},versionTable,{utils:{version:currentVersion}});
59
+ const ntable = Object.assign({},versionTable,{[PKG_NAME]:{version:currentVersion}});
60
60
  await UtilFT.writeJSONFile(VERSION_PATH, ntable);
61
61
 
62
62
  if(!hasOut) return;
File without changes
@@ -15,7 +15,7 @@ export type IOMap = { [key: string]: string };
15
15
  class SFfmpegTool {
16
16
  /**静态构造函数 */
17
17
  static init() {
18
- const ffmpegPath = process.env.FFMPEG_PATH;
18
+ const ffmpegPath = process?.env?.FFMPEG_PATH;
19
19
  if(ffmpegPath!=null){
20
20
  const exepath = path.join(ffmpegPath,"ffmpeg.exe");
21
21
  SFfmpegTool.setFfmpegPath(exepath);
@@ -8,6 +8,7 @@ import opath from 'path';
8
8
  import { UtilFT } from "./UtilFileTools";
9
9
  import {inspect} from 'util';
10
10
  import type * as publicIp from 'public-ip';
11
+ import { PRecord } from "..";
11
12
 
12
13
  const HashMethodList = ["md5","sha1","sha256","sha512","sha3","blake2","blake3"] as const;
13
14
  type HashMethod = typeof HashMethodList[number];
@@ -459,19 +460,21 @@ static stringifyJToken(token:JToken|IJData,opt?:StringifyOpt){
459
460
 
460
461
  if(!compress) return JSON.stringify(token,null,space);
461
462
 
463
+ const ec = '\uF121\uF122\uF123';
464
+
462
465
  const compressReplacer = (key: string, value: any)=> {
463
466
  if (Array.isArray(value) && value.every(item =>
464
467
  typeof item === 'number' || typeof item === 'string' ||
465
468
  typeof item === 'boolean' || item == null))
466
- return `@@@${JSON.stringify(value)}@@@`;
469
+ return `${ec}${JSON.stringify(value)}${ec}`;
467
470
  const str = JSON.stringify(value);
468
471
  if(typeof str!='string') return value;
469
- if(str.length<=100) return `@@@${str}@@@`;
472
+ if(str.length<=100) return `${ec}${str}${ec}`;
470
473
  return value;
471
474
  }
472
475
 
473
476
  return JSON.stringify(token,compressReplacer,space)
474
- .replace(/"@@@(.*?)@@@"/g, (match, p1) =>
477
+ .replace(new RegExp(`"${ec}(.*?)${ec}"`,'g'), (match, p1) =>
475
478
  p1.replace(/\\([\\"])/g, '$1'));
476
479
  }
477
480
 
@@ -553,9 +556,10 @@ static outcome<K extends Keyable,V> (key:K,value:V):Outcome<K,V>{
553
556
  */
554
557
  static matchProc<
555
558
  T extends Matchable<Keyable>,
556
- P extends UnionToIntersection<
557
- { [K in MatchableFlag<T>] :
558
- (k:K,v:ExtractOutcome<T,MatchableFlag<K>>['result'])=>unknown }>>
559
+ P extends UnionToIntersection<{//旧版本可能会将此映射处理为联合类型 所以需要UnionToIntersection
560
+ [K in MatchableFlag<T>] :
561
+ (k:K,v:ExtractOutcome<T,MatchableFlag<K>>['result'])=>unknown
562
+ }>>
559
563
  (t:T, procObj:P):
560
564
  P extends Record<any,AnyFunc>
561
565
  ? {[K in keyof P]: ReturnType<P[K]>}[keyof P]
@@ -569,6 +573,35 @@ static matchProc<
569
573
  UtilFunc.throwError(`matchProc 传入了一个预料之外的值: ${String(t)}`,'fatal');
570
574
  }
571
575
 
576
+ /**处理部分联合值
577
+ * @param t - 目标值
578
+ * @param procObj - 所有可能的id组成的处理函数映射 (status, result)=>any
579
+ * @returns 任意处理函数的返回值
580
+ */
581
+ static matchPartialProc<
582
+ T extends Matchable<Keyable>,
583
+ P extends Partial<{
584
+ [K in MatchableFlag<T>] :
585
+ (k:K,v:ExtractOutcome<T,MatchableFlag<K>>['result'])=>unknown
586
+ }>>
587
+ (t:T, procObj:P):
588
+ (P extends PRecord<Keyable,AnyFunc>
589
+ ? {[K in keyof P]: P[K] extends AnyFunc
590
+ ? ReturnType<P[K]> : undefined
591
+ }[keyof P] : never ) | undefined {
592
+ if (typeof t === 'string' || typeof t === 'number' || typeof t === 'symbol'){
593
+ if ((procObj as any)[t])
594
+ return (procObj as any)[t](t);
595
+ else return undefined;
596
+ }
597
+ else if ((procObj as any)[t.status])
598
+ return (procObj as any)[t.status](t.status,(t as any).result);
599
+ SLogger.warn(`matchPartialProc 传入了一个预料之外的值, 已返回undefined: ${String(t)}`);
600
+ return undefined;
601
+ }
602
+
603
+
604
+
572
605
  /**根据典型的成功或失败状态运行函数
573
606
  * @param t - 目标值
574
607
  * @param sucessFunc - 成功则运行的函数 (result)=>any
@@ -1,47 +0,0 @@
1
- import { Await, JToken, NeedInit } from "./UtilInterfaces";
2
- export type ServiceInstance = {
3
- /**停止实例释放内存
4
- * @async
5
- */
6
- kill: () => Promise<void> | void;
7
- };
8
- export type ServiceConfig = {
9
- name: string;
10
- opt: JToken;
11
- };
12
- export type CtorTable = {
13
- [Type in string]: (opt: any) => Promise<ServiceInstance> | ServiceInstance;
14
- };
15
- export type CtorTable2FullCfgTable<CT extends CtorTable, Cfg> = {
16
- [P in keyof CT]: P extends string ? CT[P] extends (opt: infer Opt) => any ? Cfg & {
17
- type: P;
18
- opt: Opt;
19
- } : never : never;
20
- };
21
- export declare class ServiceAdapter<CTT extends CtorTable, CFG extends ServiceConfig, FCT extends CtorTable2FullCfgTable<CTT, CFG> = CtorTable2FullCfgTable<CTT, CFG>> implements NeedInit {
22
- private _cfgPath;
23
- private _ctorTable;
24
- inited: Promise<void>;
25
- protected _cfgTable: Record<string, FCT[keyof FCT]>;
26
- protected _insPool: Record<string, Await<ReturnType<CTT[keyof CTT]>>>;
27
- protected constructor(_cfgPath: string, _ctorTable: CTT);
28
- start<T extends keyof FCT>(type: T, opt: FCT[T]['opt'], name?: string): Promise<void>;
29
- }
30
- type ITest1 = {
31
- test1(): void;
32
- } & ServiceInstance;
33
- type ITest2 = {
34
- test2(): void;
35
- } & ServiceInstance;
36
- type TCT = {
37
- 'Test1': (opt: {
38
- t1: number;
39
- }) => ITest1;
40
- 'Test2': (opt: {
41
- t2: string;
42
- }) => ITest2;
43
- };
44
- export type CfgSchema = CtorTable2FullCfgTable<TCT, {
45
- ssa: string;
46
- }>;
47
- export {};
@@ -1,35 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ServiceAdapter = void 0;
4
- const UtilFileTools_1 = require("./UtilFileTools");
5
- const UtilFunctions_1 = require("./UtilFunctions");
6
- class ServiceAdapter {
7
- _cfgPath;
8
- _ctorTable;
9
- inited;
10
- _cfgTable = {};
11
- _insPool = {};
12
- constructor(_cfgPath, _ctorTable) {
13
- this._cfgPath = _cfgPath;
14
- this._ctorTable = _ctorTable;
15
- this.inited = UtilFunctions_1.UtilFunc.ivk(async () => {
16
- this._cfgTable = await UtilFileTools_1.UtilFT.loadJSONFile(_cfgPath);
17
- for (const name in this._cfgTable) {
18
- const cfg = this._cfgTable[name];
19
- //@ts-ignore
20
- this._insPool[name] = await this._ctorTable[cfg.type](cfg.opt);
21
- }
22
- });
23
- }
24
- async start(type, opt, name = 'default') {
25
- if (this._insPool[name] != undefined)
26
- await this._insPool[name].kill();
27
- //@ts-ignore
28
- this._insPool[name] = await this._ctorTable[type](opt);
29
- }
30
- }
31
- exports.ServiceAdapter = ServiceAdapter;
32
- class TessClass extends ServiceAdapter {
33
- }
34
- let a = null;
35
- a.start('Test1', { t1: 100 });
@@ -1,73 +0,0 @@
1
- import { UtilFT } from "./UtilFileTools";
2
- import { UtilFunc } from "./UtilFunctions";
3
- import { Await, JToken, NeedInit } from "./UtilInterfaces";
4
-
5
-
6
-
7
-
8
-
9
-
10
- export type ServiceInstance = {
11
- /**停止实例释放内存
12
- * @async
13
- */
14
- kill:()=>Promise<void>|void;
15
- }
16
- export type ServiceConfig = {
17
- name:string;
18
- opt :JToken;
19
- };
20
- export type CtorTable = {
21
- [Type in string]:(opt:any)=>Promise<ServiceInstance>|ServiceInstance
22
- }
23
- export type CtorTable2FullCfgTable<CT extends CtorTable,Cfg> = {
24
- [P in keyof CT]: P extends string ?
25
- CT[P] extends (opt:infer Opt)=>any
26
- ? Cfg&{type:P,opt:Opt}
27
- : never
28
- : never
29
- }
30
- export class ServiceAdapter
31
- <CTT extends CtorTable,
32
- CFG extends ServiceConfig,
33
- FCT extends CtorTable2FullCfgTable<CTT,CFG> = CtorTable2FullCfgTable<CTT,CFG>>
34
- implements NeedInit{
35
- inited:Promise<void>;
36
- protected _cfgTable :Record<string,FCT[keyof FCT]>={};
37
- protected _insPool :Record<string,Await<ReturnType<CTT[keyof CTT]>>>={};
38
- protected constructor(private _cfgPath:string, private _ctorTable:CTT){
39
- this.inited=UtilFunc.ivk(async ()=>{
40
- this._cfgTable = await UtilFT.loadJSONFile(_cfgPath) as Record<string,FCT[keyof FCT]>;
41
- for(const name in this._cfgTable){
42
- const cfg = this._cfgTable[name];
43
- //@ts-ignore
44
- this._insPool[name] = await this._ctorTable[cfg.type](cfg.opt);
45
- }
46
- });
47
- }
48
- async start<T extends keyof FCT>(type:T,opt:FCT[T]['opt'],name:string='default'){
49
- if(this._insPool[name]!=undefined)
50
- await this._insPool[name].kill();
51
- //@ts-ignore
52
- this._insPool[name] = await this._ctorTable[type as keyof CTT](opt)
53
- }
54
- }
55
-
56
-
57
- type ITest1 = {
58
- test1():void;
59
- }&ServiceInstance;
60
- type ITest2 = {
61
- test2():void;
62
- }&ServiceInstance;
63
-
64
- type TCT = {
65
- 'Test1':(opt:{t1:number})=>ITest1;
66
- 'Test2':(opt:{t2:string})=>ITest2;
67
- }
68
- class TessClass extends ServiceAdapter<TCT,{}&ServiceConfig>{
69
- }
70
- let a:TessClass = null as any;
71
- a.start('Test1',{t1:100});
72
-
73
- export type CfgSchema = CtorTable2FullCfgTable<TCT,{ssa:string}>;