@zwa73/utils 1.0.60 → 1.0.62

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.
Files changed (42) hide show
  1. package/dist/UtilCodecs.d.ts +16 -16
  2. package/dist/UtilCodecs.js +16 -16
  3. package/dist/UtilCom.d.ts +22 -25
  4. package/dist/UtilCom.js +35 -38
  5. package/dist/UtilDecorators.d.ts +15 -8
  6. package/dist/UtilDecorators.js +79 -25
  7. package/dist/UtilFP.d.ts +46 -0
  8. package/dist/UtilFP.js +52 -0
  9. package/dist/UtilFfmpegTools.d.ts +30 -30
  10. package/dist/UtilFfmpegTools.js +32 -32
  11. package/dist/UtilFileTools.d.ts +30 -35
  12. package/dist/UtilFileTools.js +17 -18
  13. package/dist/UtilFunctions.d.ts +37 -78
  14. package/dist/UtilFunctions.js +27 -62
  15. package/dist/UtilInterfaces.d.ts +26 -11
  16. package/dist/UtilInterfaces.js +12 -3
  17. package/dist/UtilLogger.d.ts +55 -55
  18. package/dist/UtilLogger.js +55 -55
  19. package/dist/index.d.ts +1 -0
  20. package/dist/index.js +1 -0
  21. package/dist/test/composeTest.d.ts +26 -0
  22. package/dist/test/composeTest.js +50 -0
  23. package/dist/test/importtest.d.ts +1 -0
  24. package/dist/test/importtest.js +4 -0
  25. package/dist/test/test.js +5 -3
  26. package/package.json +1 -1
  27. package/src/UtilClass.ts +1051 -1051
  28. package/src/UtilCodecs.ts +117 -117
  29. package/src/UtilCom.ts +171 -174
  30. package/src/UtilDecorators.ts +174 -116
  31. package/src/UtilFP.ts +98 -0
  32. package/src/UtilFfmpegTools.ts +271 -271
  33. package/src/UtilFileTools.ts +231 -236
  34. package/src/UtilFunctions.ts +289 -364
  35. package/src/UtilInterfaces.ts +158 -138
  36. package/src/UtilLogger.ts +386 -386
  37. package/src/index.ts +10 -9
  38. package/src/test/composeTest.ts +50 -0
  39. package/src/test/importtest.ts +5 -0
  40. package/src/test/test.ts +8 -6
  41. package/src/test/test2.ts +2 -3
  42. package/tsconfig.json +2 -7
@@ -1,116 +1,174 @@
1
- import { SLogger } from "./UtilLogger";
2
-
3
-
4
- /**用于打印方法的调用 */
5
- export function DLogger(){
6
- return function (target:any, propertyKey:string, descriptor:PropertyDescriptor){
7
- const originalMethod = descriptor.value;
8
- descriptor.value = function(...args:any[]){
9
- let result = originalMethod.apply(this, args);
10
- SLogger.info(`Call: ${propertyKey}(${args}) => ${result}`);
11
- return result;
12
- }
13
- }
14
- }
15
-
16
- /**用于打印异步方法的调用 */
17
- export function DLoggerAsync(){
18
- return function (target:any, propertyKey:string, descriptor:PropertyDescriptor){
19
- const originalMethod = descriptor.value;
20
- descriptor.value = async function(...args:any[]){
21
- let result = await originalMethod.apply(this, args);
22
- SLogger.info(`Call: ${propertyKey}(${args}) => ${result}`);
23
- return result;
24
- }
25
- }
26
- }
27
- /**try-finally包装 */
28
- export function Defer(deferLogic: () => void) {
29
- return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
30
- const originalMethod = descriptor.value;
31
- descriptor.value = function (...args: any[]) {
32
- try {
33
- const result = originalMethod.apply(this, args);
34
- deferLogic();
35
- return result;
36
- } catch(e) {
37
- deferLogic();
38
- throw e;
39
- }
40
- };
41
- return descriptor;
42
- };
43
- }
44
- /**异步的try-finally包装 */
45
- export function DeferAsync(deferLogic: () => void) {
46
- return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
47
- const originalMethod = descriptor.value;
48
- descriptor.value = async function (...args: any[]) {
49
- try {
50
- const result = await originalMethod.apply(this, args);
51
- deferLogic();
52
- return result;
53
- } catch(e) {
54
- deferLogic();
55
- throw e;
56
- }
57
- };
58
- return descriptor;
59
- };
60
- }
61
-
62
- /**用于捕获方法中的错误 */
63
- export function DCatchErrors() {
64
- return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
65
- const originalMethod = descriptor.value;
66
- descriptor.value = function (...args: any[]) {
67
- try {
68
- originalMethod.apply(this, args);
69
- } catch (err) {
70
- SLogger.warn(`Error in method ${propertyKey}: ${err}`);
71
- }
72
- };
73
- };
74
- }
75
-
76
- /**用于捕获异步方法中的错误 */
77
- export function DCatchErrorsAsync() {
78
- return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
79
- const originalMethod = descriptor.value;
80
- descriptor.value = async function (...args: any[]) {
81
- try {
82
- await originalMethod.apply(this, args);
83
- } catch (err) {
84
- SLogger.warn(`Error in method ${propertyKey}: ${err}`);
85
- }
86
- };
87
- };
88
- }
89
-
90
-
91
- function AddNumberDecorator(n: number) {
92
- type NumberToNumberFunc = (num: number) => number;
93
- return function(
94
- this:any,
95
- target: Object,
96
- propertyKey: string,
97
- descriptor: TypedPropertyDescriptor<NumberToNumberFunc>
98
- ) {
99
- const originalMethod = descriptor.value!;
100
- descriptor.value = function (num: number) {
101
- const result = originalMethod.apply(this, [num]);
102
- return result + n;
103
- };
104
- return descriptor;
105
- };
106
- }
107
-
108
- class Example {
109
- @AddNumberDecorator(10)
110
- myMethod(num: number): number {
111
- return num;
112
- }
113
- }
114
-
115
- //let a = new Example();
116
- //a.myMethod(10);//?
1
+ import { SLogger } from "./UtilLogger";
2
+
3
+ type TDTg<T> =
4
+ (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<T>)=>
5
+ TypedPropertyDescriptor<T>;
6
+ type DTg = (target:Object, propertyKey:string, descriptor:PropertyDescriptor)=>
7
+ PropertyDescriptor;
8
+
9
+ /**用于打印方法的调用 */
10
+ export function LogCall():DTg{
11
+ return function (target, propertyKey, descriptor){
12
+ const originalMethod = descriptor.value;
13
+ descriptor.value = function(...args:any[]){
14
+ let result = originalMethod.apply(this, args);
15
+ SLogger.info(`调用函数: ${propertyKey}(${args}) => ${result}`);
16
+ return result;
17
+ }
18
+ return descriptor;
19
+ }
20
+ }
21
+
22
+ /**用于打印异步方法的调用 */
23
+ export function LogCallAsync():DTg{
24
+ return function (target, propertyKey, descriptor){
25
+ const originalMethod = descriptor.value;
26
+ descriptor.value = async function(...args:any[]){
27
+ let result = await originalMethod.apply(this, args);
28
+ SLogger.info(`调用函数: ${propertyKey}(${args}) => ${result}`);
29
+ return result;
30
+ }
31
+ return descriptor;
32
+ }
33
+ }
34
+
35
+ /**用于打印方法的调用 */
36
+ export function LogErr():DTg{
37
+ return function (target, propertyKey, descriptor){
38
+ const originalMethod = descriptor.value;
39
+ descriptor.value = function(...args:any[]){
40
+ try {
41
+ const result = originalMethod.apply(this, args);
42
+ return result;
43
+ } catch(err) {
44
+ SLogger.warn(`函数出现错误: ${propertyKey}(${args}): ${err}`);
45
+ throw err;
46
+ }
47
+ }
48
+ return descriptor;
49
+ }
50
+ }
51
+
52
+ /**用于打印异步方法的调用 */
53
+ export function LogErrAsync():DTg{
54
+ return function (target, propertyKey, descriptor){
55
+ const originalMethod = descriptor.value;
56
+ descriptor.value = async function(...args:any[]){
57
+ try {
58
+ const result = await originalMethod.apply(this, args);
59
+ return result;
60
+ } catch(err) {
61
+ SLogger.warn(`函数出现错误: ${propertyKey}(${args}): ${err}`);
62
+ throw err;
63
+ }
64
+ }
65
+ return descriptor;
66
+ }
67
+ }
68
+
69
+ /**try-finally包装 */
70
+ export function Defer<T extends (...args:any)=>any>
71
+ (deferLogic:(...args:Parameters<T>)=>any):TDTg<T> {
72
+ return function(target, propertyKey, descriptor) {
73
+ const originalMethod = descriptor.value!;
74
+ (descriptor as any).value = function (...args:any) {
75
+ try {
76
+ const result = originalMethod.apply(this, args);
77
+ return result;
78
+ } finally {
79
+ deferLogic(...args);
80
+ }
81
+ };
82
+ return descriptor;
83
+ };
84
+ }
85
+ /**异步的try-finally包装 */
86
+ export function DeferAsync<T extends (...args:any)=>Promise<any>>
87
+ (deferLogic:(...args:Parameters<T>)=>any|Promise<any>):TDTg<T> {
88
+ return function(target, propertyKey, descriptor) {
89
+ const originalMethod = descriptor.value!;
90
+ (descriptor as any).value = async function (...args:any) {
91
+ try {
92
+ const result = await originalMethod.apply(this, args);
93
+ await deferLogic(...args);
94
+ return result;
95
+ } finally {
96
+ await deferLogic(...args);
97
+ }
98
+ };
99
+ return descriptor;
100
+ };
101
+ }
102
+ /**try-catch包装 */
103
+ export function DCatch<T extends (...args:any)=>any>
104
+ (catchLogic:(error: any, ...args:Parameters<T>)=>ReturnType<T>):TDTg<T> {
105
+ return function(target, propertyKey, descriptor) {
106
+ const originalMethod = descriptor.value!;
107
+ (descriptor as any).value = function (...args:any) {
108
+ try {
109
+ const result = originalMethod.apply(this, args);
110
+ return result;
111
+ } catch(err) {
112
+ //SLogger.warn(`一个函数出现了错误 ${propertyKey}: ${err}`);
113
+ return catchLogic(err, ...args);
114
+ }
115
+ };
116
+ return descriptor;
117
+ };
118
+ }
119
+ /**异步的try-catch包装 */
120
+ export function CatchAsync<T extends (...args:any)=>Promise<any>>
121
+ (catchLogic:(error: any, ...args:Parameters<T>)=>ReturnType<T>):TDTg<T> {
122
+ return function(target, propertyKey, descriptor) {
123
+ const originalMethod = descriptor.value!;
124
+ (descriptor as any).value = async function (...args:any) {
125
+ try {
126
+ const result = await originalMethod.apply(this, args);
127
+ return result;
128
+ } catch(err) {
129
+ //SLogger.warn(`一个函数出现了错误 ${propertyKey}: ${err}`);
130
+ return await catchLogic(err, ...args);
131
+ }
132
+ };
133
+ return descriptor;
134
+ };
135
+ }
136
+
137
+
138
+
139
+
140
+ function AddNumberDecorator(n: number) {
141
+ type NumberToNumberFunc = (num: number) => number;
142
+ return function(
143
+ this:any,
144
+ target: Object,
145
+ propertyKey: string,
146
+ descriptor: TypedPropertyDescriptor<NumberToNumberFunc>
147
+ ) {
148
+ const originalMethod = descriptor.value!;
149
+ descriptor.value = function (num: number) {
150
+ const result = originalMethod.apply(this, [num]);
151
+ return result + n;
152
+ };
153
+ return descriptor;
154
+ };
155
+ }
156
+
157
+
158
+ class Example {
159
+ @AddNumberDecorator(10)
160
+ myMethod(num: number): number {
161
+ return num;
162
+ }
163
+ @LogCall()
164
+ @Defer((a,b)=>null)
165
+ async myMethod1(num: number,ss:string) {
166
+ return 312;
167
+ }
168
+ }
169
+ //let e = new Example();
170
+ //e.myMethod1(1,"");
171
+
172
+ //console.log(123);
173
+ //let a = new Example();
174
+ //a.myMethod(10);//?
package/src/UtilFP.ts ADDED
@@ -0,0 +1,98 @@
1
+
2
+ /**常用函数式编程库 */
3
+ export namespace UtilFP {
4
+
5
+ /**柯里化函数类型 */
6
+ type CurryFunc<T, PrevArgs extends any[] = []> = T extends (...args: infer Args) => infer Result
7
+ ? Args extends [infer Arg, ...infer RestArgs]
8
+ ? RestArgs extends []
9
+ ? (...args: [...PrevArgs, Arg]) => Result
10
+ : (...args: [...PrevArgs, Arg]) => CurryFunc<(...rest:RestArgs) => Result>
11
+ & CurryFunc<(...args: RestArgs) => Result, [...PrevArgs, Arg]>
12
+ : Args extends []
13
+ ? () => Result
14
+ : "CurryFunc错误 可选无法被识别" & Error
15
+ : never;
16
+
17
+ /**柯里化转换
18
+ * @param {T} fn - 将要转换的函数
19
+ * @returns {CurryFunc<T>} 柯里化的函数
20
+ */
21
+ export function curry<T extends Function>(fn: T): CurryFunc<T> {
22
+ return (function curried(...args: any[]) {
23
+ if (args.length >= fn.length)
24
+ return fn(...args);
25
+ return (...restArgs: any[]) => curried(...args, ...restArgs);
26
+ }) as any as CurryFunc<T>;
27
+ }
28
+
29
+ /**可组合的函数 */
30
+ type CompFunc<T, R> = (arg: T) => R;
31
+
32
+ /**函数管道组合
33
+ * 从左到右执行
34
+ * @param fs - 待组合的函数
35
+ * @returns 组合完成的函数
36
+ */
37
+ export function flow<I>(...fs: CompFunc<I, I>[]): CompFunc<I, I>;
38
+ export function flow<I, R1>(f1: CompFunc<I, R1>,...fs: CompFunc<R1, R1>[]): CompFunc<I, R1>;
39
+ export function flow<I, R1, R2>(f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>,...fs: CompFunc<R2, R2>[]): CompFunc<I, R2>;
40
+ export function flow<I, R1, R2, R3>(f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>, f3: CompFunc<R2, R3>,...fs: CompFunc<R3, R3>[]): CompFunc<I, R3>;
41
+ export function flow<I, R1, R2, R3, R4>(f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>, f3: CompFunc<R2, R3>, f4: CompFunc<R3, R4>,...fs: CompFunc<R4, R4>[]): CompFunc<I, R4>;
42
+ export function flow<I, R1, R2, R3, R4, R5>(f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>, f3: CompFunc<R2, R3>, f4: CompFunc<R3, R4>, f5: CompFunc<R4, R5>,...fs: CompFunc<R5, R5>[]): CompFunc<I, R5>;
43
+ export function flow<I, R1, R2, R3, R4, R5, R6>(f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>, f3: CompFunc<R2, R3>, f4: CompFunc<R3, R4>, f5: CompFunc<R4, R5>, f6: CompFunc<R5, R6>,...fs: CompFunc<R6, R6>[]): CompFunc<I, R6>;
44
+ export function flow<I, R1, R2, R3, R4, R5, R6, R7>(f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>, f3: CompFunc<R2, R3>, f4: CompFunc<R3, R4>, f5: CompFunc<R4, R5>, f6: CompFunc<R5, R6>, f7: CompFunc<R6, R7>,...fs: CompFunc<R7, R7>[]): CompFunc<I, R7>;
45
+ export function flow<I, R1, R2, R3, R4, R5, R6, R7, R8>(f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>, f3: CompFunc<R2, R3>, f4: CompFunc<R3, R4>, f5: CompFunc<R4, R5>, f6: CompFunc<R5, R6>, f7: CompFunc<R6, R7>, f8: CompFunc<R7, R8>,...fs: CompFunc<R8, R8>[]): CompFunc<I, R8>;
46
+ export function flow<I, R1, R2, R3, R4, R5, R6, R7, R8, R9>(f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>, f3: CompFunc<R2, R3>, f4: CompFunc<R3, R4>, f5: CompFunc<R4, R5>, f6: CompFunc<R5, R6>, f7: CompFunc<R6, R7>, f8: CompFunc<R7, R8>, f9: CompFunc<R8, R9>,...fs: CompFunc<R9, R9>[]): CompFunc<I, R9>;
47
+ export function flow<T>(...fs: ((arg: T) => T)[]): (arg: T) => T {
48
+ return (arg: T): T => {
49
+ return fs.reduce((value, func) => func(value), arg);
50
+ };
51
+ }
52
+
53
+ /**函数管道
54
+ * 从左到右执行
55
+ * @param input - 初始输入值
56
+ * @param fs - 待组合的函数
57
+ * @returns 经过所有函数处理后的结果
58
+ */
59
+ export function pipe<I>(input: I, ...fs: CompFunc<I, I>[]): I;
60
+ export function pipe<I, R1>(input: I, f1: CompFunc<I, R1>,...fs: CompFunc<R1, R1>[]): R1;
61
+ export function pipe<I, R1, R2>(input: I, f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>,...fs: CompFunc<R2, R2>[]): R2;
62
+ export function pipe<I, R1, R2, R3>(input: I, f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>, f3: CompFunc<R2, R3>,...fs: CompFunc<R3, R3>[]): R3;
63
+ export function pipe<I, R1, R2, R3, R4>(input: I, f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>, f3: CompFunc<R2, R3>, f4: CompFunc<R3, R4>,...fs: CompFunc<R4, R4>[]): R4;
64
+ export function pipe<I, R1, R2, R3, R4, R5>(input: I, f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>, f3: CompFunc<R2, R3>, f4: CompFunc<R3, R4>, f5: CompFunc<R4, R5>,...fs: CompFunc<R5, R5>[]): R5;
65
+ export function pipe<I, R1, R2, R3, R4, R5, R6>(input: I, f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>, f3: CompFunc<R2, R3>, f4: CompFunc<R3, R4>, f5: CompFunc<R4, R5>, f6: CompFunc<R5, R6>,...fs: CompFunc<R6, R6>[]): R6;
66
+ export function pipe<I, R1, R2, R3, R4, R5, R6, R7>(input: I, f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>, f3: CompFunc<R2, R3>, f4: CompFunc<R3, R4>, f5: CompFunc<R4, R5>, f6: CompFunc<R5, R6>, f7: CompFunc<R6, R7>,...fs: CompFunc<R7, R7>[]): R7;
67
+ export function pipe<I, R1, R2, R3, R4, R5, R6, R7, R8>(input: I, f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>, f3: CompFunc<R2, R3>, f4: CompFunc<R3, R4>, f5: CompFunc<R4, R5>, f6: CompFunc<R5, R6>, f7: CompFunc<R6, R7>, f8: CompFunc<R7, R8>,...fs: CompFunc<R8, R8>[]): R8;
68
+ export function pipe<I, R1, R2, R3, R4, R5, R6, R7, R8, R9>(input: I, f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>, f3: CompFunc<R2, R3>, f4: CompFunc<R3, R4>, f5: CompFunc<R4, R5>, f6: CompFunc<R5, R6>, f7: CompFunc<R6, R7>, f8: CompFunc<R7, R8>, f9: CompFunc<R8, R9>,...fs: CompFunc<R9, R9>[]): R9;
69
+ export function pipe<T>(input: T, ...fs: ((arg: T) => T)[]): T {
70
+ return fs.reduce((value, func) => func(value), input);
71
+ }
72
+
73
+
74
+ /**将一个字段加入一个对象中,返回新类型 */
75
+ export function bindTo<K extends string, T, B extends {} = {}>
76
+ (key:K,value:T,base?:B): B & Record<K,T>{
77
+ let out = base as any;
78
+ out = out??{};
79
+ out[key]=value;
80
+ return out;
81
+ }
82
+
83
+ /**
84
+ let asd = bindTo("sss",123,{abc:223});//?
85
+ let sumvoid = ()=>10;
86
+ let a = curry(sumvoid);//?
87
+ console.log(a());
88
+ let sum = (a:number,b:string,c:number,d:boolean)=>a+b+c+d;
89
+ let sumCu = curry(sum);//?
90
+ let suma = sumCu(1)("ss");//?
91
+ let sumb = sumCu(1,"1")(2);//?
92
+ let sumc = sumCu(4);//?
93
+ let sumz = sumCu(1,"b",3)(false);//?
94
+ console.log(suma(2,true));
95
+ console.log(sumb(true));
96
+ console.log(sumc("s",3,false));
97
+ */
98
+ }