@zwa73/utils 1.0.34 → 1.0.35
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/UtilCodecs.d.ts +43 -40
- package/dist/UtilCodecs.js +110 -106
- package/dist/UtilCom.d.ts +21 -18
- package/dist/UtilCom.js +88 -84
- package/dist/UtilFileTools.d.ts +81 -78
- package/dist/UtilFileTools.js +171 -167
- package/dist/UtilInterfaces.d.ts +6 -0
- package/package.json +1 -1
- package/src/UtilCodecs.ts +9 -1
- package/src/UtilCom.ts +3 -0
- package/src/UtilDecorators.ts +1 -0
- package/src/UtilFileTools.ts +4 -2
- package/src/UtilInterfaces.ts +10 -1
package/dist/UtilCodecs.d.ts
CHANGED
|
@@ -1,40 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
1
|
+
/**编码/解码器 */
|
|
2
|
+
export declare namespace UtilCodec {
|
|
3
|
+
/**HTML实体解码 将一个字符串中的HTML实体转换为对应的字符
|
|
4
|
+
* @param {string} str - 要转换的字符串
|
|
5
|
+
* @returns {string} 转换后的字符串
|
|
6
|
+
*/
|
|
7
|
+
function decodeHtmlEntities(str: string): string;
|
|
8
|
+
/**HTML实体编码 将一个字符串中的 需编码字符转换为 HTML实体
|
|
9
|
+
* @param {string} str - 要转换的字符串
|
|
10
|
+
* @returns {string} 转换后的字符串
|
|
11
|
+
*/
|
|
12
|
+
function encodeHtmlEntities(str: string): string;
|
|
13
|
+
/**token长度计算器 Turbo模型
|
|
14
|
+
* @param {string} str = 所要计算的消息
|
|
15
|
+
* @returns {number} 整数长度结果
|
|
16
|
+
*/
|
|
17
|
+
function tokenNumTurbo(str: string): number;
|
|
18
|
+
/**token长度计算器 Davinci模型
|
|
19
|
+
* @param {string} str = 所要计算的消息
|
|
20
|
+
* @returns {number} 整数长度结果
|
|
21
|
+
*/
|
|
22
|
+
function tokenNumDavinci(str: string): number;
|
|
23
|
+
/**token编码 Turbo模型
|
|
24
|
+
* @param {string} str = 所要计算的消息
|
|
25
|
+
* @returns {Array<number>} Token数组
|
|
26
|
+
*/
|
|
27
|
+
function encodeTokenTurbo(str: string): Uint32Array;
|
|
28
|
+
/**token编码 Davinci模型
|
|
29
|
+
* @param {string} str = 所要计算的消息
|
|
30
|
+
* @returns {Array<number>} Token数组
|
|
31
|
+
*/
|
|
32
|
+
function encodeTokenDavinci(str: string): Uint32Array;
|
|
33
|
+
/**token解码 Turbo模型
|
|
34
|
+
* @param {Array<number>} arr = Token数组
|
|
35
|
+
* @returns {string} 消息字符串
|
|
36
|
+
*/
|
|
37
|
+
function decodeTokenTurbo(arr: Uint32Array): string;
|
|
38
|
+
/**token解码 Davinci模型
|
|
39
|
+
* @param {Array<number>} arr = Token数组
|
|
40
|
+
* @returns {string} 消息字符串
|
|
41
|
+
*/
|
|
42
|
+
function decodeTokenDavinci(arr: Uint32Array): string;
|
|
43
|
+
}
|
package/dist/UtilCodecs.js
CHANGED
|
@@ -1,110 +1,114 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.UtilCodec = void 0;
|
|
4
4
|
const he = require("html-entities");
|
|
5
5
|
const tiktoken_1 = require("tiktoken");
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
let
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
//
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
6
|
+
/**编码/解码器 */
|
|
7
|
+
var UtilCodec;
|
|
8
|
+
(function (UtilCodec) {
|
|
9
|
+
let encoderTurbo = null;
|
|
10
|
+
let encoderDavinci = null;
|
|
11
|
+
const textDecoder = new TextDecoder();
|
|
12
|
+
// 定义一个对象,存储常见的HTML实体和对应的字符
|
|
13
|
+
let htmlEntities = {
|
|
14
|
+
"<": "<",
|
|
15
|
+
">": ">",
|
|
16
|
+
"&": "&",
|
|
17
|
+
""": "\"",
|
|
18
|
+
"'": "'",
|
|
19
|
+
"[": "[",
|
|
20
|
+
"]": "]"
|
|
21
|
+
};
|
|
22
|
+
/**HTML实体解码 将一个字符串中的HTML实体转换为对应的字符
|
|
23
|
+
* @param {string} str - 要转换的字符串
|
|
24
|
+
* @returns {string} 转换后的字符串
|
|
25
|
+
*/
|
|
26
|
+
function decodeHtmlEntities(str) {
|
|
27
|
+
//for(let code in htmlEntities){
|
|
28
|
+
// let cha = htmlEntities[code]
|
|
29
|
+
// str = str.replaceAll(code, cha);
|
|
30
|
+
//}
|
|
31
|
+
//return str
|
|
32
|
+
return he.decode(str);
|
|
33
|
+
}
|
|
34
|
+
UtilCodec.decodeHtmlEntities = decodeHtmlEntities;
|
|
35
|
+
/**HTML实体编码 将一个字符串中的 需编码字符转换为 HTML实体
|
|
36
|
+
* @param {string} str - 要转换的字符串
|
|
37
|
+
* @returns {string} 转换后的字符串
|
|
38
|
+
*/
|
|
39
|
+
function encodeHtmlEntities(str) {
|
|
40
|
+
//for(let code in htmlEntities){
|
|
41
|
+
// let cha = htmlEntities[code]
|
|
42
|
+
// str = str.replaceAll(cha, code);
|
|
43
|
+
//}
|
|
44
|
+
//return str
|
|
45
|
+
return he.encode(str);
|
|
46
|
+
}
|
|
47
|
+
UtilCodec.encodeHtmlEntities = encodeHtmlEntities;
|
|
48
|
+
//token长度计算器
|
|
49
|
+
//cl100k_base ChatGPT models, text-embedding-ada-002
|
|
50
|
+
//p50k_base Code models, text-davinci-002, text-davinci-003
|
|
51
|
+
//r50k_base (or gpt2) GPT-3 models like davinci
|
|
52
|
+
//避免在nextjs调用时出错
|
|
53
|
+
function initTikTokenEncoder() {
|
|
54
|
+
if (encoderTurbo != null && encoderDavinci != null)
|
|
55
|
+
return;
|
|
56
|
+
encoderTurbo = (0, tiktoken_1.get_encoding)("cl100k_base");
|
|
57
|
+
encoderDavinci = (0, tiktoken_1.get_encoding)("p50k_base");
|
|
58
|
+
}
|
|
59
|
+
/**token长度计算器 Turbo模型
|
|
60
|
+
* @param {string} str = 所要计算的消息
|
|
61
|
+
* @returns {number} 整数长度结果
|
|
62
|
+
*/
|
|
63
|
+
function tokenNumTurbo(str) {
|
|
64
|
+
initTikTokenEncoder();
|
|
65
|
+
//return encoder.encode(str).length
|
|
66
|
+
return encoderTurbo?.encode(str).length;
|
|
67
|
+
}
|
|
68
|
+
UtilCodec.tokenNumTurbo = tokenNumTurbo;
|
|
69
|
+
/**token长度计算器 Davinci模型
|
|
70
|
+
* @param {string} str = 所要计算的消息
|
|
71
|
+
* @returns {number} 整数长度结果
|
|
72
|
+
*/
|
|
73
|
+
function tokenNumDavinci(str) {
|
|
74
|
+
initTikTokenEncoder();
|
|
75
|
+
return encoderDavinci?.encode(str).length;
|
|
76
|
+
}
|
|
77
|
+
UtilCodec.tokenNumDavinci = tokenNumDavinci;
|
|
78
|
+
/**token编码 Turbo模型
|
|
79
|
+
* @param {string} str = 所要计算的消息
|
|
80
|
+
* @returns {Array<number>} Token数组
|
|
81
|
+
*/
|
|
82
|
+
function encodeTokenTurbo(str) {
|
|
83
|
+
initTikTokenEncoder();
|
|
84
|
+
return encoderTurbo?.encode(str);
|
|
85
|
+
}
|
|
86
|
+
UtilCodec.encodeTokenTurbo = encodeTokenTurbo;
|
|
87
|
+
/**token编码 Davinci模型
|
|
88
|
+
* @param {string} str = 所要计算的消息
|
|
89
|
+
* @returns {Array<number>} Token数组
|
|
90
|
+
*/
|
|
91
|
+
function encodeTokenDavinci(str) {
|
|
92
|
+
initTikTokenEncoder();
|
|
93
|
+
return encoderDavinci?.encode(str);
|
|
94
|
+
}
|
|
95
|
+
UtilCodec.encodeTokenDavinci = encodeTokenDavinci;
|
|
96
|
+
/**token解码 Turbo模型
|
|
97
|
+
* @param {Array<number>} arr = Token数组
|
|
98
|
+
* @returns {string} 消息字符串
|
|
99
|
+
*/
|
|
100
|
+
function decodeTokenTurbo(arr) {
|
|
101
|
+
initTikTokenEncoder();
|
|
102
|
+
return textDecoder.decode(encoderTurbo?.decode(arr));
|
|
103
|
+
}
|
|
104
|
+
UtilCodec.decodeTokenTurbo = decodeTokenTurbo;
|
|
105
|
+
/**token解码 Davinci模型
|
|
106
|
+
* @param {Array<number>} arr = Token数组
|
|
107
|
+
* @returns {string} 消息字符串
|
|
108
|
+
*/
|
|
109
|
+
function decodeTokenDavinci(arr) {
|
|
110
|
+
initTikTokenEncoder();
|
|
111
|
+
return textDecoder.decode(encoderDavinci?.decode(arr));
|
|
112
|
+
}
|
|
113
|
+
UtilCodec.decodeTokenDavinci = decodeTokenDavinci;
|
|
114
|
+
})(UtilCodec = exports.UtilCodec || (exports.UtilCodec = {}));
|
package/dist/UtilCom.d.ts
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
import { JObject } from "./UtilInterfaces";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
2
|
+
/**网络工具 */
|
|
3
|
+
export declare namespace UtilCom {
|
|
4
|
+
/**发送一个POST请求并接受数据
|
|
5
|
+
* Object ()
|
|
6
|
+
* @async
|
|
7
|
+
* @param {JObject} json - 数据对象
|
|
8
|
+
* @param {Object} options - 参数对象
|
|
9
|
+
* @param {number} [timeLimit] - 超时时间/秒 最小为10秒
|
|
10
|
+
* @returns {Promise<Object|null>} 结果 null 为未能成功接收
|
|
11
|
+
*/
|
|
12
|
+
function shttpsPost(json: JObject, options: Object, timeLimit?: number): Promise<JObject | null>;
|
|
13
|
+
/**发送一个POST请求并接受数据
|
|
14
|
+
* Object ()
|
|
15
|
+
* @async
|
|
16
|
+
* @param {JObject} json - 数据对象
|
|
17
|
+
* @param {Object} options - 参数对象
|
|
18
|
+
* @param {number} [timeLimit] - 超时时间/秒 最小为10秒
|
|
19
|
+
* @returns {Promise<Object|null>} 结果 null 为未能成功接收
|
|
20
|
+
*/
|
|
21
|
+
function shttpPost(json: JObject, options: Object, timeLimit?: number): Promise<JObject | null>;
|
|
22
|
+
}
|
package/dist/UtilCom.js
CHANGED
|
@@ -1,97 +1,101 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.UtilCom = void 0;
|
|
4
4
|
const UtilInterfaces_1 = require("./UtilInterfaces");
|
|
5
5
|
const https = require("https");
|
|
6
6
|
const http = require("http");
|
|
7
7
|
const UtilLogger_1 = require("./UtilLogger");
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
8
|
+
/**网络工具 */
|
|
9
|
+
var UtilCom;
|
|
10
|
+
(function (UtilCom) {
|
|
11
|
+
function sPost(type, json, options, timeLimit = -1) {
|
|
12
|
+
//转换为毫秒
|
|
13
|
+
const hasTimeLimit = (timeLimit >= 10);
|
|
14
|
+
if (hasTimeLimit)
|
|
15
|
+
timeLimit *= 1000;
|
|
16
|
+
const jsonStr = (0, UtilInterfaces_1.stringifyJToken)(json);
|
|
17
|
+
const funcName = "s" + type + "Psot";
|
|
18
|
+
return new Promise((resolve, rejecte) => {
|
|
19
|
+
const resFunc = (res) => {
|
|
20
|
+
try {
|
|
21
|
+
//请求超时
|
|
22
|
+
if (hasTimeLimit) {
|
|
23
|
+
res.setTimeout(timeLimit, () => {
|
|
24
|
+
//res.abort();
|
|
25
|
+
throw funcName + " 接收反馈超时: " + timeLimit + " ms";
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
let resdata = "";
|
|
29
|
+
res.setEncoding('utf8');
|
|
30
|
+
res.on('data', (chunk) => resdata += chunk);
|
|
31
|
+
res.on('error', (e) => {
|
|
32
|
+
throw funcName + " 接收反馈错误:" + e;
|
|
33
|
+
});
|
|
34
|
+
res.on('end', () => {
|
|
35
|
+
if (resdata == "")
|
|
36
|
+
throw funcName + " 接收反馈错误: resdata 为空";
|
|
37
|
+
try {
|
|
38
|
+
let obj = JSON.parse(resdata);
|
|
39
|
+
UtilLogger_1.SLogger.http(funcName + " 接受信息:", obj);
|
|
40
|
+
resolve(obj);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
throw funcName + " 接收反馈错误:" + e + "\n原始字符串:" + resdata;
|
|
45
|
+
}
|
|
23
46
|
});
|
|
24
47
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
48
|
+
catch (err) {
|
|
49
|
+
if (typeof err != "string")
|
|
50
|
+
throw err;
|
|
51
|
+
UtilLogger_1.SLogger.warn(err);
|
|
52
|
+
resolve(null);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
//路由 http/https
|
|
57
|
+
let req = null;
|
|
58
|
+
if (type === "https")
|
|
59
|
+
req = https.request(options, resFunc);
|
|
60
|
+
else if (type === "http")
|
|
61
|
+
req = http.request(options, resFunc);
|
|
62
|
+
//请求超时
|
|
63
|
+
if (hasTimeLimit) {
|
|
64
|
+
req.setTimeout(timeLimit, () => {
|
|
65
|
+
UtilLogger_1.SLogger.warn(funcName + " 发送请求超时: " + timeLimit + " ms");
|
|
66
|
+
req.destroy();
|
|
43
67
|
});
|
|
44
68
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
throw err;
|
|
48
|
-
UtilLogger_1.SLogger.warn(err);
|
|
69
|
+
req.on('error', (e) => {
|
|
70
|
+
UtilLogger_1.SLogger.warn(funcName + " 发送请求错误:" + e);
|
|
49
71
|
resolve(null);
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
//路由 http/https
|
|
54
|
-
let req = null;
|
|
55
|
-
if (type === "https")
|
|
56
|
-
req = https.request(options, resFunc);
|
|
57
|
-
else if (type === "http")
|
|
58
|
-
req = http.request(options, resFunc);
|
|
59
|
-
//请求超时
|
|
60
|
-
if (hasTimeLimit) {
|
|
61
|
-
req.setTimeout(timeLimit, () => {
|
|
62
|
-
UtilLogger_1.SLogger.warn(funcName + " 发送请求超时: " + timeLimit + " ms");
|
|
63
|
-
req.destroy();
|
|
64
72
|
});
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
UtilLogger_1.SLogger.warn(funcName + " 发送请求错误:" + e);
|
|
68
|
-
resolve(null);
|
|
73
|
+
req.write(jsonStr);
|
|
74
|
+
req.end();
|
|
69
75
|
});
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
exports.shttpPost = shttpPost;
|
|
76
|
+
}
|
|
77
|
+
/**发送一个POST请求并接受数据
|
|
78
|
+
* Object ()
|
|
79
|
+
* @async
|
|
80
|
+
* @param {JObject} json - 数据对象
|
|
81
|
+
* @param {Object} options - 参数对象
|
|
82
|
+
* @param {number} [timeLimit] - 超时时间/秒 最小为10秒
|
|
83
|
+
* @returns {Promise<Object|null>} 结果 null 为未能成功接收
|
|
84
|
+
*/
|
|
85
|
+
function shttpsPost(json, options, timeLimit = -1) {
|
|
86
|
+
return sPost("https", json, options, timeLimit);
|
|
87
|
+
}
|
|
88
|
+
UtilCom.shttpsPost = shttpsPost;
|
|
89
|
+
/**发送一个POST请求并接受数据
|
|
90
|
+
* Object ()
|
|
91
|
+
* @async
|
|
92
|
+
* @param {JObject} json - 数据对象
|
|
93
|
+
* @param {Object} options - 参数对象
|
|
94
|
+
* @param {number} [timeLimit] - 超时时间/秒 最小为10秒
|
|
95
|
+
* @returns {Promise<Object|null>} 结果 null 为未能成功接收
|
|
96
|
+
*/
|
|
97
|
+
function shttpPost(json, options, timeLimit = -1) {
|
|
98
|
+
return sPost("http", json, options, timeLimit);
|
|
99
|
+
}
|
|
100
|
+
UtilCom.shttpPost = shttpPost;
|
|
101
|
+
})(UtilCom = exports.UtilCom || (exports.UtilCom = {}));
|
package/dist/UtilFileTools.d.ts
CHANGED
|
@@ -1,79 +1,82 @@
|
|
|
1
1
|
import { JObject, JToken } from "./UtilInterfaces";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
2
|
+
/**文件工具 */
|
|
3
|
+
export declare namespace UtilFT {
|
|
4
|
+
/**验证路径 文件或文件夹 是否存在 异步
|
|
5
|
+
* @async
|
|
6
|
+
* @param {string} filePath - 待验证的路径
|
|
7
|
+
* @returns {Promise<boolean>} - 是否存在
|
|
8
|
+
*/
|
|
9
|
+
function pathExists(filePath: string): Promise<boolean>;
|
|
10
|
+
/**验证路径 文件或文件夹 是否存在 同步
|
|
11
|
+
* @param {string} filePath - 待验证的路径
|
|
12
|
+
* @returns {boolean} - 是否存在
|
|
13
|
+
*/
|
|
14
|
+
function pathExistsSync(filePath: string): boolean;
|
|
15
|
+
/**路径不存在时创建路径 以path.sep结尾时创建文件夹 异步
|
|
16
|
+
* @async
|
|
17
|
+
* @param {string} filePath - 待创建的路径
|
|
18
|
+
* @param {boolean} isDir - 强制创建一个文件夹
|
|
19
|
+
* @returns {Promise<boolean>} - 是否成功创建
|
|
20
|
+
*/
|
|
21
|
+
function createPath(filePath: string, isDir?: boolean): Promise<boolean>;
|
|
22
|
+
/**路径不存在时创建路径 以path.sep结尾时创建文件夹 同步
|
|
23
|
+
* @param {string} filePath - 待创建的路径
|
|
24
|
+
* @param {boolean} isDir - 强制验证一个文件夹
|
|
25
|
+
* @returns {boolean} - 是否成功创建
|
|
26
|
+
*/
|
|
27
|
+
function createPathSync(filePath: string, isDir?: boolean): boolean;
|
|
28
|
+
/**确保路径存在 不存在时创建 异步
|
|
29
|
+
* @async
|
|
30
|
+
* @param {string} filePath - 待验证的路径
|
|
31
|
+
* @param {boolean} isDir - 强制验证一个文件夹
|
|
32
|
+
* @returns {Promise<boolean>} - 是否成功执行 创建或已存在
|
|
33
|
+
*/
|
|
34
|
+
function ensurePathExists(filePath: string, isDir?: boolean): Promise<boolean>;
|
|
35
|
+
/**确保路径存在 不存在时创建 同步
|
|
36
|
+
* @param {string} filePath - 待验证的路径
|
|
37
|
+
* @returns {boolean} - 是否成功执行 创建或已存在
|
|
38
|
+
*/
|
|
39
|
+
function ensurePathExistsSync(filePath: string, isDir?: boolean): boolean;
|
|
40
|
+
/**加载json文件 同步
|
|
41
|
+
* Object (string)
|
|
42
|
+
* @param {string} filePath - 文件路径
|
|
43
|
+
* @returns {JObject} - 加载完成的对象或空{}
|
|
44
|
+
*/
|
|
45
|
+
function loadJSONFileSync(filePath: string): JObject;
|
|
46
|
+
/**加载json文件 同步
|
|
47
|
+
* Object (string)
|
|
48
|
+
* @param {string} filePath - 文件路径
|
|
49
|
+
* @param {T} def - 默认值
|
|
50
|
+
* @returns {T} - 加载完成的对象或默认值
|
|
51
|
+
*/
|
|
52
|
+
function loadJSONFileSync<T extends JToken>(filePath: string, def: T): T;
|
|
53
|
+
/**加载json文件 异步
|
|
54
|
+
* Object (string)
|
|
55
|
+
* @async
|
|
56
|
+
* @param {string} filePath - 文件路径
|
|
57
|
+
* @returns {Promise<JObject>} - 加载完成的对象或空{}
|
|
58
|
+
*/
|
|
59
|
+
function loadJSONFile(filePath: string): Promise<JObject>;
|
|
60
|
+
/**加载json文件 异步
|
|
61
|
+
* Object (string)
|
|
62
|
+
* @async
|
|
63
|
+
* @param {string} filePath - 文件路径
|
|
64
|
+
* @param {T} T - 默认值
|
|
65
|
+
* @returns {Promise<T>} - 加载完成的对象或默认值
|
|
66
|
+
*/
|
|
67
|
+
function loadJSONFile<T extends JToken>(filePath: string, def: T): Promise<T>;
|
|
68
|
+
/**写入JSON文件
|
|
69
|
+
* void (string,Object)
|
|
70
|
+
* @async
|
|
71
|
+
* @param {string} filePath - 文件路径
|
|
72
|
+
* @param {JToken} token - 所要写入的JToken
|
|
73
|
+
* @returns {Promise<void>}
|
|
74
|
+
*/
|
|
75
|
+
function writeJSONFile(filePath: string, token: JToken): Promise<void>;
|
|
76
|
+
/**搜索路径符合正则表达式的文件
|
|
77
|
+
* @param folder - 文件夹路径
|
|
78
|
+
* @param traitRegex - 正则表达式
|
|
79
|
+
* @returns {Record<string, string>} 文件名与路径的映射
|
|
80
|
+
*/
|
|
81
|
+
function fileSearch(folder: string, traitRegex: string): Record<string, string>;
|
|
82
|
+
}
|
package/dist/UtilFileTools.js
CHANGED
|
@@ -1,188 +1,192 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.UtilFT = void 0;
|
|
4
4
|
const fs = require("fs");
|
|
5
5
|
const path = require("path");
|
|
6
6
|
const UtilInterfaces_1 = require("./UtilInterfaces");
|
|
7
7
|
const UtilLogger_1 = require("./UtilLogger");
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
* @param {string} filePath - 待验证的路径
|
|
26
|
-
* @returns {boolean} - 是否存在
|
|
27
|
-
*/
|
|
28
|
-
function pathExistsSync(filePath) {
|
|
29
|
-
try {
|
|
30
|
-
fs.accessSync(filePath);
|
|
31
|
-
return true;
|
|
32
|
-
}
|
|
33
|
-
catch (e) {
|
|
34
|
-
return false;
|
|
8
|
+
/**文件工具 */
|
|
9
|
+
var UtilFT;
|
|
10
|
+
(function (UtilFT) {
|
|
11
|
+
/**验证路径 文件或文件夹 是否存在 异步
|
|
12
|
+
* @async
|
|
13
|
+
* @param {string} filePath - 待验证的路径
|
|
14
|
+
* @returns {Promise<boolean>} - 是否存在
|
|
15
|
+
*/
|
|
16
|
+
async function pathExists(filePath) {
|
|
17
|
+
try {
|
|
18
|
+
const stats = await fs.promises.stat(filePath);
|
|
19
|
+
await fs.promises.access(filePath);
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
35
25
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
async function createPath(filePath, isDir) {
|
|
45
|
-
if (isDir == true)
|
|
46
|
-
filePath = path.join(filePath, path.sep);
|
|
47
|
-
try {
|
|
48
|
-
if (filePath.endsWith(path.sep)) {
|
|
49
|
-
await fs.promises.mkdir(filePath, { recursive: true });
|
|
26
|
+
UtilFT.pathExists = pathExists;
|
|
27
|
+
/**验证路径 文件或文件夹 是否存在 同步
|
|
28
|
+
* @param {string} filePath - 待验证的路径
|
|
29
|
+
* @returns {boolean} - 是否存在
|
|
30
|
+
*/
|
|
31
|
+
function pathExistsSync(filePath) {
|
|
32
|
+
try {
|
|
33
|
+
fs.accessSync(filePath);
|
|
50
34
|
return true;
|
|
51
35
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
36
|
+
catch (e) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
55
39
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
40
|
+
UtilFT.pathExistsSync = pathExistsSync;
|
|
41
|
+
/**路径不存在时创建路径 以path.sep结尾时创建文件夹 异步
|
|
42
|
+
* @async
|
|
43
|
+
* @param {string} filePath - 待创建的路径
|
|
44
|
+
* @param {boolean} isDir - 强制创建一个文件夹
|
|
45
|
+
* @returns {Promise<boolean>} - 是否成功创建
|
|
46
|
+
*/
|
|
47
|
+
async function createPath(filePath, isDir) {
|
|
48
|
+
if (isDir == true)
|
|
49
|
+
filePath = path.join(filePath, path.sep);
|
|
50
|
+
try {
|
|
51
|
+
if (filePath.endsWith(path.sep)) {
|
|
52
|
+
await fs.promises.mkdir(filePath, { recursive: true });
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
|
|
56
|
+
await fs.promises.open(filePath, 'w');
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
UtilLogger_1.SLogger.error("createPath 错误", e);
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
59
63
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
64
|
+
UtilFT.createPath = createPath;
|
|
65
|
+
/**路径不存在时创建路径 以path.sep结尾时创建文件夹 同步
|
|
66
|
+
* @param {string} filePath - 待创建的路径
|
|
67
|
+
* @param {boolean} isDir - 强制验证一个文件夹
|
|
68
|
+
* @returns {boolean} - 是否成功创建
|
|
69
|
+
*/
|
|
70
|
+
function createPathSync(filePath, isDir) {
|
|
71
|
+
if (isDir == true)
|
|
72
|
+
filePath = path.join(filePath, path.sep);
|
|
73
|
+
try {
|
|
74
|
+
if (filePath.endsWith(path.sep)) {
|
|
75
|
+
fs.mkdirSync(filePath, { recursive: true });
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
79
|
+
fs.openSync(filePath, 'w');
|
|
73
80
|
return true;
|
|
74
81
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
82
|
+
catch (e) {
|
|
83
|
+
UtilLogger_1.SLogger.error("createPathSync 错误", e);
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
78
86
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
87
|
+
UtilFT.createPathSync = createPathSync;
|
|
88
|
+
/**确保路径存在 不存在时创建 异步
|
|
89
|
+
* @async
|
|
90
|
+
* @param {string} filePath - 待验证的路径
|
|
91
|
+
* @param {boolean} isDir - 强制验证一个文件夹
|
|
92
|
+
* @returns {Promise<boolean>} - 是否成功执行 创建或已存在
|
|
93
|
+
*/
|
|
94
|
+
async function ensurePathExists(filePath, isDir) {
|
|
95
|
+
if (await pathExists(filePath))
|
|
96
|
+
return true;
|
|
97
|
+
return await createPath(filePath, isDir);
|
|
82
98
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (await pathExists(filePath))
|
|
93
|
-
return true;
|
|
94
|
-
return await createPath(filePath, isDir);
|
|
95
|
-
}
|
|
96
|
-
exports.ensurePathExists = ensurePathExists;
|
|
97
|
-
/**确保路径存在 不存在时创建 同步
|
|
98
|
-
* @param {string} filePath - 待验证的路径
|
|
99
|
-
* @returns {boolean} - 是否成功执行 创建或已存在
|
|
100
|
-
*/
|
|
101
|
-
function ensurePathExistsSync(filePath, isDir) {
|
|
102
|
-
if (pathExistsSync(filePath))
|
|
103
|
-
return true;
|
|
104
|
-
return createPathSync(filePath, isDir);
|
|
105
|
-
}
|
|
106
|
-
exports.ensurePathExistsSync = ensurePathExistsSync;
|
|
107
|
-
function loadJSONFileSync(filePath, def) {
|
|
108
|
-
if (path.extname(filePath) !== '.json')
|
|
109
|
-
filePath += '.json';
|
|
110
|
-
let str = "";
|
|
111
|
-
// 判断文件路径是否存在
|
|
112
|
-
if (pathExistsSync(filePath))
|
|
113
|
-
str = fs.readFileSync(filePath, "utf-8");
|
|
114
|
-
// 如果不存在则返回默认值
|
|
115
|
-
if (str == "" || str == null) {
|
|
116
|
-
if (def !== undefined)
|
|
117
|
-
return def;
|
|
118
|
-
return {};
|
|
99
|
+
UtilFT.ensurePathExists = ensurePathExists;
|
|
100
|
+
/**确保路径存在 不存在时创建 同步
|
|
101
|
+
* @param {string} filePath - 待验证的路径
|
|
102
|
+
* @returns {boolean} - 是否成功执行 创建或已存在
|
|
103
|
+
*/
|
|
104
|
+
function ensurePathExistsSync(filePath, isDir) {
|
|
105
|
+
if (pathExistsSync(filePath))
|
|
106
|
+
return true;
|
|
107
|
+
return createPathSync(filePath, isDir);
|
|
119
108
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
str
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
return
|
|
109
|
+
UtilFT.ensurePathExistsSync = ensurePathExistsSync;
|
|
110
|
+
function loadJSONFileSync(filePath, def) {
|
|
111
|
+
if (path.extname(filePath) !== '.json')
|
|
112
|
+
filePath += '.json';
|
|
113
|
+
let str = "";
|
|
114
|
+
// 判断文件路径是否存在
|
|
115
|
+
if (pathExistsSync(filePath))
|
|
116
|
+
str = fs.readFileSync(filePath, "utf-8");
|
|
117
|
+
// 如果不存在则返回默认值
|
|
118
|
+
if (str == "" || str == null) {
|
|
119
|
+
if (def !== undefined)
|
|
120
|
+
return def;
|
|
121
|
+
return {};
|
|
122
|
+
}
|
|
123
|
+
return JSON.parse(str);
|
|
135
124
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
if (!(await pathExists(filePath)))
|
|
152
|
-
await createPath(filePath);
|
|
153
|
-
// 写入文件
|
|
154
|
-
try {
|
|
155
|
-
await fs.promises.writeFile(filePath, str);
|
|
156
|
-
UtilLogger_1.SLogger.verbose(`${filePath} writeJSONFile 成功`);
|
|
125
|
+
UtilFT.loadJSONFileSync = loadJSONFileSync;
|
|
126
|
+
async function loadJSONFile(filePath, def) {
|
|
127
|
+
if (path.extname(filePath) !== '.json')
|
|
128
|
+
filePath += '.json';
|
|
129
|
+
let str = "";
|
|
130
|
+
// 判断文件路径是否存在
|
|
131
|
+
if (await pathExists(filePath))
|
|
132
|
+
str = await fs.promises.readFile(filePath, "utf-8");
|
|
133
|
+
// 如果不存在则返回默认值
|
|
134
|
+
if (str == "" || str == null) {
|
|
135
|
+
if (def !== undefined)
|
|
136
|
+
return def;
|
|
137
|
+
return {};
|
|
138
|
+
}
|
|
139
|
+
return JSON.parse(str);
|
|
157
140
|
}
|
|
158
|
-
|
|
159
|
-
|
|
141
|
+
UtilFT.loadJSONFile = loadJSONFile;
|
|
142
|
+
/**写入JSON文件
|
|
143
|
+
* void (string,Object)
|
|
144
|
+
* @async
|
|
145
|
+
* @param {string} filePath - 文件路径
|
|
146
|
+
* @param {JToken} token - 所要写入的JToken
|
|
147
|
+
* @returns {Promise<void>}
|
|
148
|
+
*/
|
|
149
|
+
async function writeJSONFile(filePath, token) {
|
|
150
|
+
let str = (0, UtilInterfaces_1.stringifyJToken)(token);
|
|
151
|
+
if (path.extname(filePath) !== '.json')
|
|
152
|
+
filePath += '.json';
|
|
153
|
+
// 判断文件路径是否存在 不存在则创建
|
|
154
|
+
if (!(await pathExists(filePath)))
|
|
155
|
+
await createPath(filePath);
|
|
156
|
+
// 写入文件
|
|
157
|
+
try {
|
|
158
|
+
await fs.promises.writeFile(filePath, str);
|
|
159
|
+
UtilLogger_1.SLogger.verbose(`${filePath} writeJSONFile 成功`);
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
UtilLogger_1.SLogger.error(`${filePath} writeJSONFile 错误`, err);
|
|
163
|
+
}
|
|
160
164
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
165
|
+
UtilFT.writeJSONFile = writeJSONFile;
|
|
166
|
+
/**搜索路径符合正则表达式的文件
|
|
167
|
+
* @param folder - 文件夹路径
|
|
168
|
+
* @param traitRegex - 正则表达式
|
|
169
|
+
* @returns {Record<string, string>} 文件名与路径的映射
|
|
170
|
+
*/
|
|
171
|
+
function fileSearch(folder, traitRegex) {
|
|
172
|
+
let outMap = {};
|
|
173
|
+
let subFiles = fs.readdirSync(folder);
|
|
174
|
+
let regex = new RegExp(traitRegex);
|
|
175
|
+
for (let subFile of subFiles) {
|
|
176
|
+
let subFilePath = path.join(folder, subFile);
|
|
177
|
+
subFilePath = subFilePath.replace(/\\/g, "/");
|
|
178
|
+
let stat = fs.lstatSync(subFilePath);
|
|
179
|
+
//判断是否是文件夹,递归调用
|
|
180
|
+
if (stat.isDirectory()) {
|
|
181
|
+
let subMap = fileSearch(path.join(subFilePath, path.sep), traitRegex);
|
|
182
|
+
for (let key in subMap)
|
|
183
|
+
outMap[key] = subMap[key];
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (regex.test(subFilePath))
|
|
187
|
+
outMap[subFile] = subFilePath;
|
|
182
188
|
}
|
|
183
|
-
|
|
184
|
-
outMap[subFile] = subFilePath;
|
|
189
|
+
return outMap;
|
|
185
190
|
}
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
exports.fileSearch = fileSearch;
|
|
191
|
+
UtilFT.fileSearch = fileSearch;
|
|
192
|
+
})(UtilFT = exports.UtilFT || (exports.UtilFT = {}));
|
package/dist/UtilInterfaces.d.ts
CHANGED
|
@@ -22,3 +22,9 @@ export declare function stringifyJToken(token: JToken, space?: string | number |
|
|
|
22
22
|
export type Writeable<T> = {
|
|
23
23
|
-readonly [P in keyof T]: T[P];
|
|
24
24
|
};
|
|
25
|
+
/**翻转K和V */
|
|
26
|
+
export type Inverted<T extends Record<keyof T, string | number | symbol>> = {
|
|
27
|
+
[K in keyof T as T[K]]: K;
|
|
28
|
+
};
|
|
29
|
+
/**N长度 T类型的元组 */
|
|
30
|
+
export type FixedLengthTuple<T, N extends number, R extends unknown[] = []> = R['length'] extends N ? R : FixedLengthTuple<T, N, [T, ...R]>;
|
package/package.json
CHANGED
package/src/UtilCodecs.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import * as he from 'html-entities';
|
|
2
2
|
import {get_encoding,Tiktoken} from 'tiktoken';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/**编码/解码器 */
|
|
7
|
+
export namespace UtilCodec{
|
|
8
|
+
|
|
9
|
+
|
|
3
10
|
let encoderTurbo:Tiktoken|null = null;
|
|
4
11
|
let encoderDavinci:Tiktoken|null = null;
|
|
5
12
|
const textDecoder = new TextDecoder();
|
|
6
13
|
|
|
7
|
-
|
|
8
14
|
// 定义一个对象,存储常见的HTML实体和对应的字符
|
|
9
15
|
let htmlEntities:Record<string,string> = {
|
|
10
16
|
"<": "<",
|
|
@@ -107,4 +113,6 @@ export function decodeTokenTurbo(arr:Uint32Array):string{
|
|
|
107
113
|
export function decodeTokenDavinci(arr:Uint32Array):string{
|
|
108
114
|
initTikTokenEncoder();
|
|
109
115
|
return textDecoder.decode(encoderDavinci?.decode(arr));
|
|
116
|
+
}
|
|
117
|
+
|
|
110
118
|
}
|
package/src/UtilCom.ts
CHANGED
|
@@ -4,6 +4,8 @@ import * as http from 'http';
|
|
|
4
4
|
import { SLogger } from "./UtilLogger";
|
|
5
5
|
|
|
6
6
|
|
|
7
|
+
/**网络工具 */
|
|
8
|
+
export namespace UtilCom{
|
|
7
9
|
|
|
8
10
|
function sPost(type:"http"|"https",json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
|
|
9
11
|
//转换为毫秒
|
|
@@ -102,4 +104,5 @@ export function shttpsPost(json:JObject,options:Object,timeLimit:number=-1):Prom
|
|
|
102
104
|
*/
|
|
103
105
|
export function shttpPost(json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
|
|
104
106
|
return sPost("http",json,options,timeLimit);
|
|
107
|
+
}
|
|
105
108
|
}
|
package/src/UtilDecorators.ts
CHANGED
package/src/UtilFileTools.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import * as fs from "fs";
|
|
2
2
|
import * as path from "path";
|
|
3
|
-
import {
|
|
3
|
+
import { JObject, JToken, stringifyJToken } from "./UtilInterfaces";
|
|
4
4
|
import { SLogger } from "./UtilLogger";
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
/**文件工具 */
|
|
7
|
+
export namespace UtilFT{
|
|
7
8
|
|
|
8
9
|
/**验证路径 文件或文件夹 是否存在 异步
|
|
9
10
|
* @async
|
|
@@ -218,4 +219,5 @@ export function fileSearch(folder: string, traitRegex: string) {
|
|
|
218
219
|
if (regex.test(subFilePath)) outMap[subFile] = subFilePath;
|
|
219
220
|
}
|
|
220
221
|
return outMap;
|
|
222
|
+
}
|
|
221
223
|
}
|
package/src/UtilInterfaces.ts
CHANGED
|
@@ -25,5 +25,14 @@ export function stringifyJToken(token:JToken,space:string|number|null|undefined=
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
/**转为可写的 */
|
|
28
|
-
export type Writeable<T> = {
|
|
28
|
+
export type Writeable<T> = {
|
|
29
|
+
-readonly [P in keyof T]: T[P]
|
|
30
|
+
};
|
|
31
|
+
/**翻转K和V */
|
|
32
|
+
export type Inverted<T extends Record<keyof T, string | number | symbol>> = {
|
|
33
|
+
[K in keyof T as T[K]]: K;
|
|
34
|
+
};
|
|
35
|
+
/**N长度 T类型的元组 */
|
|
36
|
+
export type FixedLengthTuple<T, N extends number, R extends unknown[] = []> =
|
|
37
|
+
R['length'] extends N ? R : FixedLengthTuple<T, N, [T, ...R]>;
|
|
29
38
|
|