@zwa73/utils 1.0.24 → 1.0.26

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/UtilCom.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.shttpPost = exports.shttpsPost = void 0;
4
+ const UtilInterfaces_1 = require("./UtilInterfaces");
4
5
  const https = require("https");
5
6
  const http = require("http");
6
7
  /**发送一个POST请求并接受数据
@@ -16,7 +17,7 @@ function shttpsPost(json, options, timeLimit = -1) {
16
17
  let hasTimeLimit = (timeLimit >= 10);
17
18
  if (hasTimeLimit)
18
19
  timeLimit *= 1000;
19
- let jsonStr = JSON.stringify(json);
20
+ let jsonStr = (0, UtilInterfaces_1.stringifyJToken)(json);
20
21
  return new Promise(function (resolve, rejecte) {
21
22
  let req = https.request(options, function (res) {
22
23
  //请求超时
@@ -41,7 +42,7 @@ function shttpsPost(json, options, timeLimit = -1) {
41
42
  if (resdata != "") {
42
43
  try {
43
44
  let obj = JSON.parse(resdata);
44
- console.log("shttpsPost 接受信息:\r\n" + JSON.stringify(obj));
45
+ console.log("shttpsPost 接受信息:\r\n" + (0, UtilInterfaces_1.stringifyJToken)(obj));
45
46
  //console.log(obj);
46
47
  resolve(obj);
47
48
  return;
@@ -87,7 +88,7 @@ function shttpPost(json, options, timeLimit = -1) {
87
88
  let hasTimeLimit = (timeLimit >= 10);
88
89
  if (hasTimeLimit)
89
90
  timeLimit *= 1000;
90
- let jsonStr = JSON.stringify(json);
91
+ let jsonStr = (0, UtilInterfaces_1.stringifyJToken)(json);
91
92
  return new Promise(function (resolve, rejecte) {
92
93
  let req = http.request(options, function (res) {
93
94
  try {
@@ -112,7 +113,7 @@ function shttpPost(json, options, timeLimit = -1) {
112
113
  throw "shttpPost 接收反馈错误: resdata 为空";
113
114
  try {
114
115
  let obj = JSON.parse(resdata);
115
- console.log("shttpPost 接受信息:\r\n" + JSON.stringify(obj));
116
+ console.log("shttpPost 接受信息:\r\n" + (0, UtilInterfaces_1.stringifyJToken)(obj));
116
117
  //console.log(obj);
117
118
  resolve(obj);
118
119
  return;
@@ -89,18 +89,25 @@ export declare function writeJSONFile(filePath: string, token: JToken): Promise<
89
89
  * @returns {string} uuid
90
90
  */
91
91
  export declare function genUUID(): string;
92
+ /**计算Hash
93
+ * string ()
94
+ * @param {string} str - 待计算的字符串
95
+ * @returns {string} hash
96
+ */
97
+ export declare function calcHash(str: string): string;
92
98
  /**深克隆 序列化并反序列化
93
- * @template {T}
99
+ * @template {T} T - JToken类型的泛型
94
100
  * @param {T} obj - 克隆目标
95
101
  * @returns {T} 克隆结果
96
102
  */
97
- export declare function deepClone<T>(obj: T): T;
103
+ export declare function deepClone<T extends JToken>(obj: T): T;
98
104
  /**是否为安全的数字
99
105
  * @param {number} num - 所要检测的数字
100
106
  * @returns {boolean} 是否安全
101
107
  */
102
108
  export declare function isSafeNumber(num: number): boolean;
103
109
  /**等待 timeMs 毫秒
110
+ * @async
104
111
  * @param {number} timeMs - 等待的毫秒数
105
112
  * @returns {Promise<boolean>}
106
113
  */
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fileSearch = exports.sleep = exports.isSafeNumber = exports.deepClone = exports.genUUID = exports.writeJSONFile = exports.loadJSONFile = exports.loadJSONFileSync = exports.ensurePathExistsSync = exports.ensurePathExists = exports.createPathSync = exports.createPath = exports.pathExistsSync = exports.pathExists = exports.initField = exports.getTime = void 0;
3
+ exports.fileSearch = exports.sleep = exports.isSafeNumber = exports.deepClone = exports.calcHash = exports.genUUID = exports.writeJSONFile = exports.loadJSONFile = exports.loadJSONFileSync = exports.ensurePathExistsSync = exports.ensurePathExists = exports.createPathSync = exports.createPath = exports.pathExistsSync = exports.pathExists = exports.initField = exports.getTime = void 0;
4
4
  const fs = require("fs");
5
5
  const crypto = require("crypto");
6
6
  const path = require("path");
7
+ const UtilInterfaces_1 = require("./UtilInterfaces");
7
8
  /**获取当前时间戳
8
9
  * number ()
9
10
  * @returns {number} 时间戳
@@ -166,7 +167,7 @@ exports.loadJSONFile = loadJSONFile;
166
167
  * @returns {Promise<void>}
167
168
  */
168
169
  async function writeJSONFile(filePath, token) {
169
- let str = JSON.stringify(token, null, "\t");
170
+ let str = (0, UtilInterfaces_1.stringifyJToken)(token);
170
171
  if (path.extname(filePath) !== '.json')
171
172
  filePath += '.json';
172
173
  // 判断文件路径是否存在 不存在则创建
@@ -191,8 +192,17 @@ function genUUID() {
191
192
  return crypto.randomBytes(16).toString("hex");
192
193
  }
193
194
  exports.genUUID = genUUID;
195
+ /**计算Hash
196
+ * string ()
197
+ * @param {string} str - 待计算的字符串
198
+ * @returns {string} hash
199
+ */
200
+ function calcHash(str) {
201
+ return crypto.createHash('md5').update(str).digest('hex');
202
+ }
203
+ exports.calcHash = calcHash;
194
204
  /**深克隆 序列化并反序列化
195
- * @template {T}
205
+ * @template {T} T - JToken类型的泛型
196
206
  * @param {T} obj - 克隆目标
197
207
  * @returns {T} 克隆结果
198
208
  */
@@ -213,10 +223,11 @@ function isSafeNumber(num) {
213
223
  }
214
224
  exports.isSafeNumber = isSafeNumber;
215
225
  /**等待 timeMs 毫秒
226
+ * @async
216
227
  * @param {number} timeMs - 等待的毫秒数
217
228
  * @returns {Promise<boolean>}
218
229
  */
219
- function sleep(timeMs) {
230
+ async function sleep(timeMs) {
220
231
  return new Promise(function (resolve, rejecte) {
221
232
  let timer = setTimeout(function () {
222
233
  resolve(true);
@@ -1,10 +1,23 @@
1
- export type JToken = JObject | JArray | JValue;
1
+ export type JToken = JObject | JArray | JValue | IJData;
2
2
  export type JValue = number | string | boolean | null;
3
3
  export type JArray = Array<JToken>;
4
4
  /**可以序列化为JSON文件的Object*/
5
5
  export type JObject = {
6
6
  [key: string]: JToken;
7
7
  };
8
+ /**可以保存为JToken的类
9
+ */
10
+ export interface IJData {
11
+ /**保存为JToken
12
+ */
13
+ toJSON(): JToken;
14
+ }
15
+ /**将JToken转换为字符串
16
+ * @param {JToken} token - 待转换的Token
17
+ * @param {string|number|null} space - 插入的空格 数字为空格数量 默认为制表符\t
18
+ * @returns 转换完成的字符串
19
+ */
20
+ export declare function stringifyJToken(token: JToken, space?: string | number | null | undefined): string;
8
21
  /**未知格式的Object*/
9
22
  export type AnyObject = {
10
23
  [key: string]: any;
@@ -17,21 +30,3 @@ export type MessageEntity = {
17
30
  role: string;
18
31
  content: string;
19
32
  };
20
- /**可以保存为JObject的类
21
- */
22
- export interface IJData {
23
- /**保存为JObject
24
- */
25
- saveToJObject(): JObject;
26
- }
27
- export type JDToken = JToken | IJData | JDArray | JDObject;
28
- export type JDArray = Array<JDToken>;
29
- /**可以转换为JObject的Object*/
30
- export type JDObject = {
31
- [key: string]: JDToken;
32
- };
33
- /**将JDToken转换为JToken
34
- * @param {JDToken} token - 待转换的JDToken
35
- * @returns {JToken} - 转换完成的JToken
36
- */
37
- export declare function parseJOData(token: JDToken): JToken;
@@ -1,29 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseJOData = void 0;
4
- /**将JDToken转换为JToken
5
- * @param {JDToken} token - 待转换的JDToken
6
- * @returns {JToken} - 转换完成的JToken
3
+ exports.stringifyJToken = void 0;
4
+ /**将JToken转换为字符串
5
+ * @param {JToken} token - 待转换的Token
6
+ * @param {string|number|null} space - 插入的空格 数字为空格数量 默认为制表符\t
7
+ * @returns 转换完成的字符串
7
8
  */
8
- function parseJOData(token) {
9
- if (token && typeof token.saveToJObject === 'function')
10
- return token.saveToJObject();
11
- if (token && (typeof token === 'number' ||
12
- typeof token === 'boolean' ||
13
- typeof token === 'string' ||
14
- token === null))
15
- return token;
16
- if (token && Array.isArray(token)) {
17
- let outArr = [];
18
- for (let item of token)
19
- outArr.push(parseJOData(item));
20
- return outArr;
21
- }
22
- let nobj = {};
23
- for (let key in token) {
24
- let value = token[key];
25
- nobj[key] = parseJOData(value);
26
- }
27
- return nobj;
9
+ function stringifyJToken(token, space = "\t") {
10
+ if (space == null)
11
+ space = undefined;
12
+ return JSON.stringify(token, null, space);
28
13
  }
29
- exports.parseJOData = parseJOData;
14
+ exports.stringifyJToken = stringifyJToken;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.24",
3
+ "version": "1.0.26",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/UtilCom.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { JObject } from "./UtilInterfaces";
1
+ import { JObject, stringifyJToken } from "./UtilInterfaces";
2
2
  import * as https from 'https';
3
3
  import * as http from 'http';
4
4
 
@@ -17,7 +17,7 @@ export function shttpsPost(json:JObject,options:Object,timeLimit:number=-1):Prom
17
17
  if(hasTimeLimit)
18
18
  timeLimit*=1000
19
19
 
20
- let jsonStr = JSON.stringify(json);
20
+ let jsonStr = stringifyJToken(json);
21
21
 
22
22
  return new Promise(function(resolve, rejecte){
23
23
  let req = https.request(options, function(res){
@@ -46,7 +46,7 @@ export function shttpsPost(json:JObject,options:Object,timeLimit:number=-1):Prom
46
46
  if(resdata!=""){
47
47
  try{
48
48
  let obj = JSON.parse(resdata);
49
- console.log("shttpsPost 接受信息:\r\n"+JSON.stringify(obj));
49
+ console.log("shttpsPost 接受信息:\r\n"+stringifyJToken(obj));
50
50
  //console.log(obj);
51
51
  resolve(obj);
52
52
  return;
@@ -97,7 +97,7 @@ export function shttpPost(json:JObject,options:Object,timeLimit:number=-1):Promi
97
97
  if(hasTimeLimit)
98
98
  timeLimit*=1000
99
99
 
100
- let jsonStr = JSON.stringify(json);
100
+ let jsonStr = stringifyJToken(json);
101
101
 
102
102
  return new Promise(function(resolve, rejecte){
103
103
  let req = http.request(options, function(res){
@@ -126,7 +126,7 @@ export function shttpPost(json:JObject,options:Object,timeLimit:number=-1):Promi
126
126
  throw "shttpPost 接收反馈错误: resdata 为空";
127
127
  try{
128
128
  let obj = JSON.parse(resdata);
129
- console.log("shttpPost 接受信息:\r\n"+JSON.stringify(obj));
129
+ console.log("shttpPost 接受信息:\r\n"+stringifyJToken(obj));
130
130
  //console.log(obj);
131
131
  resolve(obj);
132
132
  return;
@@ -1,7 +1,7 @@
1
1
  import * as fs from "fs";
2
2
  import * as crypto from "crypto";
3
3
  import * as path from "path";
4
- import { JArray, JObject, JToken } from "./UtilInterfaces";
4
+ import { JArray, JObject, JToken, stringifyJToken } from "./UtilInterfaces";
5
5
 
6
6
  /**获取当前时间戳
7
7
  * number ()
@@ -203,7 +203,7 @@ export async function writeJSONFile(
203
203
  filePath: string,
204
204
  token: JToken
205
205
  ): Promise<void> {
206
- let str = JSON.stringify(token, null, "\t");
206
+ let str = stringifyJToken(token);
207
207
  if (path.extname(filePath) !== '.json') filePath += '.json';
208
208
 
209
209
  // 判断文件路径是否存在 不存在则创建
@@ -226,13 +226,21 @@ export async function writeJSONFile(
226
226
  export function genUUID() {
227
227
  return crypto.randomBytes(16).toString("hex");
228
228
  }
229
+ /**计算Hash
230
+ * string ()
231
+ * @param {string} str - 待计算的字符串
232
+ * @returns {string} hash
233
+ */
234
+ export function calcHash(str:string) {
235
+ return crypto.createHash('md5').update(str).digest('hex');
236
+ }
229
237
 
230
238
  /**深克隆 序列化并反序列化
231
- * @template {T}
239
+ * @template {T} T - JToken类型的泛型
232
240
  * @param {T} obj - 克隆目标
233
241
  * @returns {T} 克隆结果
234
242
  */
235
- export function deepClone<T>(obj: T): T {
243
+ export function deepClone<T extends JToken>(obj: T): T {
236
244
  return JSON.parse(JSON.stringify(obj));
237
245
  }
238
246
 
@@ -247,10 +255,11 @@ export function isSafeNumber(num: number): boolean {
247
255
  }
248
256
 
249
257
  /**等待 timeMs 毫秒
258
+ * @async
250
259
  * @param {number} timeMs - 等待的毫秒数
251
260
  * @returns {Promise<boolean>}
252
261
  */
253
- export function sleep(timeMs: number): Promise<boolean> {
262
+ export async function sleep(timeMs: number): Promise<boolean> {
254
263
  return new Promise(function (resolve, rejecte) {
255
264
  let timer = setTimeout(function () {
256
265
  resolve(true);
@@ -1,11 +1,29 @@
1
1
 
2
- export type JToken = JObject|JArray|JValue;
2
+ export type JToken = JObject|JArray|JValue|IJData;
3
3
  export type JValue = number|string|boolean|null;
4
4
  export type JArray = Array<JToken>;
5
5
  /**可以序列化为JSON文件的Object*/
6
6
  export type JObject = {
7
7
  [key:string]:JToken;
8
8
  }
9
+ /**可以保存为JToken的类
10
+ */
11
+ export interface IJData{
12
+ /**保存为JToken
13
+ */
14
+ toJSON():JToken;
15
+ }
16
+ /**将JToken转换为字符串
17
+ * @param {JToken} token - 待转换的Token
18
+ * @param {string|number|null} space - 插入的空格 数字为空格数量 默认为制表符\t
19
+ * @returns 转换完成的字符串
20
+ */
21
+ export function stringifyJToken(token:JToken,space:string|number|null|undefined="\t"){
22
+ if(space==null)
23
+ space=undefined;
24
+ return JSON.stringify(token,null,space);
25
+ }
26
+
9
27
 
10
28
  /**未知格式的Object*/
11
29
  export type AnyObject={
@@ -20,46 +38,3 @@ export type MessageEntity={
20
38
  role: string;
21
39
  content:string;
22
40
  }
23
-
24
- /**可以保存为JObject的类
25
- */
26
- export interface IJData{
27
- /**保存为JObject
28
- */
29
- saveToJObject():JObject;
30
- }
31
- export type JDToken = JToken|IJData|JDArray|JDObject;
32
- export type JDArray = Array<JDToken>;
33
- /**可以转换为JObject的Object*/
34
- export type JDObject = {
35
- [key:string]:JDToken;
36
- }
37
-
38
- /**将JDToken转换为JToken
39
- * @param {JDToken} token - 待转换的JDToken
40
- * @returns {JToken} - 转换完成的JToken
41
- */
42
- export function parseJOData(token:JDToken):JToken{
43
- if(token && typeof (token as any as IJData).saveToJObject === 'function')
44
- return (token as any as IJData).saveToJObject();
45
-
46
- if(token && (typeof token === 'number' ||
47
- typeof token === 'boolean' ||
48
- typeof token === 'string' ||
49
- token === null)
50
- )return token;
51
-
52
- if(token && Array.isArray(token)){
53
- let outArr = [];
54
- for(let item of token)
55
- outArr.push(parseJOData(item));
56
- return outArr;
57
- }
58
-
59
- let nobj = {} as JObject;
60
- for(let key in (token as any as JDObject)){
61
- let value = (token as any as JDObject)[key];
62
- nobj[key] = parseJOData(value);
63
- }
64
- return nobj;
65
- }