@zwa73/utils 1.0.23 → 1.0.25

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;
@@ -4,6 +4,7 @@ exports.fileSearch = exports.sleep = exports.isSafeNumber = exports.deepClone =
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
  // 判断文件路径是否存在 不存在则创建
@@ -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.23",
3
+ "version": "1.0.25",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -12,6 +12,7 @@
12
12
  "author": "zwa73",
13
13
  "license": "ISC",
14
14
  "dependencies": {
15
+ "@types/fluent-ffmpeg": "^2.1.21",
15
16
  "esm": "^3.2.25",
16
17
  "esm-resolve": "^1.0.8",
17
18
  "fluent-ffmpeg": "^2.1.2",
@@ -21,7 +22,6 @@
21
22
  "tiktoken": "^1.0.7"
22
23
  },
23
24
  "devDependencies": {
24
- "@types/fluent-ffmpeg": "^2.1.21",
25
25
  "@types/node": "^18.16.3"
26
26
  }
27
27
  }
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
  // 判断文件路径是否存在 不存在则创建
@@ -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
- }