@zwa73/utils 1.0.33 → 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.
@@ -67,7 +67,12 @@ interface SortCallback<T> {
67
67
  }
68
68
  export declare class SList<T> {
69
69
  private _arr;
70
- constructor(obj?: Array<T> | number);
70
+ /**建立0长度的SList */
71
+ constructor();
72
+ /**建立一个长度为 obj 的SList */
73
+ constructor(obj: number);
74
+ /**根据obj建立SList */
75
+ constructor(obj: Array<T>);
71
76
  /**返回数组长度
72
77
  * @returns {number} - 长度
73
78
  */
@@ -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 + " 接受信息:", (0, UtilInterfaces_1.stringifyJToken)(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 = {}));