@zwa73/utils 1.0.34 → 1.0.36
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/UtilFunctions.d.ts +44 -41
- package/dist/UtilFunctions.js +76 -72
- 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/UtilFunctions.ts +3 -1
- package/src/UtilInterfaces.ts +10 -1
package/dist/UtilFunctions.js
CHANGED
|
@@ -1,76 +1,80 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.UtilFunc = void 0;
|
|
4
4
|
const crypto = require("crypto");
|
|
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
|
-
|
|
5
|
+
/**常用函数 */
|
|
6
|
+
var UtilFunc;
|
|
7
|
+
(function (UtilFunc) {
|
|
8
|
+
/**获取当前时间戳
|
|
9
|
+
* number ()
|
|
10
|
+
* @returns {number} 时间戳
|
|
11
|
+
*/
|
|
12
|
+
function getTime() {
|
|
13
|
+
return new Date().getTime();
|
|
14
|
+
}
|
|
15
|
+
UtilFunc.getTime = getTime;
|
|
16
|
+
/**初始化对象的字段
|
|
17
|
+
* void (Object,string,any)
|
|
18
|
+
* @param {Record<string,T>} obj - 所要初始化的对象
|
|
19
|
+
* @param {string} field - 所要初始化的字段
|
|
20
|
+
* @param {T} defaultVal - 默认值
|
|
21
|
+
* @returns {T} - 最终值
|
|
22
|
+
*/
|
|
23
|
+
function initField(obj, field, defaultVal) {
|
|
24
|
+
if (!(field in obj))
|
|
25
|
+
obj[field] = defaultVal;
|
|
26
|
+
return obj[field];
|
|
27
|
+
}
|
|
28
|
+
UtilFunc.initField = initField;
|
|
29
|
+
/**生成一串uuid
|
|
30
|
+
* string ()
|
|
31
|
+
* @returns {string} uuid
|
|
32
|
+
*/
|
|
33
|
+
function genUUID() {
|
|
34
|
+
return crypto.randomBytes(16).toString("hex");
|
|
35
|
+
}
|
|
36
|
+
UtilFunc.genUUID = genUUID;
|
|
37
|
+
/**计算Hash
|
|
38
|
+
* string ()
|
|
39
|
+
* @param {string} str - 待计算的字符串
|
|
40
|
+
* @returns {string} hash
|
|
41
|
+
*/
|
|
42
|
+
function calcHash(str) {
|
|
43
|
+
return crypto.createHash('md5').update(str).digest('hex');
|
|
44
|
+
}
|
|
45
|
+
UtilFunc.calcHash = calcHash;
|
|
46
|
+
/**深克隆 序列化并反序列化
|
|
47
|
+
* @template {T} T - JToken类型的泛型
|
|
48
|
+
* @param {T} obj - 克隆目标
|
|
49
|
+
* @returns {T} 克隆结果
|
|
50
|
+
*/
|
|
51
|
+
function deepClone(obj) {
|
|
52
|
+
return JSON.parse(JSON.stringify(obj));
|
|
53
|
+
}
|
|
54
|
+
UtilFunc.deepClone = deepClone;
|
|
55
|
+
/**是否为安全的数字
|
|
56
|
+
* @param {number} num - 所要检测的数字
|
|
57
|
+
* @returns {boolean} 是否安全
|
|
58
|
+
*/
|
|
59
|
+
function isSafeNumber(num) {
|
|
60
|
+
if (num === undefined || num == null || isNaN(num))
|
|
61
|
+
return false;
|
|
62
|
+
if (typeof num === 'number')
|
|
63
|
+
return true;
|
|
58
64
|
return false;
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
exports.sleep = sleep;
|
|
65
|
+
}
|
|
66
|
+
UtilFunc.isSafeNumber = isSafeNumber;
|
|
67
|
+
/**等待 timeMs 毫秒
|
|
68
|
+
* @async
|
|
69
|
+
* @param {number} timeMs - 等待的毫秒数
|
|
70
|
+
* @returns {Promise<boolean>}
|
|
71
|
+
*/
|
|
72
|
+
async function sleep(timeMs) {
|
|
73
|
+
return new Promise(function (resolve, rejecte) {
|
|
74
|
+
let timer = setTimeout(function () {
|
|
75
|
+
resolve(true);
|
|
76
|
+
}, timeMs);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
UtilFunc.sleep = sleep;
|
|
80
|
+
})(UtilFunc = exports.UtilFunc || (exports.UtilFunc = {}));
|
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/UtilFunctions.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import * as crypto from "crypto";
|
|
2
2
|
import { JToken } from "./UtilInterfaces";
|
|
3
3
|
|
|
4
|
+
/**常用函数 */
|
|
5
|
+
export namespace UtilFunc{
|
|
4
6
|
/**获取当前时间戳
|
|
5
7
|
* number ()
|
|
6
8
|
* @returns {number} 时间戳
|
|
@@ -72,5 +74,5 @@ export async function sleep(timeMs: number): Promise<boolean> {
|
|
|
72
74
|
}, timeMs);
|
|
73
75
|
});
|
|
74
76
|
}
|
|
75
|
-
|
|
77
|
+
}
|
|
76
78
|
|
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
|
|