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