@zwa73/utils 1.0.22 → 1.0.24

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,4 +1,4 @@
1
- import { JObject } from "./UtilInterfaces";
1
+ import { JObject, JToken } from "./UtilInterfaces";
2
2
  /**获取当前时间戳
3
3
  * number ()
4
4
  * @returns {number} 时间戳
@@ -51,24 +51,39 @@ export declare function ensurePathExistsSync(filePath: string, isDir?: boolean):
51
51
  /**加载json文件 同步
52
52
  * Object (string)
53
53
  * @param {string} filePath - 文件路径
54
- * @returns {JObject}
54
+ * @returns {JObject} - 加载完成的对象或空{}
55
55
  */
56
56
  export declare function loadJSONFileSync(filePath: string): JObject;
57
+ /**加载json文件 同步
58
+ * Object (string)
59
+ * @param {string} filePath - 文件路径
60
+ * @param {T} def - 默认值
61
+ * @returns {T} - 加载完成的对象或默认值
62
+ */
63
+ export declare function loadJSONFileSync<T extends JToken>(filePath: string, def: T): T;
57
64
  /**加载json文件 异步
58
65
  * Object (string)
59
66
  * @async
60
67
  * @param {string} filePath - 文件路径
61
- * @returns {JObject}
68
+ * @returns {Promise<JObject>} - 加载完成的对象或空{}
62
69
  */
63
70
  export declare function loadJSONFile(filePath: string): Promise<JObject>;
71
+ /**加载json文件 异步
72
+ * Object (string)
73
+ * @async
74
+ * @param {string} filePath - 文件路径
75
+ * @param {T} T - 默认值
76
+ * @returns {Promise<T>} - 加载完成的对象或默认值
77
+ */
78
+ export declare function loadJSONFile<T extends JToken>(filePath: string, def: T): Promise<T>;
64
79
  /**写入JSON文件
65
80
  * void (string,Object)
66
81
  * @async
67
82
  * @param {string} filePath - 文件路径
68
- * @param {JObject} obj - 所要写入的JObject
83
+ * @param {JToken} token - 所要写入的JToken
69
84
  * @returns {Promise<void>}
70
85
  */
71
- export declare function writeJSONFile(filePath: string, obj: JObject): Promise<void>;
86
+ export declare function writeJSONFile(filePath: string, token: JToken): Promise<void>;
72
87
  /**生成一串uuid
73
88
  * string ()
74
89
  * @returns {string} uuid
@@ -126,38 +126,35 @@ function ensurePathExistsSync(filePath, isDir) {
126
126
  return createPathSync(filePath, isDir);
127
127
  }
128
128
  exports.ensurePathExistsSync = ensurePathExistsSync;
129
- /**加载json文件 同步
130
- * Object (string)
131
- * @param {string} filePath - 文件路径
132
- * @returns {JObject}
133
- */
134
- function loadJSONFileSync(filePath) {
129
+ function loadJSONFileSync(filePath, def) {
135
130
  if (path.extname(filePath) !== '.json')
136
131
  filePath += '.json';
137
132
  let str = "";
138
133
  // 判断文件路径是否存在
139
134
  if (pathExistsSync(filePath))
140
135
  str = fs.readFileSync(filePath, "utf-8");
141
- if (str == "" || str == null)
142
- str = "{}";
136
+ // 如果不存在则返回默认值
137
+ if (str == "" || str == null) {
138
+ if (def !== undefined)
139
+ return def;
140
+ return {};
141
+ }
143
142
  return JSON.parse(str);
144
143
  }
145
144
  exports.loadJSONFileSync = loadJSONFileSync;
146
- /**加载json文件 异步
147
- * Object (string)
148
- * @async
149
- * @param {string} filePath - 文件路径
150
- * @returns {JObject}
151
- */
152
- async function loadJSONFile(filePath) {
145
+ async function loadJSONFile(filePath, def) {
153
146
  if (path.extname(filePath) !== '.json')
154
147
  filePath += '.json';
155
148
  let str = "";
156
149
  // 判断文件路径是否存在
157
150
  if (await pathExists(filePath))
158
151
  str = await fs.promises.readFile(filePath, "utf-8");
159
- if (str == "" || str == null)
160
- str = "{}";
152
+ // 如果不存在则返回默认值
153
+ if (str == "" || str == null) {
154
+ if (def !== undefined)
155
+ return def;
156
+ return {};
157
+ }
161
158
  return JSON.parse(str);
162
159
  }
163
160
  exports.loadJSONFile = loadJSONFile;
@@ -165,11 +162,11 @@ exports.loadJSONFile = loadJSONFile;
165
162
  * void (string,Object)
166
163
  * @async
167
164
  * @param {string} filePath - 文件路径
168
- * @param {JObject} obj - 所要写入的JObject
165
+ * @param {JToken} token - 所要写入的JToken
169
166
  * @returns {Promise<void>}
170
167
  */
171
- async function writeJSONFile(filePath, obj) {
172
- let str = JSON.stringify(obj, null, "\t");
168
+ async function writeJSONFile(filePath, token) {
169
+ let str = JSON.stringify(token, null, "\t");
173
170
  if (path.extname(filePath) !== '.json')
174
171
  filePath += '.json';
175
172
  // 判断文件路径是否存在 不存在则创建
@@ -24,7 +24,7 @@ export interface IJData {
24
24
  */
25
25
  saveToJObject(): JObject;
26
26
  }
27
- export type JDToken = JToken | IJData | JDArray;
27
+ export type JDToken = JToken | IJData | JDArray | JDObject;
28
28
  export type JDArray = Array<JDToken>;
29
29
  /**可以转换为JObject的Object*/
30
30
  export type JDObject = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.22",
3
+ "version": "1.0.24",
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
  }
@@ -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 { JObject } from "./UtilInterfaces";
4
+ import { JArray, JObject, JToken } from "./UtilInterfaces";
5
5
 
6
6
  /**获取当前时间戳
7
7
  * number ()
@@ -131,9 +131,17 @@ export function ensurePathExistsSync(filePath: string, isDir?:boolean):boolean{
131
131
  /**加载json文件 同步
132
132
  * Object (string)
133
133
  * @param {string} filePath - 文件路径
134
- * @returns {JObject}
134
+ * @returns {JObject} - 加载完成的对象或空{}
135
135
  */
136
- export function loadJSONFileSync(filePath: string): JObject {
136
+ export function loadJSONFileSync(filePath: string): JObject
137
+ /**加载json文件 同步
138
+ * Object (string)
139
+ * @param {string} filePath - 文件路径
140
+ * @param {T} def - 默认值
141
+ * @returns {T} - 加载完成的对象或默认值
142
+ */
143
+ export function loadJSONFileSync<T extends JToken>(filePath: string,def: T): T
144
+ export function loadJSONFileSync<T extends JToken>(filePath: string,def?: T): T {
137
145
  if (path.extname(filePath) !== '.json') filePath += '.json';
138
146
 
139
147
  let str = "";
@@ -142,16 +150,31 @@ export function loadJSONFileSync(filePath: string): JObject {
142
150
  if(pathExistsSync(filePath))
143
151
  str = fs.readFileSync(filePath, "utf-8");
144
152
 
145
- if (str == "" || str == null) str = "{}";
153
+ // 如果不存在则返回默认值
154
+ if (str == "" || str == null){
155
+ if(def!==undefined)
156
+ return def;
157
+ return {} as T;
158
+ }
146
159
  return JSON.parse(str);
147
160
  }
161
+
148
162
  /**加载json文件 异步
149
163
  * Object (string)
150
164
  * @async
151
165
  * @param {string} filePath - 文件路径
152
- * @returns {JObject}
166
+ * @returns {Promise<JObject>} - 加载完成的对象或空{}
153
167
  */
154
- export async function loadJSONFile(filePath: string): Promise<JObject> {
168
+ export async function loadJSONFile(filePath: string): Promise<JObject>
169
+ /**加载json文件 异步
170
+ * Object (string)
171
+ * @async
172
+ * @param {string} filePath - 文件路径
173
+ * @param {T} T - 默认值
174
+ * @returns {Promise<T>} - 加载完成的对象或默认值
175
+ */
176
+ export async function loadJSONFile<T extends JToken>(filePath: string,def: T): Promise<T>
177
+ export async function loadJSONFile<T extends JToken>(filePath: string,def?: T): Promise<T> {
155
178
  if (path.extname(filePath) !== '.json') filePath += '.json';
156
179
 
157
180
  let str = "";
@@ -160,7 +183,12 @@ export async function loadJSONFile(filePath: string): Promise<JObject> {
160
183
  if(await pathExists(filePath))
161
184
  str = await fs.promises.readFile(filePath, "utf-8");
162
185
 
163
- if (str == "" || str == null) str = "{}";
186
+ // 如果不存在则返回默认值
187
+ if (str == "" || str == null){
188
+ if(def!==undefined)
189
+ return def;
190
+ return {} as T;
191
+ }
164
192
  return JSON.parse(str);
165
193
  }
166
194
 
@@ -168,14 +196,14 @@ export async function loadJSONFile(filePath: string): Promise<JObject> {
168
196
  * void (string,Object)
169
197
  * @async
170
198
  * @param {string} filePath - 文件路径
171
- * @param {JObject} obj - 所要写入的JObject
199
+ * @param {JToken} token - 所要写入的JToken
172
200
  * @returns {Promise<void>}
173
201
  */
174
202
  export async function writeJSONFile(
175
203
  filePath: string,
176
- obj: JObject
204
+ token: JToken
177
205
  ): Promise<void> {
178
- let str = JSON.stringify(obj, null, "\t");
206
+ let str = JSON.stringify(token, null, "\t");
179
207
  if (path.extname(filePath) !== '.json') filePath += '.json';
180
208
 
181
209
  // 判断文件路径是否存在 不存在则创建
@@ -28,12 +28,13 @@ export interface IJData{
28
28
  */
29
29
  saveToJObject():JObject;
30
30
  }
31
- export type JDToken = JToken|IJData|JDArray;
31
+ export type JDToken = JToken|IJData|JDArray|JDObject;
32
32
  export type JDArray = Array<JDToken>;
33
33
  /**可以转换为JObject的Object*/
34
34
  export type JDObject = {
35
35
  [key:string]:JDToken;
36
36
  }
37
+
37
38
  /**将JDToken转换为JToken
38
39
  * @param {JDToken} token - 待转换的JDToken
39
40
  * @returns {JToken} - 转换完成的JToken