@zwa73/utils 1.0.201 → 1.0.203

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.
package/src/UtilFP.ts DELETED
@@ -1,222 +0,0 @@
1
- import { isFailed, likeNone } from "./QuickExport";
2
- import { AnyRecord, Keyable, Literal, Matchable, MatchableFlag, ProperSubset, ProperSubsetCheck } from "./UtilInterfaces";
3
- import { Failed, FailedLike, None } from "./UtilSymbol";
4
-
5
- /**常用函数式编程库 */
6
- export namespace UtilFP {
7
-
8
- /**柯里化函数类型 */
9
- type CurryFunc<T, PrevArgs extends unknown[] = []> =
10
- T extends (...args: infer Args) => infer Result
11
- ? Args extends [infer Arg, ...infer RestArgs]
12
- ? RestArgs extends []
13
- ? ((...args: [...PrevArgs, Arg]) => Result)
14
- : ((...args: [...PrevArgs, Arg]) => CurryFunc<(...rest:RestArgs) => Result>)
15
- & (CurryFunc<(...args: RestArgs) => Result, [...PrevArgs, Arg]>)
16
- : Args extends []
17
- ? () => Result
18
- : "CurryFunc错误 默认参数可选无法被识别" & Error
19
- : "CurryFunc错误 传入的并非函数" & Error;
20
-
21
- /**柯里化转换
22
- * @param {T} fn - 将要转换的函数
23
- * @returns {CurryFunc<T>} 柯里化的函数
24
- */
25
- export function curry<T extends Function>(fn: T): CurryFunc<T> {
26
- return (function curried(...args: any[]) {
27
- if (args.length >= fn.length)
28
- return fn(...args);
29
- return (...restArgs: any[]) => curried(...args, ...restArgs);
30
- }) as any as CurryFunc<T>;
31
- }
32
-
33
- /**可组合的函数 */
34
- type CompFunc<T, R> = (arg: T) => R;
35
-
36
-
37
- /**函数管道组合
38
- * 从左到右执行
39
- * @param fs - 待组合的函数
40
- * @returns 组合完成的函数
41
- */
42
- //#region flow重载
43
- export function flow<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15, R16, R17, R18, R19, R20>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, f13: CompFunc<R12, R13>, f14: CompFunc<R13, R14>, f15: CompFunc<R14, R15>, f16: CompFunc<R15, R16>, f17: CompFunc<R16, R17>, f18: CompFunc<R17, R18>, f19: CompFunc<R18, R19>, f20: CompFunc<R19, R20>, ...fs: CompFunc<R20, R20>[]): CompFunc<I, R20>;
44
- export function flow<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15, R16, R17, R18, R19>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, f13: CompFunc<R12, R13>, f14: CompFunc<R13, R14>, f15: CompFunc<R14, R15>, f16: CompFunc<R15, R16>, f17: CompFunc<R16, R17>, f18: CompFunc<R17, R18>, f19: CompFunc<R18, R19>, ...fs: CompFunc<R19, R19>[]): CompFunc<I, R19>;
45
- export function flow<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15, R16, R17, R18>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, f13: CompFunc<R12, R13>, f14: CompFunc<R13, R14>, f15: CompFunc<R14, R15>, f16: CompFunc<R15, R16>, f17: CompFunc<R16, R17>, f18: CompFunc<R17, R18>, ...fs: CompFunc<R18, R18>[]): CompFunc<I, R18>;
46
- export function flow<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15, R16, R17>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, f13: CompFunc<R12, R13>, f14: CompFunc<R13, R14>, f15: CompFunc<R14, R15>, f16: CompFunc<R15, R16>, f17: CompFunc<R16, R17>, ...fs: CompFunc<R17, R17>[]): CompFunc<I, R17>;
47
- export function flow<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15, R16>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, f13: CompFunc<R12, R13>, f14: CompFunc<R13, R14>, f15: CompFunc<R14, R15>, f16: CompFunc<R15, R16>, ...fs: CompFunc<R16, R16>[]): CompFunc<I, R16>;
48
- export function flow<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, f13: CompFunc<R12, R13>, f14: CompFunc<R13, R14>, f15: CompFunc<R14, R15>, ...fs: CompFunc<R15, R15>[]): CompFunc<I, R15>;
49
- export function flow<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, f13: CompFunc<R12, R13>, f14: CompFunc<R13, R14>, ...fs: CompFunc<R14, R14>[]): CompFunc<I, R14>;
50
- export function flow<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, f13: CompFunc<R12, R13>, ...fs: CompFunc<R13, R13>[]): CompFunc<I, R13>;
51
- export function flow<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, ...fs: CompFunc<R12, R12>[]): CompFunc<I, R12>;
52
- export function flow<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, ...fs: CompFunc<R11, R11>[]): CompFunc<I, R11>;
53
- export function flow<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10>(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>, f10: CompFunc<R9, R10>, ...fs: CompFunc<R10, R10>[]): CompFunc<I, R10>;
54
- 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>;
55
- 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>;
56
- 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>;
57
- 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>;
58
- 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>;
59
- 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>;
60
- 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>;
61
- export function flow<I, R1, R2>(f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>, ...fs: CompFunc<R2, R2>[]): CompFunc<I, R2>;
62
- export function flow<I, R1>(f1: CompFunc<I, R1>, ...fs: CompFunc<R1, R1>[]): CompFunc<I, R1>;
63
- export function flow<I>(...fs: CompFunc<I, I>[]): CompFunc<I, I>;
64
- //#endregion
65
- export function flow<T>(...fs: ((arg: T) => T)[]): (arg: T) => T {
66
- return (arg: T): T => {
67
- return fs.reduce((value, func) => func(value), arg);
68
- };
69
- }
70
-
71
- /**函数管道
72
- * 从左到右执行
73
- * @param input - 初始输入值
74
- * @param fs - 待组合的函数
75
- * @returns 经过所有函数处理后的结果
76
- */
77
- //#region pipe重载
78
- export function pipe<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15, R16, R17, R18, R19, R20>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, f13: CompFunc<R12, R13>, f14: CompFunc<R13, R14>, f15: CompFunc<R14, R15>, f16: CompFunc<R15, R16>, f17: CompFunc<R16, R17>, f18: CompFunc<R17, R18>, f19: CompFunc<R18, R19>, f20: CompFunc<R19, R20>, ...fs: CompFunc<R20, R20>[]): R20;
79
- export function pipe<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15, R16, R17, R18, R19>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, f13: CompFunc<R12, R13>, f14: CompFunc<R13, R14>, f15: CompFunc<R14, R15>, f16: CompFunc<R15, R16>, f17: CompFunc<R16, R17>, f18: CompFunc<R17, R18>, f19: CompFunc<R18, R19>, ...fs: CompFunc<R19, R19>[]): R19;
80
- export function pipe<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15, R16, R17, R18>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, f13: CompFunc<R12, R13>, f14: CompFunc<R13, R14>, f15: CompFunc<R14, R15>, f16: CompFunc<R15, R16>, f17: CompFunc<R16, R17>, f18: CompFunc<R17, R18>, ...fs: CompFunc<R18, R18>[]): R18;
81
- export function pipe<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15, R16, R17>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, f13: CompFunc<R12, R13>, f14: CompFunc<R13, R14>, f15: CompFunc<R14, R15>, f16: CompFunc<R15, R16>, f17: CompFunc<R16, R17>, ...fs: CompFunc<R17, R17>[]): R17;
82
- export function pipe<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15, R16>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, f13: CompFunc<R12, R13>, f14: CompFunc<R13, R14>, f15: CompFunc<R14, R15>, f16: CompFunc<R15, R16>, ...fs: CompFunc<R16, R16>[]): R16;
83
- export function pipe<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, f13: CompFunc<R12, R13>, f14: CompFunc<R13, R14>, f15: CompFunc<R14, R15>, ...fs: CompFunc<R15, R15>[]): R15;
84
- export function pipe<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, f13: CompFunc<R12, R13>, f14: CompFunc<R13, R14>, ...fs: CompFunc<R14, R14>[]): R14;
85
- export function pipe<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, f13: CompFunc<R12, R13>, ...fs: CompFunc<R13, R13>[]): R13;
86
- export function pipe<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, f12: CompFunc<R11, R12>, ...fs: CompFunc<R12, R12>[]): R12;
87
- export function pipe<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11>(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>, f10: CompFunc<R9, R10>, f11: CompFunc<R10, R11>, ...fs: CompFunc<R11, R11>[]): R11;
88
- export function pipe<I, R1, R2, R3, R4, R5, R6, R7, R8, R9, R10>(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>, f10: CompFunc<R9, R10>, ...fs: CompFunc<R10, R10>[]): R10;
89
- 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;
90
- 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;
91
- 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;
92
- 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;
93
- 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;
94
- 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;
95
- 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;
96
- export function pipe<I, R1, R2>(input: I, f1: CompFunc<I, R1>, f2: CompFunc<R1, R2>, ...fs: CompFunc<R2, R2>[]): R2;
97
- export function pipe<I, R1>(input: I, f1: CompFunc<I, R1>, ...fs: CompFunc<R1, R1>[]): R1;
98
- export function pipe<I>(input: I, ...fs: CompFunc<I, I>[]): I;
99
- //#endregion
100
- export function pipe<T>(input: T, ...fs: ((arg: T) => T)[]): T {
101
- return fs.reduce((value, func) => func(value), input);
102
- }
103
-
104
-
105
- /**将一个字段加入一个对象中, 返回新类型 */
106
- //#region bind重载
107
- /**绑定一个键和一个值到一个基础对象上, 并返回一个新的对象
108
- * 新的对象包含了基础对象的所有属性, 以及一个新的属性,
109
- * 这个新的属性的键是`key`, 值是`value`
110
- * @template K - 要添加到对象的键的类型 必须为字面量
111
- * @template V - 要添加到对象的值的类型
112
- * @param key - 要添加到对象的键 必须为字面量
113
- * @param value - 要添加到对象的值
114
- * @returns 一个函数, 这个函数接受一个基础对象,
115
- * 然后返回一个新的对象新的对象包含了基础对象的所有属性,
116
- * 以及一个新的属性, 这个新的属性的键是`key`, 值是`value`
117
- */
118
- export function bind<K extends Keyable, V>
119
- (key: Literal<K>,value: V): <B extends AnyRecord>
120
- (base?: B) => keyof B extends K
121
- ? "Base中已有对应键"&Error
122
- : {[P in K | keyof B]: P extends K ? V : P extends keyof B ? B[P] : never;};
123
- /**绑定一个键到一个基础对象上,
124
- * 并返回一个新的对象新的对象包含了基础对象的所有属性,
125
- * 以及一个新的属性, 这个新的属性的键是`key`, 值是基础对象
126
- * @template K - 要添加到对象的键的类型 必须为字面量
127
- * @param key - 要添加到对象的键 必须为字面量
128
- * @returns 一个函数, 这个函数接受一个基础对象,
129
- * 然后返回一个新的对象新的对象包含了基础对象的所有属性,
130
- * 以及一个新的属性, 这个新的属性的键是`key`, 值是基础对象
131
- */
132
- export function bind<K extends Keyable>
133
- (key: Literal<K>): <B extends AnyRecord>
134
- (base?: B) => { [P in K]: B };
135
- /**绑定一个键和一个值到一个基础对象上, 并返回一个新的对象
136
- * 新的对象包含了基础对象的所有属性, 以及一个新的属性,
137
- * 这个新的属性的键是`key`, 值是`value`
138
- * @template K - 要添加到对象的键的类型 必须为字面量
139
- * @template V - 要添加到对象的值的类型
140
- * @template B - 基础对象的类型
141
- * @param key - 要添加到对象的键 必须为字面量
142
- * @param value - 要添加到对象的值
143
- * @param base - 基础对象
144
- * @returns 完成绑定的基础对象
145
- */
146
- export function bind<K extends Keyable, V, B extends AnyRecord>
147
- (key: Literal<K>,value: V, base:B):keyof B extends K
148
- ? "Base中已有对应键"&Error
149
- : {[P in K | keyof B]: P extends K ? V : P extends keyof B ? B[P] : never;};
150
- //#endregion
151
- export function bind
152
- <K extends Keyable, V, B>(key: Literal<K>, value?: V, base?:B){
153
- if(base !== undefined)
154
- return { ...base, [key as Keyable]: value };
155
- if(value ===undefined)
156
- return <B>(base?: B) =>
157
- ({[key as Keyable]:base});
158
-
159
- return <B extends AnyRecord>
160
- (base?: B):{
161
- [P in K | keyof B]: P extends K ? V : P extends keyof B ? B[P] : never;
162
- } => base !== undefined
163
- ? { ...base, [key as Keyable]: value }
164
- : ({ [key as Keyable]: value } as any);
165
- }
166
-
167
- /**创建一个对对象的每个属性进行映射的函数
168
- * @param func - 映射函数, 接受一个属性值和它的键, 返回一个新的属性值
169
- * @returns 一个函数, 这个函数接受一个对象, 然后返回一个新的对象新的对象的每个属性都是原始对象的属性经过映射函数处理后的结果
170
- */
171
- export function map<VAL, OUT, KEY extends symbol|string = string>
172
- (func:(input:VAL,k:KEY)=>OUT):
173
- <OM extends Record<KEY,VAL>>
174
- (input:OM)=>keyof OM extends KEY
175
- ? {[P in keyof OM]:OUT}
176
- : "map函数传入的键类型无法处理此对象"&Error{
177
- return (input) => {
178
- return Reflect
179
- .ownKeys(input as Record<Keyable,unknown>)
180
- .reduce((sum,curr)=>({ ...sum,
181
- [curr] : func((input as any)[curr],curr as any)
182
- }),{}) as any;
183
- };
184
- }
185
-
186
- /**创建在传入值为失败时不运行的函数 */
187
- export const chain = <T, R>(f: (arg:T)=>R) =>
188
- (arg: T): 'E' extends ProperSubsetCheck<symbol,T>? T|R :
189
- T extends Matchable<FailedLike>
190
- ? T : MatchableFlag<T> extends Exclude<MatchableFlag<T>,FailedLike>
191
- ? R : T|R => isFailed(arg) ? arg : f(arg) as any;
192
-
193
- /**创建在传入值为失败时才会尝试运行的函数 */
194
- export const alt = <T, R>(f: CompFunc<T, R>) =>
195
- (arg: T): 'E' extends ProperSubsetCheck<symbol,T> ? T|R :
196
- T extends Matchable<FailedLike>
197
- ? R : MatchableFlag<T> extends Exclude<MatchableFlag<T>,FailedLike>
198
- ? T : T|R => isFailed(arg) ? f(arg) : arg as any;
199
-
200
-
201
- /**
202
- let asd = bindTo("sss",123,{abc:223});//?
203
- let sumvoid = ()=>10;
204
- let a = curry(sumvoid);//?
205
- console.log(a());
206
- let sum = (a:number,b:string,c:number,d:boolean)=>a+b+c+d;
207
- let sumCu = curry(sum);//?
208
- let suma = sumCu(1)("ss");//?
209
- let sumb = sumCu(1,"1")(2);//?
210
- let sumc = sumCu(4);//?
211
- let sumz = sumCu(1,"b",3)(false);//?
212
- console.log(suma(2,true));
213
- console.log(sumb(true));
214
- console.log(sumc("s",3,false));
215
-
216
- Reflect.ownKeys({[1]:1,[None]:2});//?
217
- let as = "ssa" as string;//?
218
- let asv = {"s":1};//?
219
- let a = map((v:number,k)=>v*21);//?
220
- let b = a({["ss"]:1})//?
221
- */
222
- }
@@ -1,320 +0,0 @@
1
- import { FfprobeData } from "fluent-ffmpeg";
2
- import fluentFfmpeg from "fluent-ffmpeg";
3
- import path from "pathe";
4
- import * as fs from "fs";
5
- import { SLogger } from "@/src/UtilLogger";
6
- import { Stream } from "./UtilClass";
7
- import { throwError } from "./QuickExport";
8
-
9
- /**输入输出路径映射
10
- * 输入路径:输入路径
11
- */
12
- export type IOMap = { [key: string]: string };
13
-
14
- /**ffmpeg工具类
15
- */
16
- class SFfmpegTool {
17
- /**静态构造函数 */
18
- static init() {
19
- const ffmpegPath = process?.env?.FFMPEG_PATH;
20
- if(ffmpegPath!=null){
21
- const exepath = path.join(ffmpegPath,"ffmpeg.exe");
22
- SFfmpegTool.setFfmpegPath(exepath);
23
- }
24
- }
25
- /**设置ffmpeg路径 */
26
- static setFfmpegPath(ffmpegPath: string) {
27
- fluentFfmpeg.setFfmpegPath(ffmpegPath);
28
- }
29
- /**获取音频文件的元数据
30
- * @param inputWavPath - 输入音频文件路径
31
- * @returns 返回音频文件的元数据
32
- */
33
- static async getAudioMetaData(
34
- inputWavPath: string
35
- ): Promise<FfprobeData | null> {
36
- return new Promise((resolve, reject) => {
37
- fluentFfmpeg.ffprobe(inputWavPath, function (err, metadata) {
38
- if (err) {
39
- SLogger.error("SFfmpegTool.getAudioMetaData 错误",err);
40
- resolve(null);
41
- } else {
42
- resolve(metadata);
43
- }
44
- });
45
- });
46
- }
47
- /**flac转ogg
48
- * @param inputFlacFile - 输入flac文件路径
49
- * @param outputOggPath - 输出ogg文件路径
50
- * @param quality - 质量
51
- */
52
- static async flac2ogg(
53
- inputFlacFile: string,
54
- outputOggPath: string,
55
- quality: number = 10
56
- ): Promise<boolean> {
57
- const wavPath = path.join(
58
- path.dirname(inputFlacFile),
59
- `tmp_${path.basename(inputFlacFile, ".flac")}.wav`
60
- );
61
- await SFfmpegTool.flac2wav(inputFlacFile, wavPath);
62
- await SFfmpegTool.wav2ogg(wavPath, outputOggPath, quality);
63
- await fs.promises.unlink(wavPath);
64
- return true;
65
- }
66
-
67
- /**flac转wav
68
- * @param inputFlacFile - 输入flac文件路径
69
- * @param outputWavPath - 输出wav文件路径
70
- */
71
- static async flac2wav(
72
- inputFlacFile: string,
73
- outputWavPath: string
74
- ): Promise<boolean> {
75
- await new Promise((resolve, reject) => {
76
- const ins = fluentFfmpeg(inputFlacFile)
77
- .audioCodec("pcm_s16le")
78
- .save(outputWavPath)
79
- .on("end", () => {
80
- resolve(true);
81
- ins.kill('SIGTERM');
82
- })
83
- .on("error", (err) => {
84
- reject(err);
85
- ins.kill('SIGTERM');
86
- });
87
- });
88
- return true;
89
- }
90
-
91
-
92
- /**wav转ogg
93
- * @param inputWavPath - 输入wav文件路径
94
- * @param outputOggPath - 输出ogg文件路径
95
- * @param quality - 质量
96
- */
97
- static async wav2ogg(
98
- inputWavPath: string,
99
- outputOggPath: string,
100
- quality: number = 10
101
- ): Promise<boolean> {
102
- //.audioQuality(10)
103
- //.audioBitrate("192k")
104
- //.audioChannels(channels)
105
- //.noMetadata()
106
- return new Promise((resolve, reject) => {
107
- const ins = fluentFfmpeg(inputWavPath)
108
- .audioQuality(quality)
109
- .audioCodec("libvorbis")
110
- .save(outputOggPath)
111
- .on("end", () => {
112
- resolve(true);
113
- ins.kill('SIGTERM');
114
- })
115
- .on("error", (err) => {
116
- reject(err);
117
- ins.kill('SIGTERM');
118
- });
119
- });
120
- }
121
- /**剪切音频
122
- * @param audioPath - 输入音频文件路径
123
- * @param outPath - 输出音频文件路径
124
- * @param start - 开始时间
125
- * @param time - 时长
126
- */
127
- static async cutAudio(
128
- audioPath: string,
129
- outPath: string,
130
- start: number,
131
- time: number
132
- ): Promise<boolean> {
133
- return new Promise((resolve, reject) => {
134
- const ins = fluentFfmpeg(audioPath)
135
- .setStartTime(start)
136
- .setDuration(time)
137
- .save(outPath)
138
- .on("end", () => {
139
- resolve(true);
140
- ins.kill('SIGTERM');
141
- })
142
- .on("error", (err) => {
143
- reject(err);
144
- ins.kill('SIGTERM');
145
- });
146
- });
147
- }
148
- /**删除首尾静音
149
- * @param inputWavPath - 输入wav文件路径
150
- * @param outputWavPath - 输出wav文件路径
151
- * @param threshold - 静音阈值/dB
152
- * @param silence - 保留静音时长
153
- */
154
- static async trimSilence(
155
- inputWavPath: string,
156
- outputWavPath: string,
157
- threshold: number = -50,
158
- silence: number = 0.1
159
- ): Promise<boolean> {
160
- return new Promise((resolve, reject) => {
161
- const ins = fluentFfmpeg(inputWavPath)
162
- .audioFilters(
163
- `silenceremove=start_periods=1:start_threshold=${threshold}dB:start_silence=${silence}:detection=peak`
164
- )
165
- .audioFilters("areverse")
166
- .audioFilters(
167
- `silenceremove=start_periods=1:start_threshold=${threshold}dB:start_silence=${silence}:detection=peak`
168
- )
169
- .audioFilters("areverse")
170
- .save(outputWavPath)
171
- .on("end", () => {
172
- resolve(true);
173
- ins.kill('SIGTERM');
174
- })
175
- .on("error", (err) => {
176
- reject(err);
177
- ins.kill('SIGTERM');
178
- });
179
- });
180
- }
181
- /**重采样
182
- * @param inputWavPath - 输入wav文件路径
183
- * @param outputWavPath - 输出wav文件路径
184
- */
185
- static async resample(inputWavPath: string, outputWavPath: string, rate: number = 22050): Promise<boolean> {
186
- return new Promise((resolve, reject) => {
187
- const ins = fluentFfmpeg(inputWavPath)
188
- .audioFrequency(rate)
189
- .save(outputWavPath)
190
- .on("end", () => {
191
- resolve(true);
192
- ins.kill('SIGTERM');
193
- })
194
- .on("error", (err) => {
195
- reject(err);
196
- ins.kill('SIGTERM');
197
- });
198
- });
199
- }
200
-
201
- /**检查WAV文件是否为单声道
202
- * @param filePath - 要检查的WAV文件路径
203
- */
204
- static async isMono(filePath: string): Promise<boolean> {
205
- const metadata = await SFfmpegTool.getAudioMetaData(filePath);
206
- if (metadata==null) throwError("SFfmpegTool.isMono 获取音频元数据失败");
207
- // 检查音频流的声道数
208
- const audioStream = metadata!.streams.find(stream => stream.codec_type === 'audio');
209
- if (audioStream==null) throwError("SFfmpegTool.isMono 未找到音频流");
210
- return audioStream!.channels === 1;
211
- }
212
-
213
- //多线程处理
214
- /**wav转ogg多线程
215
- * @param ioMap - 输入输出路径映射
216
- * @param quality - 质量
217
- * @param cpCount - 并发数
218
- */
219
- static async wav2oggMP(ioMap: IOMap, quality = 10, cpCount = 16) {
220
- await Stream.from(Object.entries(ioMap))
221
- .concurrent(cpCount)
222
- .map(async ([inPath, outPath]) => {
223
- SLogger.info(`SFfmpegTool.wav2oggMP 正在处理:${outPath}`);
224
- await SFfmpegTool.wav2ogg(inPath, outPath, quality);
225
- })
226
- .append();
227
- }
228
- /**flac转ogg多线程
229
- * @param ioMap - 输入输出路径映射
230
- * @param quality - 质量
231
- * @param cpCount - 并发数
232
- */
233
- static async flac2oggMP(
234
- ioMap: IOMap,
235
- quality: number = 10,
236
- cpCount: number = 16
237
- ) {
238
- await Stream.from(Object.entries(ioMap))
239
- .concurrent(cpCount)
240
- .map(async ([inPath, outPath]) => {
241
- SLogger.info(`SFfmpegTool.flac2oggMP 正在处理:${outPath}`);
242
- await SFfmpegTool.flac2ogg(inPath, outPath, quality);
243
- })
244
- .append();
245
- }
246
- /**删除静音多线程
247
- * @param ioMap - 输入输出路径映射
248
- * @param threshold - 静音阈值/dB
249
- * @param silence - 保留静音时长
250
- */
251
- static async trimSilenceMP(
252
- ioMap: IOMap,
253
- threshold: number = -50,
254
- silence: number = 0.1,
255
- cpCount: number = 16
256
- ) {
257
- await Stream.from(Object.entries(ioMap))
258
- .concurrent(cpCount)
259
- .map(async ([inPath, outPath]) => {
260
- SLogger.info(`SFfmpegTool.trimSilenceMP 正在处理:${outPath}`);
261
- await SFfmpegTool.trimSilence(inPath, outPath, threshold, silence);
262
- })
263
- .append();
264
- }
265
-
266
- /**重采样多线程
267
- * @param ioMap - 输入输出路径映射
268
- * @param rate - 采样率
269
- * @param cpCount - 并发数
270
- */
271
- static async resampleMP(ioMap: IOMap, rate: number = 22050, cpCount: number = 16) {
272
- await Stream.from(Object.entries(ioMap))
273
- .concurrent(cpCount)
274
- .map(async ([inPath, outPath]) => {
275
- SLogger.info(`SFfmpegTool.resampleMP 正在处理:${outPath}`);
276
- await SFfmpegTool.resample(inPath, outPath, rate);
277
- })
278
- .append();
279
- }
280
- }
281
- SFfmpegTool.init();
282
-
283
-
284
- /**多线程任务分割器
285
- * @param iomap - 输入输出路径映射
286
- * @param cpCount - 并发数
287
- */
288
- function MPClip(iomap: IOMap, cpCount: number = 16) {
289
- let cpList: Array<IOMap> = [];
290
- for (let i = 0; i < cpCount; i++) cpList.push({});
291
- let cpCounter = 0;
292
- for (let key in iomap) {
293
- let cpIndex = cpCounter % cpCount;
294
- cpCounter++;
295
- cpList[cpIndex][key] = iomap[key];
296
- }
297
- return cpList;
298
- }
299
-
300
- type SFfmpegCmd = {
301
- name: string;
302
- opt: string | null;
303
- };
304
-
305
- /**ffmpeg流
306
- */
307
- class SFfmpegStream {
308
- iomap;
309
- cmdList: Array<SFfmpegCmd> = [];
310
- constructor(iomap: IOMap) {
311
- this.iomap = iomap;
312
- }
313
- async save() {}
314
-
315
- //执行函数
316
- private async cmdAreverse() {}
317
- }
318
-
319
- export default SFfmpegTool;
320
- export { SFfmpegTool };