@zwa73/utils 1.0.202 → 1.0.204
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/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/package.json +7 -4
- package/compile.bat +0 -2
- package/dist/UtilFfmpegTools.d.ts +0 -86
- package/dist/UtilFfmpegTools.js +0 -291
- package/input.wav +0 -0
- package/jest.config.js +0 -16
- package/scripts/compile.ps1 +0 -6
- package/scripts/expand-macro.ps1 +0 -2
- package/scripts/postinstall.js +0 -61
- package/scripts/release.ps1 +0 -3
- package/scripts/watch.ps1 +0 -10
- package/src/QuickExport.ts +0 -70
- package/src/UtilClass.ts +0 -259
- package/src/UtilCodecs.ts +0 -102
- package/src/UtilCom.macro.ts +0 -28
- package/src/UtilCom.ts +0 -456
- package/src/UtilCom_bak.txt +0 -345
- package/src/UtilDecorators.ts +0 -223
- package/src/UtilFP.macro.ts +0 -29
- package/src/UtilFP.ts +0 -222
- package/src/UtilFfmpegTools.ts +0 -320
- package/src/UtilFileTools.ts +0 -410
- package/src/UtilFunctions.ts +0 -938
- package/src/UtilI18n.ts +0 -278
- package/src/UtilInterfaces.ts +0 -264
- package/src/UtilLogger.ts +0 -390
- package/src/UtilSymbol.ts +0 -45
- package/src/backup.txt +0 -102
- package/src/index.ts +0 -13
- package/test.bat +0 -2
- package/watch.bat +0 -1
package/src/UtilClass.ts
DELETED
|
@@ -1,259 +0,0 @@
|
|
|
1
|
-
import { matchProc } from "./QuickExport";
|
|
2
|
-
import { Keyable, Literal, MPromise } from "./UtilInterfaces";
|
|
3
|
-
import Handlebars from 'handlebars';
|
|
4
|
-
import { SLogger } from "./UtilLogger";
|
|
5
|
-
|
|
6
|
-
/**函数管道器 */
|
|
7
|
-
export class Piper<Arg, Result = Arg> {
|
|
8
|
-
/**管道函数列表 */
|
|
9
|
-
private funcs: Function[] = [];
|
|
10
|
-
private constructor(){};
|
|
11
|
-
/**添加单个参数与返回值不同的函数 */
|
|
12
|
-
public static pipe<Arg, Result>(func: (arg: Arg) => Result): Piper<Arg, Result>;
|
|
13
|
-
/**添加多个参数与返回值相同的函数 */
|
|
14
|
-
public static pipe<Arg>(func:((arg: Arg) => Arg), ...funcs: ((arg: Arg) => Arg)[]): Piper<Arg>;
|
|
15
|
-
public static pipe(...funcs: Function[]){
|
|
16
|
-
const newPiper = new Piper<any>();
|
|
17
|
-
newPiper.funcs = [...funcs];
|
|
18
|
-
return newPiper;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**添加单个参数与返回值不同的函数 */
|
|
22
|
-
public pipe<T>(func: (arg: Result) => T): Piper<Arg, T>;
|
|
23
|
-
/**添加多个参数与返回值相同的函数 */
|
|
24
|
-
public pipe(func:((arg: Result) => Result), ...funcs: ((arg: Result) => Result)[]): Piper<Arg, Result>;
|
|
25
|
-
public pipe(...funcs: Function[]): Piper<Arg, any> {
|
|
26
|
-
const newPiper = new Piper<Arg>();
|
|
27
|
-
newPiper.funcs = [...this.funcs, ...funcs];
|
|
28
|
-
return newPiper;
|
|
29
|
-
}
|
|
30
|
-
/**管道函数 */
|
|
31
|
-
public pipeline():(arg:Arg)=>Result {
|
|
32
|
-
return (arg:Arg) => this.funcs.reduce((value, func) => func(value), arg) as any as Result;
|
|
33
|
-
}
|
|
34
|
-
/**直接调用 */
|
|
35
|
-
public invoke(arg:Arg):Result {
|
|
36
|
-
return this.funcs.reduce((value, func) => func(value), arg) as any as Result;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
//#region 并行流
|
|
42
|
-
type StreamOperation<T, U> = (item: T)=>MPromise<U>;
|
|
43
|
-
/**并行流 */
|
|
44
|
-
export class Stream<T> implements Iterable<T>{
|
|
45
|
-
/**并发数*/
|
|
46
|
-
private _concurrent: number;
|
|
47
|
-
private _list: T[];
|
|
48
|
-
/**加工函数列表*/
|
|
49
|
-
private _operation: Array<StreamOperation<T, T>> = [];
|
|
50
|
-
private constructor(base?: Array<T>|number, concurrent: number = 1) {
|
|
51
|
-
if(base==undefined) this._list = new Array();
|
|
52
|
-
else if(typeof base === 'number') this._list = new Array(base);
|
|
53
|
-
else this._list = new Array(...base);
|
|
54
|
-
this._concurrent = concurrent;
|
|
55
|
-
}
|
|
56
|
-
/**从arraylike创建流
|
|
57
|
-
* @param args - arraylike
|
|
58
|
-
* @param concurrent - 并发数 默认 1
|
|
59
|
-
*/
|
|
60
|
-
static from<T>(args:ArrayLike<T>,concurrent:number=1){
|
|
61
|
-
return new Stream(Array.from(args),concurrent);
|
|
62
|
-
}
|
|
63
|
-
/**从数组创建流 */
|
|
64
|
-
static of<T>(...args:T[]){
|
|
65
|
-
return new Stream(args);
|
|
66
|
-
}
|
|
67
|
-
/**设置并发数 */
|
|
68
|
-
concurrent(count: number): Stream<T> {
|
|
69
|
-
this._concurrent = count;
|
|
70
|
-
return this;
|
|
71
|
-
}
|
|
72
|
-
/**平分数组
|
|
73
|
-
* @param count - 份数
|
|
74
|
-
* @param mode - 模式 average:轮询平均分 chunk:切块均分
|
|
75
|
-
* @returns 新数组
|
|
76
|
-
*/
|
|
77
|
-
private divide(count: number, mode: "average" | "chunk" = "average"): T[][] {
|
|
78
|
-
if (count <= 0) return [];
|
|
79
|
-
if (count == 1) return [[...this]];
|
|
80
|
-
const size = this.length;
|
|
81
|
-
const result:T[][] = [];
|
|
82
|
-
|
|
83
|
-
matchProc(mode,{
|
|
84
|
-
//轮询平均分
|
|
85
|
-
'average':()=>{
|
|
86
|
-
for (let i = 0; i < count; i++) {
|
|
87
|
-
const clist:T[] = [];
|
|
88
|
-
for (let j = i; j < size; j += count)
|
|
89
|
-
clist.push(this._list[j]);
|
|
90
|
-
result.push(clist);
|
|
91
|
-
}
|
|
92
|
-
},
|
|
93
|
-
//切块均分
|
|
94
|
-
'chunk':()=>{
|
|
95
|
-
const chunkSize = Math.ceil(size / count);
|
|
96
|
-
for (let i = 0; i < count; i++) {
|
|
97
|
-
const start = i * chunkSize;
|
|
98
|
-
let end = (i + 1) * chunkSize;
|
|
99
|
-
if (end > size) end = size;
|
|
100
|
-
result.push(this._list.slice(start, end));
|
|
101
|
-
}
|
|
102
|
-
},
|
|
103
|
-
})
|
|
104
|
-
return result;
|
|
105
|
-
}
|
|
106
|
-
/**转换流
|
|
107
|
-
* @param operation - 加工函数
|
|
108
|
-
* @returns 新流
|
|
109
|
-
*/
|
|
110
|
-
private trans<U>(operation:StreamOperation<T, U>): Stream<U>{
|
|
111
|
-
const ns = new Stream(this._list,this._concurrent);
|
|
112
|
-
ns._operation.push(...this._operation);
|
|
113
|
-
ns._operation.push(operation as any);
|
|
114
|
-
return ns as any;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**流式的 映射加工
|
|
118
|
-
* @param operation - 加工函数
|
|
119
|
-
* @returns 新流
|
|
120
|
-
*/
|
|
121
|
-
map<U>(operation: StreamOperation<T, U>): Stream<U> {
|
|
122
|
-
return this.trans(operation);
|
|
123
|
-
}
|
|
124
|
-
/**流式的 遍历
|
|
125
|
-
* 返回自身
|
|
126
|
-
* @param operation - 遍历函数
|
|
127
|
-
* @returns 自身
|
|
128
|
-
*/
|
|
129
|
-
each(operation: StreamOperation<T, void>): Stream<T> {
|
|
130
|
-
const opera = async (item: T) => {
|
|
131
|
-
await operation(item);
|
|
132
|
-
return item;
|
|
133
|
-
};
|
|
134
|
-
return this.trans(opera);;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
//终结操作
|
|
138
|
-
/**应用加工
|
|
139
|
-
* @returns 自身
|
|
140
|
-
*/
|
|
141
|
-
async append(): Promise<Stream<T>> {
|
|
142
|
-
if (this._operation.length == 0) return this;
|
|
143
|
-
|
|
144
|
-
//均分处理
|
|
145
|
-
const pList = this.divide(this._concurrent)
|
|
146
|
-
.map(async (subList)=>{
|
|
147
|
-
const result:T[] = [];
|
|
148
|
-
for (let item of subList) {
|
|
149
|
-
for (const operation of this._operation)
|
|
150
|
-
item = await operation(item);
|
|
151
|
-
result.push(item);
|
|
152
|
-
}
|
|
153
|
-
return (result);
|
|
154
|
-
});
|
|
155
|
-
const rlist = await Promise.all(pList);
|
|
156
|
-
|
|
157
|
-
//拼接结果 轮询均分
|
|
158
|
-
const result = new Array(this.length).fill(undefined);
|
|
159
|
-
rlist.forEach((subList,i)=>{
|
|
160
|
-
if (!subList) return;
|
|
161
|
-
const subSize = subList.length;
|
|
162
|
-
for (let j = 0; j < subSize; j++)
|
|
163
|
-
result[i + j * this._concurrent] = subList[j];
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
this._list = result;
|
|
167
|
-
this._operation = [];
|
|
168
|
-
return this;
|
|
169
|
-
}
|
|
170
|
-
/**应用加工 并转换为数组
|
|
171
|
-
* @returns 数组
|
|
172
|
-
*/
|
|
173
|
-
async toArray(): Promise<Array<T>> {
|
|
174
|
-
await this.append();
|
|
175
|
-
return [...this];
|
|
176
|
-
}
|
|
177
|
-
/**应用加工 并排除某个字面量
|
|
178
|
-
* @param value - 排除的值
|
|
179
|
-
* @returns 自身
|
|
180
|
-
*/
|
|
181
|
-
async exclude<E>(value:Literal<E>): Promise<Stream<Exclude<T,E>>>{
|
|
182
|
-
await this.append();
|
|
183
|
-
return new Stream(this._list.filter((v)=>v!==value as any) as Exclude<T,E>[]);
|
|
184
|
-
}
|
|
185
|
-
/**应用加工 并过滤
|
|
186
|
-
* @param func - 过滤函数
|
|
187
|
-
* @returns 自身
|
|
188
|
-
*/
|
|
189
|
-
async filter(func:(value: T, index: number, array: T[])=>boolean): Promise<Stream<T>>{
|
|
190
|
-
await this.append();
|
|
191
|
-
return new Stream(this._list.filter(func));
|
|
192
|
-
}
|
|
193
|
-
//迭代器
|
|
194
|
-
[Symbol.iterator](): Iterator<T> {
|
|
195
|
-
let index = 0;
|
|
196
|
-
const data = this._list;
|
|
197
|
-
|
|
198
|
-
return {
|
|
199
|
-
next: () => ({
|
|
200
|
-
value: data[index++],
|
|
201
|
-
done: index > data.length
|
|
202
|
-
})
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
// 创建 length 属性的 getter 和 setter
|
|
207
|
-
get length(): number {
|
|
208
|
-
return this._list.length;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
//#endregion
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
/**文本模板渲染器
|
|
215
|
-
* @template T - 上下文对象的类型,默认为记录类型
|
|
216
|
-
*/
|
|
217
|
-
export class Hbs<T extends Record<Keyable,any> = Record<Keyable,any>>{
|
|
218
|
-
hbs = Handlebars.create();
|
|
219
|
-
context:T;
|
|
220
|
-
|
|
221
|
-
/**创建一个新的 Hbs 实例
|
|
222
|
-
* @param context - 上下文对象,用于在渲染模板时提供数据
|
|
223
|
-
*/
|
|
224
|
-
constructor(context:T={} as T) {
|
|
225
|
-
this.context = context;
|
|
226
|
-
|
|
227
|
-
this.hbs.registerHelper('def', (name, options)=> {
|
|
228
|
-
const value = options.fn(this.context).trim();
|
|
229
|
-
(this.context as any)[name] = value;
|
|
230
|
-
return '';
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
this.hbs.registerHelper('defli', (name,options)=> {
|
|
234
|
-
const rawvalue = options.fn(this.context) as string;
|
|
235
|
-
|
|
236
|
-
const values = rawvalue
|
|
237
|
-
.split('[[br]]')
|
|
238
|
-
.map(value => value.trim());
|
|
239
|
-
(this.context as any)[name] = values;
|
|
240
|
-
return '';
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**渲染模板
|
|
245
|
-
* @param template - 要渲染的 Handlebars 模板字符串
|
|
246
|
-
* @returns 渲染后的字符串
|
|
247
|
-
*/
|
|
248
|
-
render(template:string):string{
|
|
249
|
-
try{
|
|
250
|
-
return this.hbs.compile(template)(this.context);
|
|
251
|
-
} catch(e){
|
|
252
|
-
SLogger.error(e);
|
|
253
|
-
return '';
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
package/src/UtilCodecs.ts
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import * as he from 'html-entities';
|
|
2
|
-
import {get_encoding,Tiktoken} from 'tiktoken';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**编码/解码器 */
|
|
7
|
-
export namespace UtilCodec{
|
|
8
|
-
|
|
9
|
-
/**gpt-4, gpt-3.5-turbo, text-embedding-ada-002, text-embedding-3-small, text-embedding-3-large
|
|
10
|
-
*/
|
|
11
|
-
let encoderCl100kBase:Tiktoken|null = null;
|
|
12
|
-
/**Codex models, text-davinci-002, text-davinci-003
|
|
13
|
-
*/
|
|
14
|
-
let encoderP50kBase:Tiktoken|null = null;
|
|
15
|
-
const textDecoder = new TextDecoder();
|
|
16
|
-
|
|
17
|
-
/**HTML实体解码 将一个字符串中的HTML实体转换为对应的字符
|
|
18
|
-
* @param str - 要转换的字符串
|
|
19
|
-
* @returns 转换后的字符串
|
|
20
|
-
*/
|
|
21
|
-
export function decodeHtmlEntities(str:string) {
|
|
22
|
-
return he.decode(str);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**HTML实体编码 将一个字符串中的 需编码字符转换为 HTML实体
|
|
26
|
-
* @param str - 要转换的字符串
|
|
27
|
-
* @returns 转换后的字符串
|
|
28
|
-
*/
|
|
29
|
-
export function encodeHtmlEntities(str:string) {
|
|
30
|
-
return he.encode(str);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
//#region LAM
|
|
34
|
-
//token长度计算器
|
|
35
|
-
//cl100k_base ChatGPT models, text-embedding-ada-002
|
|
36
|
-
//p50k_base Code models, text-davinci-002, text-davinci-003
|
|
37
|
-
//r50k_base (or gpt2) GPT-3 models like davinci
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
//避免在nextjs调用时出错
|
|
41
|
-
function initTikTokenEncoder (){
|
|
42
|
-
if(encoderCl100kBase!=null && encoderP50kBase!=null)
|
|
43
|
-
return;
|
|
44
|
-
|
|
45
|
-
encoderCl100kBase = get_encoding("cl100k_base");
|
|
46
|
-
encoderP50kBase = get_encoding("p50k_base");
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**token长度计算器 Turbo模型
|
|
50
|
-
* @param str = 所要计算的消息
|
|
51
|
-
* @returns 整数长度结果
|
|
52
|
-
*/
|
|
53
|
-
export function tokenNumTurbo(str:string):number{
|
|
54
|
-
initTikTokenEncoder();
|
|
55
|
-
//return encoder.encode(str).length
|
|
56
|
-
return encoderCl100kBase?.encode(str).length as any as number;
|
|
57
|
-
}
|
|
58
|
-
/**token长度计算器 Davinci模型
|
|
59
|
-
* @param str = 所要计算的消息
|
|
60
|
-
* @returns 整数长度结果
|
|
61
|
-
*/
|
|
62
|
-
export function tokenNumDavinci(str:string):number{
|
|
63
|
-
initTikTokenEncoder();
|
|
64
|
-
return encoderP50kBase?.encode(str).length as any as number;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**token编码 Turbo模型
|
|
68
|
-
* @param str = 所要计算的消息
|
|
69
|
-
* @returns Token数组
|
|
70
|
-
*/
|
|
71
|
-
export function encodeTokenTurbo(str:string):Uint32Array{
|
|
72
|
-
initTikTokenEncoder();
|
|
73
|
-
return encoderCl100kBase?.encode(str) as any as Uint32Array
|
|
74
|
-
}
|
|
75
|
-
/**token编码 Davinci模型
|
|
76
|
-
* @param str = 所要计算的消息
|
|
77
|
-
* @returns Token数组
|
|
78
|
-
*/
|
|
79
|
-
export function encodeTokenDavinci(str:string):Uint32Array{
|
|
80
|
-
initTikTokenEncoder();
|
|
81
|
-
return encoderP50kBase?.encode(str) as any as Uint32Array;
|
|
82
|
-
}
|
|
83
|
-
/**token解码 Turbo模型
|
|
84
|
-
* @param arr = Token数组
|
|
85
|
-
* @returns 消息字符串
|
|
86
|
-
*/
|
|
87
|
-
export function decodeTokenTurbo(arr:Uint32Array|number[]):string{
|
|
88
|
-
initTikTokenEncoder();
|
|
89
|
-
if(Array.isArray(arr)) arr = new Uint32Array(arr);
|
|
90
|
-
return textDecoder.decode(encoderCl100kBase?.decode(arr));
|
|
91
|
-
}
|
|
92
|
-
/**token解码 Davinci模型
|
|
93
|
-
* @param arr = Token数组
|
|
94
|
-
* @returns 消息字符串
|
|
95
|
-
*/
|
|
96
|
-
export function decodeTokenDavinci(arr:Uint32Array|number[]):string{
|
|
97
|
-
initTikTokenEncoder();
|
|
98
|
-
if(Array.isArray(arr)) arr = new Uint32Array(arr);
|
|
99
|
-
return textDecoder.decode(encoderP50kBase?.decode(arr));
|
|
100
|
-
}
|
|
101
|
-
//#endregion
|
|
102
|
-
}
|
package/src/UtilCom.macro.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { regionMacro } from "@zwa73/dev-utils";
|
|
2
|
-
import { dedent } from "./QuickExport";
|
|
3
|
-
|
|
4
|
-
const sep1 = ["http","https"];
|
|
5
|
-
const sep2 = ["get","post"];
|
|
6
|
-
const AcceptTypeList = ["json","raw"]as const;
|
|
7
|
-
const SendTypeList = ["json","query","formData","none","raw"] as const;
|
|
8
|
-
const sep3 = SendTypeList;
|
|
9
|
-
const up = (str:string)=>{
|
|
10
|
-
if (str.length === 0) return str; // 处理空字符串
|
|
11
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
12
|
-
}
|
|
13
|
-
if(false)
|
|
14
|
-
regionMacro("UtilCom宏定义",()=>{
|
|
15
|
-
const result:string[] = [];
|
|
16
|
-
sep1.forEach(s1=>{
|
|
17
|
-
sep2.forEach(s2=>{
|
|
18
|
-
sep3.filter(s=>s!='raw').forEach(s3=>{
|
|
19
|
-
result.push(dedent`
|
|
20
|
-
/** 宏 UtilCom.${s1}().${s2}().send('${s3}').acceptJson() */
|
|
21
|
-
static ${s1}${up(s2)}${up(s3)}(){
|
|
22
|
-
return UtilCom.${s1}().${s2}().send('${s3}' as const).acceptJson();
|
|
23
|
-
}`.trim());
|
|
24
|
-
})
|
|
25
|
-
})
|
|
26
|
-
})
|
|
27
|
-
return result.reverse().join('\n');
|
|
28
|
-
});
|