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