@zwa73/utils 1.0.0

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.
@@ -0,0 +1,40 @@
1
+ /**HTML实体解码 将一个字符串中的HTML实体转换为对应的字符
2
+ * @param {string} str - 要转换的字符串
3
+ * @returns {string} 转换后的字符串
4
+ */
5
+ export declare function decodeHtmlEntities(str: string): any;
6
+ /**HTML实体编码 将一个字符串中的 需编码字符转换为 HTML实体
7
+ * @param {string} str - 要转换的字符串
8
+ * @returns {string} 转换后的字符串
9
+ */
10
+ export declare function encodeHtmlEntities(str: string): any;
11
+ /**token长度计算器 Turbo模型
12
+ * @param {string} str = 所要计算的消息
13
+ * @returns {number} 整数长度结果
14
+ */
15
+ export declare function tokenNumTurbo(str: string): any;
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;
@@ -0,0 +1,99 @@
1
+ "use strict";
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;
4
+ const he = require("html-entities");
5
+ //const encoder = require('gpt-3-encoder');
6
+ //const tiktoken = require('@dqbd/tiktoken');
7
+ const tiktoken = require("@dqbd/tiktoken");
8
+ const encoderTurbo = tiktoken.get_encoding("cl100k_base");
9
+ const encoderDavinci = tiktoken.get_encoding("p50k_base");
10
+ const textDecoder = new TextDecoder();
11
+ // 定义一个对象,存储常见的HTML实体和对应的字符
12
+ let htmlEntities = {
13
+ "&lt;": "<",
14
+ "&gt;": ">",
15
+ "&amp;": "&",
16
+ "&quot;": "\"",
17
+ "&#39;": "'",
18
+ "&#91;": "[",
19
+ "&#93;": "]"
20
+ };
21
+ /**HTML实体解码 将一个字符串中的HTML实体转换为对应的字符
22
+ * @param {string} str - 要转换的字符串
23
+ * @returns {string} 转换后的字符串
24
+ */
25
+ function decodeHtmlEntities(str) {
26
+ //for(let code in htmlEntities){
27
+ // let cha = htmlEntities[code]
28
+ // str = str.replaceAll(code, cha);
29
+ //}
30
+ //return str
31
+ return he.decode(str);
32
+ }
33
+ exports.decodeHtmlEntities = decodeHtmlEntities;
34
+ /**HTML实体编码 将一个字符串中的 需编码字符转换为 HTML实体
35
+ * @param {string} str - 要转换的字符串
36
+ * @returns {string} 转换后的字符串
37
+ */
38
+ function encodeHtmlEntities(str) {
39
+ //for(let code in htmlEntities){
40
+ // let cha = htmlEntities[code]
41
+ // str = str.replaceAll(cha, code);
42
+ //}
43
+ //return str
44
+ return he.encode(str);
45
+ }
46
+ exports.encodeHtmlEntities = encodeHtmlEntities;
47
+ //token长度计算器
48
+ //cl100k_base ChatGPT models, text-embedding-ada-002
49
+ //p50k_base Code models, text-davinci-002, text-davinci-003
50
+ //r50k_base (or gpt2) GPT-3 models like davinci
51
+ /**token长度计算器 Turbo模型
52
+ * @param {string} str = 所要计算的消息
53
+ * @returns {number} 整数长度结果
54
+ */
55
+ function tokenNumTurbo(str) {
56
+ //return encoder.encode(str).length
57
+ return encoderTurbo.encode(str).length;
58
+ }
59
+ exports.tokenNumTurbo = tokenNumTurbo;
60
+ /**token长度计算器 Davinci模型
61
+ * @param {string} str = 所要计算的消息
62
+ * @returns {number} 整数长度结果
63
+ */
64
+ function tokenNumDavinci(str) {
65
+ return encoderDavinci.encode(str).length;
66
+ }
67
+ exports.tokenNumDavinci = tokenNumDavinci;
68
+ /**token编码 Turbo模型
69
+ * @param {string} str = 所要计算的消息
70
+ * @returns {Array<number>} Token数组
71
+ */
72
+ function encodeTokenTurbo(str) {
73
+ return encoderTurbo.encode(str);
74
+ }
75
+ exports.encodeTokenTurbo = encodeTokenTurbo;
76
+ /**token编码 Davinci模型
77
+ * @param {string} str = 所要计算的消息
78
+ * @returns {Array<number>} Token数组
79
+ */
80
+ function encodeTokenDavinci(str) {
81
+ return encoderDavinci.encode(str);
82
+ }
83
+ exports.encodeTokenDavinci = encodeTokenDavinci;
84
+ /**token解码 Turbo模型
85
+ * @param {Array<number>} arr = Token数组
86
+ * @returns {string} 消息字符串
87
+ */
88
+ function decodeTokenTurbo(arr) {
89
+ return textDecoder.decode(encoderTurbo.decode(arr));
90
+ }
91
+ exports.decodeTokenTurbo = decodeTokenTurbo;
92
+ /**token解码 Davinci模型
93
+ * @param {Array<number>} arr = Token数组
94
+ * @returns {string} 消息字符串
95
+ */
96
+ function decodeTokenDavinci(arr) {
97
+ return textDecoder.decode(encoderDavinci.decode(arr));
98
+ }
99
+ exports.decodeTokenDavinci = decodeTokenDavinci;
@@ -0,0 +1,45 @@
1
+ import { JObject } from './UtilInterfaces';
2
+ export declare function getTime(): number;
3
+ export declare function initField(obj: Record<string, any>, field: string, defaultVal: any): void;
4
+ /**加载json文件
5
+ * Object (string)
6
+ * @param {string} filePath - 文件路径
7
+ * @returns {JObject}
8
+ */
9
+ export declare function loadJSONFile(filePath: string): JObject;
10
+ /**写入JSON文件
11
+ * void (string,Object)
12
+ * @param {string} filePath - 文件路径
13
+ * @param {JObject} obj - 所要写入的JObject
14
+ * @returns {void}
15
+ */
16
+ export declare function writeJSONFile_o(filePath: string, obj: JObject): void;
17
+ /**写入JSON文件
18
+ * void (string,Object)
19
+ * @async
20
+ * @param {string} filePath - 文件路径
21
+ * @param {JObject} obj - 所要写入的JObject
22
+ * @returns {Promise<void>}
23
+ */
24
+ export declare function writeJSONFile(filePath: string, obj: JObject): Promise<void>;
25
+ /**生成一串uuid
26
+ * string ()
27
+ * @returns {string} uuid
28
+ */
29
+ export declare function genUUID(): any;
30
+ /**深克隆 序列化并反序列化
31
+ * @template {T}
32
+ * @param {T} obj - 克隆目标
33
+ * @returns {T} 克隆结果
34
+ */
35
+ export declare function deepClone<T>(obj: T): T;
36
+ /**是否为安全的数字
37
+ * @param {number} num - 所要检测的数字
38
+ * @returns {boolean} 是否安全
39
+ */
40
+ export declare function isSafeNumber(num: number): boolean;
41
+ /**等待 timeMs 毫秒
42
+ * @param {number} timeMs - 等待的毫秒数
43
+ * @returns {Promise<boolean>}
44
+ */
45
+ export declare function sleep(timeMs: number): Promise<boolean>;
@@ -0,0 +1,179 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sleep = exports.isSafeNumber = exports.deepClone = exports.genUUID = exports.writeJSONFile = exports.writeJSONFile_o = exports.loadJSONFile = exports.initField = exports.getTime = void 0;
4
+ const fs = require("fs");
5
+ const crypto = require("crypto");
6
+ const path = require("path");
7
+ function getTime() {
8
+ return new Date().getTime();
9
+ }
10
+ exports.getTime = getTime;
11
+ function initField(obj, field, defaultVal) {
12
+ if (!(field in obj))
13
+ obj[field] = defaultVal;
14
+ }
15
+ exports.initField = initField;
16
+ /**加载json文件
17
+ * Object (string)
18
+ * @param {string} filePath - 文件路径
19
+ * @returns {JObject}
20
+ */
21
+ function loadJSONFile(filePath) {
22
+ if (filePath.indexOf(".json") == -1)
23
+ filePath += ".json";
24
+ // 判断文件路径是否存在
25
+ if (!fs.existsSync(filePath)) {
26
+ // 如果路径不存在,创建文件夹
27
+ try {
28
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
29
+ }
30
+ catch (e) { }
31
+ // 创建文件
32
+ fs.writeFileSync(filePath, '');
33
+ }
34
+ let str = fs.readFileSync(filePath);
35
+ if (str == "" || str == null)
36
+ str = "{}";
37
+ return JSON.parse(str);
38
+ }
39
+ exports.loadJSONFile = loadJSONFile;
40
+ /**写入JSON文件
41
+ * void (string,Object)
42
+ * @param {string} filePath - 文件路径
43
+ * @param {JObject} obj - 所要写入的JObject
44
+ * @returns {void}
45
+ */
46
+ function writeJSONFile_o(filePath, obj) {
47
+ let str = JSON.stringify(obj, null, "\t");
48
+ if (filePath.indexOf(".json") == -1)
49
+ filePath += ".json";
50
+ // 判断文件路径是否存在
51
+ if (!fs.existsSync(filePath)) {
52
+ // 如果路径不存在,创建文件夹
53
+ try {
54
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
55
+ }
56
+ catch (e) { }
57
+ }
58
+ fs.writeFile(filePath, str, function (err) {
59
+ if (err == null)
60
+ console.log(filePath + "writeJSONFile 成功");
61
+ else {
62
+ console.log(filePath + "writeJSONFile 错误");
63
+ console.log(err);
64
+ }
65
+ });
66
+ }
67
+ exports.writeJSONFile_o = writeJSONFile_o;
68
+ /**写入JSON文件
69
+ * void (string,Object)
70
+ * @async
71
+ * @param {string} filePath - 文件路径
72
+ * @param {JObject} obj - 所要写入的JObject
73
+ * @returns {Promise<void>}
74
+ */
75
+ async function writeJSONFile(filePath, obj) {
76
+ let str = JSON.stringify(obj, null, "\t");
77
+ if (filePath.indexOf(".json") === -1)
78
+ filePath += ".json";
79
+ // 判断文件路径是否存在
80
+ await new Promise((resolve, reject) => {
81
+ fs.exists(filePath, (ex) => {
82
+ if (!ex) {
83
+ try {
84
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
85
+ }
86
+ catch (e) {
87
+ console.log("创建文件错误:" + e);
88
+ }
89
+ }
90
+ resolve(true);
91
+ });
92
+ });
93
+ return new Promise((resolve, reject) => {
94
+ fs.writeFile(filePath, str, function (err) {
95
+ if (err === null) {
96
+ console.log(`${filePath} writeJSONFile 成功`);
97
+ resolve();
98
+ }
99
+ else {
100
+ console.log(`${filePath} writeJSONFile 错误`);
101
+ console.log(err);
102
+ reject(err);
103
+ }
104
+ });
105
+ });
106
+ }
107
+ exports.writeJSONFile = writeJSONFile;
108
+ /**生成一串uuid
109
+ * string ()
110
+ * @returns {string} uuid
111
+ */
112
+ function genUUID() {
113
+ return crypto.randomBytes(16).toString('hex');
114
+ }
115
+ exports.genUUID = genUUID;
116
+ /**深克隆 序列化并反序列化
117
+ * @template {T}
118
+ * @param {T} obj - 克隆目标
119
+ * @returns {T} 克隆结果
120
+ */
121
+ function deepClone(obj) {
122
+ return JSON.parse(JSON.stringify(obj));
123
+ }
124
+ exports.deepClone = deepClone;
125
+ /**是否为安全的数字
126
+ * @param {number} num - 所要检测的数字
127
+ * @returns {boolean} 是否安全
128
+ */
129
+ function isSafeNumber(num) {
130
+ if (num === undefined || num == null || isNaN(num))
131
+ return false;
132
+ return true;
133
+ }
134
+ exports.isSafeNumber = isSafeNumber;
135
+ /**等待 timeMs 毫秒
136
+ * @param {number} timeMs - 等待的毫秒数
137
+ * @returns {Promise<boolean>}
138
+ */
139
+ function sleep(timeMs) {
140
+ return new Promise(function (resolve, rejecte) {
141
+ let timer = setTimeout(function () {
142
+ resolve(true);
143
+ }, timeMs);
144
+ });
145
+ }
146
+ exports.sleep = sleep;
147
+ class SEntry {
148
+ _key;
149
+ _value;
150
+ constructor(key, value) {
151
+ this._key = key;
152
+ this._value = value;
153
+ }
154
+ getKey() {
155
+ return this._key;
156
+ }
157
+ getValue() {
158
+ return this._value;
159
+ }
160
+ }
161
+ class SHashMap {
162
+ obj = {};
163
+ constructor() { }
164
+ get(key) {
165
+ return this.obj[key];
166
+ }
167
+ put(key, value) {
168
+ this.obj[key] = value;
169
+ }
170
+ has(key) {
171
+ return key in this.obj;
172
+ }
173
+ entrys() {
174
+ }
175
+ }
176
+ class SKVC {
177
+ stringMap = new SHashMap();
178
+ constructor() { }
179
+ }
@@ -0,0 +1,19 @@
1
+ /**可以序列化为JSON文件的Object*/
2
+ export type JToken = JObject | JArray | JValue;
3
+ export type JValue = number | string | boolean | null;
4
+ export type JArray = Array<JToken>;
5
+ export type JObject = {
6
+ [key: string]: JToken;
7
+ };
8
+ /**未知格式的Object*/
9
+ export type AnyObject = {
10
+ [key: string]: any;
11
+ };
12
+ /**用于存储消息的Entry
13
+ * 暂时存于Util
14
+ * 未来应转入LaMChar中
15
+ */
16
+ export type MessageEntity = {
17
+ role: string;
18
+ content: string;
19
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ export * from './UtilFunctions';
2
+ export * from './UtilInterfaces';
3
+ export * from './UtilCodecs';
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./UtilFunctions"), exports);
18
+ __exportStar(require("./UtilInterfaces"), exports);
19
+ __exportStar(require("./UtilCodecs"), exports);
package/index.js ADDED
@@ -0,0 +1,16 @@
1
+ const tsconfigPaths = require("tsconfig-paths");
2
+
3
+ const baseUrl = "."; // 项目根目录
4
+ const paths = {
5
+ "@/*" : ["./*"] ,
6
+ "@/src/*" : ["./dist/*"] , // 将 @ 映射到 dist 目录
7
+ };
8
+
9
+
10
+ tsconfigPaths.register({
11
+ baseUrl,
12
+ paths,
13
+ });
14
+
15
+ const dist = require("./dist");
16
+ module.exports = dist;
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@zwa73/utils",
3
+ "version": "1.0.0",
4
+ "description": "my utils",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node test"
8
+ },
9
+ "keywords": [
10
+ "zwa73"
11
+ ],
12
+ "author": "zwa73",
13
+ "license": "ISC",
14
+ "dependencies": {
15
+ "@dqbd/tiktoken": "^1.0.7",
16
+ "@types/node": "^18.16.3",
17
+ "@vitalets/google-translate-api": "^9.1.0",
18
+ "esm-resolve": "^1.0.8",
19
+ "express": "^4.18.2",
20
+ "gpt-3-encoder": "^1.1.4",
21
+ "html-entities": "^2.3.3",
22
+ "http-proxy-agent": "^5.0.0",
23
+ "https-proxy-agent": "^5.0.1",
24
+ "iconv-lite": "^0.6.3",
25
+ "md5": "^2.3.0",
26
+ "querystring": "^0.2.1",
27
+ "tsconfig-paths": "^4.2.0"
28
+ }
29
+ }
@@ -0,0 +1,96 @@
1
+ import * as he from 'html-entities';
2
+ //const encoder = require('gpt-3-encoder');
3
+ //const tiktoken = require('@dqbd/tiktoken');
4
+ import * as tiktoken from '@dqbd/tiktoken';
5
+ const encoderTurbo = tiktoken.get_encoding("cl100k_base");
6
+ const encoderDavinci = tiktoken.get_encoding("p50k_base");
7
+ const textDecoder = new TextDecoder();
8
+
9
+
10
+ // 定义一个对象,存储常见的HTML实体和对应的字符
11
+ let htmlEntities:Record<string,string> = {
12
+ "&lt;": "<",
13
+ "&gt;": ">",
14
+ "&amp;": "&",
15
+ "&quot;": "\"",
16
+ "&#39;": "'",
17
+ "&#91;": "[",
18
+ "&#93;": "]"
19
+ };
20
+
21
+ /**HTML实体解码 将一个字符串中的HTML实体转换为对应的字符
22
+ * @param {string} str - 要转换的字符串
23
+ * @returns {string} 转换后的字符串
24
+ */
25
+ export function decodeHtmlEntities(str:string) {
26
+ //for(let code in htmlEntities){
27
+ // let cha = htmlEntities[code]
28
+ // str = str.replaceAll(code, cha);
29
+ //}
30
+ //return str
31
+ return he.decode(str);
32
+ }
33
+
34
+ /**HTML实体编码 将一个字符串中的 需编码字符转换为 HTML实体
35
+ * @param {string} str - 要转换的字符串
36
+ * @returns {string} 转换后的字符串
37
+ */
38
+ export function encodeHtmlEntities(str:string) {
39
+ //for(let code in htmlEntities){
40
+ // let cha = htmlEntities[code]
41
+ // str = str.replaceAll(cha, code);
42
+ //}
43
+ //return str
44
+ return he.encode(str);
45
+ }
46
+
47
+
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
+
53
+ /**token长度计算器 Turbo模型
54
+ * @param {string} str = 所要计算的消息
55
+ * @returns {number} 整数长度结果
56
+ */
57
+ export function tokenNumTurbo(str:string){
58
+ //return encoder.encode(str).length
59
+ return encoderTurbo.encode(str).length;
60
+ }
61
+ /**token长度计算器 Davinci模型
62
+ * @param {string} str = 所要计算的消息
63
+ * @returns {number} 整数长度结果
64
+ */
65
+ export function tokenNumDavinci(str:string):number{
66
+ return encoderDavinci.encode(str).length;
67
+ }
68
+
69
+ /**token编码 Turbo模型
70
+ * @param {string} str = 所要计算的消息
71
+ * @returns {Array<number>} Token数组
72
+ */
73
+ export function encodeTokenTurbo(str:string):Uint32Array{
74
+ return encoderTurbo.encode(str)
75
+ }
76
+ /**token编码 Davinci模型
77
+ * @param {string} str = 所要计算的消息
78
+ * @returns {Array<number>} Token数组
79
+ */
80
+ export function encodeTokenDavinci(str:string):Uint32Array{
81
+ return encoderDavinci.encode(str)
82
+ }
83
+ /**token解码 Turbo模型
84
+ * @param {Array<number>} arr = Token数组
85
+ * @returns {string} 消息字符串
86
+ */
87
+ export function decodeTokenTurbo(arr:Uint32Array):string{
88
+ return textDecoder.decode(encoderTurbo.decode(arr));
89
+ }
90
+ /**token解码 Davinci模型
91
+ * @param {Array<number>} arr = Token数组
92
+ * @returns {string} 消息字符串
93
+ */
94
+ export function decodeTokenDavinci(arr:Uint32Array):string{
95
+ return textDecoder.decode(encoderDavinci.decode(arr));
96
+ }
@@ -0,0 +1,177 @@
1
+ import * as fs from 'fs';
2
+ import * as crypto from 'crypto';
3
+ import * as path from 'path';
4
+ import { JObject } from './UtilInterfaces';
5
+
6
+ export function getTime():number{
7
+ return new Date().getTime();
8
+ }
9
+
10
+ export function initField(obj:Record<string,any>,field:string,defaultVal:any):void{
11
+ if(!(field in obj))
12
+ obj[field] = defaultVal;
13
+ }
14
+
15
+ /**加载json文件
16
+ * Object (string)
17
+ * @param {string} filePath - 文件路径
18
+ * @returns {JObject}
19
+ */
20
+ export function loadJSONFile(filePath:string):JObject{
21
+ if(filePath.indexOf(".json")==-1)
22
+ filePath += ".json";
23
+ // 判断文件路径是否存在
24
+ if (!fs.existsSync(filePath)) {
25
+ // 如果路径不存在,创建文件夹
26
+ try {
27
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
28
+ }catch(e){}
29
+ // 创建文件
30
+ fs.writeFileSync(filePath, '');
31
+ }
32
+ let str = fs.readFileSync(filePath) as any;
33
+ if(str=="" || str==null)
34
+ str = "{}";
35
+ return JSON.parse(str);
36
+ }
37
+ /**写入JSON文件
38
+ * void (string,Object)
39
+ * @param {string} filePath - 文件路径
40
+ * @param {JObject} obj - 所要写入的JObject
41
+ * @returns {void}
42
+ */
43
+ export function writeJSONFile_o(filePath:string,obj:JObject){
44
+ let str = JSON.stringify(obj,null,"\t");
45
+ if(filePath.indexOf(".json")==-1)
46
+ filePath += ".json";
47
+ // 判断文件路径是否存在
48
+ if (!fs.existsSync(filePath)) {
49
+ // 如果路径不存在,创建文件夹
50
+ try {
51
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
52
+ }catch(e){}
53
+ }
54
+ fs.writeFile(filePath,str,function(err:any){
55
+ if(err==null)
56
+ console.log(filePath+"writeJSONFile 成功");
57
+ else{
58
+ console.log(filePath+"writeJSONFile 错误");
59
+ console.log(err);
60
+ }
61
+ });
62
+ }
63
+ /**写入JSON文件
64
+ * void (string,Object)
65
+ * @async
66
+ * @param {string} filePath - 文件路径
67
+ * @param {JObject} obj - 所要写入的JObject
68
+ * @returns {Promise<void>}
69
+ */
70
+ export async function writeJSONFile(filePath:string, obj:JObject):Promise<void> {
71
+ let str = JSON.stringify(obj, null, "\t");
72
+ if (filePath.indexOf(".json") === -1) filePath += ".json";
73
+
74
+ // 判断文件路径是否存在
75
+ await new Promise((resolve, reject)=>{
76
+ fs.exists(filePath, (ex:boolean) => {
77
+ if(!ex){
78
+ try {
79
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
80
+ } catch (e) {
81
+ console.log("创建文件错误:"+e);
82
+ }
83
+ }
84
+ resolve(true);
85
+ });
86
+ })
87
+
88
+ return new Promise((resolve, reject) => {
89
+ fs.writeFile(filePath, str, function (err) {
90
+ if (err === null) {
91
+ console.log(`${filePath} writeJSONFile 成功`);
92
+ resolve();
93
+ } else {
94
+ console.log(`${filePath} writeJSONFile 错误`);
95
+ console.log(err);
96
+ reject(err);
97
+ }
98
+ });
99
+ });
100
+ }
101
+ /**生成一串uuid
102
+ * string ()
103
+ * @returns {string} uuid
104
+ */
105
+ export function genUUID(){
106
+ return crypto.randomBytes(16).toString('hex');
107
+ }
108
+
109
+ /**深克隆 序列化并反序列化
110
+ * @template {T}
111
+ * @param {T} obj - 克隆目标
112
+ * @returns {T} 克隆结果
113
+ */
114
+ export function deepClone<T>(obj:T):T{
115
+ return JSON.parse(JSON.stringify(obj));
116
+ }
117
+
118
+ /**是否为安全的数字
119
+ * @param {number} num - 所要检测的数字
120
+ * @returns {boolean} 是否安全
121
+ */
122
+ export function isSafeNumber(num:number):boolean{
123
+ if(num === undefined || num==null || isNaN(num))
124
+ return false;
125
+ return true;
126
+ }
127
+
128
+ /**等待 timeMs 毫秒
129
+ * @param {number} timeMs - 等待的毫秒数
130
+ * @returns {Promise<boolean>}
131
+ */
132
+ export function sleep(timeMs:number):Promise<boolean>{
133
+ return new Promise(function(resolve, rejecte){
134
+ let timer = setTimeout(function(){
135
+ resolve(true)
136
+ },timeMs);
137
+ });
138
+ }
139
+
140
+ type struct = number|string;
141
+
142
+ class SEntry<K extends struct,V>{
143
+ _key:K;
144
+ _value:V|null;
145
+ constructor(key:K,value:V|null){
146
+ this._key = key;
147
+ this._value = value;
148
+ }
149
+ getKey(){
150
+ return this._key;
151
+ }
152
+ getValue(){
153
+ return this._value;
154
+ }
155
+ }
156
+ class SHashMap<K extends struct,V>{
157
+ obj:Record<K,V|null> = {} as Record<K,V>;
158
+ constructor(){}
159
+ get(key:K){
160
+ return this.obj[key];
161
+ }
162
+ put(key:K, value:V){
163
+ this.obj[key] = value;
164
+ }
165
+ has(key:K):boolean{
166
+ return key in this.obj;
167
+ }
168
+ entrys(){
169
+
170
+ }
171
+ }
172
+
173
+ class SKVC{
174
+ stringMap:SHashMap<string,string> = new SHashMap();
175
+
176
+ constructor(){}
177
+ }
@@ -0,0 +1,26 @@
1
+ /**可以序列化为JSON文件的Object*/
2
+ //export type JObject={
3
+ // [key: string]: JObject | string | number | boolean | null | Array<JObject| string | number | boolean | null>;
4
+ //}
5
+ export type JToken = JObject|JArray|JValue;
6
+ export type JValue = number|string|boolean|null;
7
+ export type JArray = Array<JToken>;
8
+ export type JObject = {
9
+ [key:string]:JToken;
10
+ }
11
+
12
+
13
+
14
+ /**未知格式的Object*/
15
+ export type AnyObject={
16
+ [key: string]:any;
17
+ }
18
+
19
+ /**用于存储消息的Entry
20
+ * 暂时存于Util
21
+ * 未来应转入LaMChar中
22
+ */
23
+ export type MessageEntity={
24
+ role: string;
25
+ content:string;
26
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './UtilFunctions';
2
+ export * from './UtilInterfaces';
3
+ export * from './UtilCodecs';
package/tsCompile.bat ADDED
@@ -0,0 +1,3 @@
1
+ @echo off
2
+ tsc
3
+ pause
@@ -0,0 +1,3 @@
1
+ @echo off
2
+ tsc -w
3
+ pause
package/tsconfig.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "compilerOptions": {
3
+ "allowJs": true,
4
+ "strict": true,
5
+ "target": "ES2022",
6
+ "module": "commonjs",
7
+ "outDir": "./dist",
8
+ "declaration": true,
9
+ "baseUrl": ".",
10
+ "paths": {
11
+ "@/*": ["./*"],
12
+ "DataController": ["./src/DataController"],
13
+ "LaMAdapter": ["./src/LaMAdapter"],
14
+ "TranslationAdapter": ["./src/TranslationAdapter"],
15
+ "Utils": ["./src/Utils"],
16
+ "LaMChar": ["./src/LaMChar"],
17
+ "TTSAdapter": ["./src/TTSAdapter"]
18
+ }
19
+ },
20
+ "include": ["./src/**/*.ts", "./src/**/*.js"],
21
+ "exclude": ["./node_modules/**/*"]
22
+ }