@zwa73/utils 1.0.15 → 1.0.17

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.
@@ -6,11 +6,47 @@ import { JObject } from "./UtilInterfaces";
6
6
  export declare function getTime(): number;
7
7
  /**初始化对象的字段
8
8
  * void (Object,string,any)
9
- * @param {Record<string,any>} obj - 所要初始化的对象
9
+ * @param {Record<string,T>} obj - 所要初始化的对象
10
10
  * @param {string} field - 所要初始化的字段
11
- * @param {any} defaultVal - 默认值
11
+ * @param {T} defaultVal - 默认值
12
+ * @returns {T} - 最终值
12
13
  */
13
- export declare function initField(obj: Record<string, any>, field: string, defaultVal: any): void;
14
+ export declare function initField<T>(obj: Record<string, T>, field: string, defaultVal: T): T;
15
+ /**验证路径 文件或文件夹 是否存在 异步
16
+ * @async
17
+ * @param {string} filePath - 待验证的路径
18
+ * @returns {Promise<boolean>} - 是否存在
19
+ */
20
+ export declare function pathExists(filePath: string): Promise<boolean>;
21
+ /**验证路径 文件或文件夹 是否存在 同步
22
+ * @param {string} filePath - 待验证的路径
23
+ * @returns {boolean} - 是否存在
24
+ */
25
+ export declare function pathExistsSync(filePath: string): boolean;
26
+ /**路径不存在时创建路径 以path.sep结尾时创建文件夹 异步
27
+ * @async
28
+ * @param {string} filePath - 待创建的路径
29
+ * @param {boolean} isDir - 强制创建一个文件夹
30
+ * @returns {Promise<boolean>} - 是否成功创建
31
+ */
32
+ export declare function createPath(filePath: string, isDir?: boolean): Promise<boolean>;
33
+ /**路径不存在时创建路径 以path.sep结尾时创建文件夹 同步
34
+ * @param {string} filePath - 待创建的路径
35
+ * @param {boolean} isDir - 强制创建一个文件夹
36
+ * @returns {boolean} - 是否成功创建
37
+ */
38
+ export declare function createPathSync(filePath: string, isDir?: boolean): boolean;
39
+ /**确保路径存在 不存在时创建 异步
40
+ * @async
41
+ * @param {string} filePath - 待验证的路径
42
+ * @returns {Promise<boolean>} - 是否成功执行 创建或已存在
43
+ */
44
+ export declare function ensurePathExists(filePath: string): Promise<boolean>;
45
+ /**确保路径存在 不存在时创建 同步
46
+ * @param {string} filePath - 待验证的路径
47
+ * @returns {boolean} - 是否成功执行 创建或已存在
48
+ */
49
+ export declare function ensurePathExistsSync(filePath: string): boolean;
14
50
  /**加载json文件 同步
15
51
  * Object (string)
16
52
  * @param {string} filePath - 文件路径
@@ -1,6 +1,6 @@
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.initField = exports.getTime = void 0;
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;
4
4
  const fs = require("fs");
5
5
  const crypto = require("crypto");
6
6
  const path = require("path");
@@ -14,15 +14,117 @@ function getTime() {
14
14
  exports.getTime = getTime;
15
15
  /**初始化对象的字段
16
16
  * void (Object,string,any)
17
- * @param {Record<string,any>} obj - 所要初始化的对象
17
+ * @param {Record<string,T>} obj - 所要初始化的对象
18
18
  * @param {string} field - 所要初始化的字段
19
- * @param {any} defaultVal - 默认值
19
+ * @param {T} defaultVal - 默认值
20
+ * @returns {T} - 最终值
20
21
  */
21
22
  function initField(obj, field, defaultVal) {
22
23
  if (!(field in obj))
23
24
  obj[field] = defaultVal;
25
+ return obj[field];
24
26
  }
25
27
  exports.initField = initField;
28
+ /**验证路径 文件或文件夹 是否存在 异步
29
+ * @async
30
+ * @param {string} filePath - 待验证的路径
31
+ * @returns {Promise<boolean>} - 是否存在
32
+ */
33
+ async function pathExists(filePath) {
34
+ try {
35
+ const stats = await fs.promises.stat(filePath);
36
+ await fs.promises.access(filePath);
37
+ return true;
38
+ }
39
+ catch (e) {
40
+ return false;
41
+ }
42
+ }
43
+ exports.pathExists = pathExists;
44
+ /**验证路径 文件或文件夹 是否存在 同步
45
+ * @param {string} filePath - 待验证的路径
46
+ * @returns {boolean} - 是否存在
47
+ */
48
+ function pathExistsSync(filePath) {
49
+ try {
50
+ fs.accessSync(filePath);
51
+ return true;
52
+ }
53
+ catch (e) {
54
+ return false;
55
+ }
56
+ }
57
+ exports.pathExistsSync = pathExistsSync;
58
+ /**路径不存在时创建路径 以path.sep结尾时创建文件夹 异步
59
+ * @async
60
+ * @param {string} filePath - 待创建的路径
61
+ * @param {boolean} isDir - 强制创建一个文件夹
62
+ * @returns {Promise<boolean>} - 是否成功创建
63
+ */
64
+ async function createPath(filePath, isDir) {
65
+ if (isDir == true)
66
+ filePath = path.join(filePath, path.sep);
67
+ try {
68
+ if (filePath.endsWith(path.sep)) {
69
+ await fs.promises.mkdir(filePath, { recursive: true });
70
+ return true;
71
+ }
72
+ await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
73
+ await fs.promises.open(filePath, 'w');
74
+ return true;
75
+ }
76
+ catch (e) {
77
+ console.log("createPath 错误");
78
+ console.log(e);
79
+ return false;
80
+ }
81
+ }
82
+ exports.createPath = createPath;
83
+ /**路径不存在时创建路径 以path.sep结尾时创建文件夹 同步
84
+ * @param {string} filePath - 待创建的路径
85
+ * @param {boolean} isDir - 强制创建一个文件夹
86
+ * @returns {boolean} - 是否成功创建
87
+ */
88
+ function createPathSync(filePath, isDir) {
89
+ if (isDir == true)
90
+ filePath = path.join(filePath, path.sep);
91
+ try {
92
+ if (filePath.endsWith(path.sep)) {
93
+ fs.mkdirSync(filePath, { recursive: true });
94
+ return true;
95
+ }
96
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
97
+ fs.openSync(filePath, 'w');
98
+ return true;
99
+ }
100
+ catch (e) {
101
+ console.log("createPath 错误");
102
+ console.log(e);
103
+ return false;
104
+ }
105
+ }
106
+ exports.createPathSync = createPathSync;
107
+ /**确保路径存在 不存在时创建 异步
108
+ * @async
109
+ * @param {string} filePath - 待验证的路径
110
+ * @returns {Promise<boolean>} - 是否成功执行 创建或已存在
111
+ */
112
+ async function ensurePathExists(filePath) {
113
+ if (await pathExists(filePath))
114
+ return true;
115
+ return await createPath(filePath);
116
+ }
117
+ exports.ensurePathExists = ensurePathExists;
118
+ /**确保路径存在 不存在时创建 同步
119
+ * @param {string} filePath - 待验证的路径
120
+ * @returns {boolean} - 是否成功执行 创建或已存在
121
+ */
122
+ function ensurePathExistsSync(filePath) {
123
+ if (pathExistsSync(filePath))
124
+ return true;
125
+ return createPathSync(filePath);
126
+ }
127
+ exports.ensurePathExistsSync = ensurePathExistsSync;
26
128
  /**加载json文件 同步
27
129
  * Object (string)
28
130
  * @param {string} filePath - 文件路径
@@ -33,12 +135,8 @@ function loadJSONFileSync(filePath) {
33
135
  filePath += '.json';
34
136
  let str = "";
35
137
  // 判断文件路径是否存在
36
- try {
37
- fs.accessSync(filePath);
38
- // 读取文件
138
+ if (pathExistsSync(filePath))
39
139
  str = fs.readFileSync(filePath, "utf-8");
40
- }
41
- catch (err) { }
42
140
  if (str == "" || str == null)
43
141
  str = "{}";
44
142
  return JSON.parse(str);
@@ -55,12 +153,8 @@ async function loadJSONFile(filePath) {
55
153
  filePath += '.json';
56
154
  let str = "";
57
155
  // 判断文件路径是否存在
58
- try {
59
- await fs.promises.access(filePath);
60
- // 读取文件
156
+ if (await pathExists(filePath))
61
157
  str = await fs.promises.readFile(filePath, "utf-8");
62
- }
63
- catch (err) { }
64
158
  if (str == "" || str == null)
65
159
  str = "{}";
66
160
  return JSON.parse(str);
@@ -77,21 +171,9 @@ async function writeJSONFile(filePath, obj) {
77
171
  let str = JSON.stringify(obj, null, "\t");
78
172
  if (path.extname(filePath) !== '.json')
79
173
  filePath += '.json';
80
- // 判断文件路径是否存在
81
- try {
82
- await fs.promises.access(filePath);
83
- }
84
- catch (err) {
85
- // 不存在则创建
86
- try {
87
- await fs.promises.mkdir(path.dirname(filePath), {
88
- recursive: true,
89
- });
90
- }
91
- catch (e) {
92
- console.log("创建文件错误:" + e);
93
- }
94
- }
174
+ // 判断文件路径是否存在 不存在则创建
175
+ if (!(await pathExists(filePath)))
176
+ await createPath(filePath);
95
177
  // 写入文件
96
178
  try {
97
179
  await fs.promises.writeFile(filePath, str);
@@ -127,7 +209,9 @@ exports.deepClone = deepClone;
127
209
  function isSafeNumber(num) {
128
210
  if (num === undefined || num == null || isNaN(num))
129
211
  return false;
130
- return true;
212
+ if (typeof num === 'number')
213
+ return true;
214
+ return false;
131
215
  }
132
216
  exports.isSafeNumber = isSafeNumber;
133
217
  /**等待 timeMs 毫秒
@@ -157,7 +241,7 @@ function fileSearch(folder, traitRegex) {
157
241
  let stat = fs.lstatSync(subFilePath);
158
242
  //判断是否是文件夹,递归调用
159
243
  if (stat.isDirectory()) {
160
- let subMap = fileSearch(path.join(subFilePath, "/"), traitRegex);
244
+ let subMap = fileSearch(path.join(subFilePath, path.sep), traitRegex);
161
245
  for (let key in subMap)
162
246
  outMap[key] = subMap[key];
163
247
  continue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -13,16 +13,118 @@ export function getTime(): number {
13
13
 
14
14
  /**初始化对象的字段
15
15
  * void (Object,string,any)
16
- * @param {Record<string,any>} obj - 所要初始化的对象
16
+ * @param {Record<string,T>} obj - 所要初始化的对象
17
17
  * @param {string} field - 所要初始化的字段
18
- * @param {any} defaultVal - 默认值
18
+ * @param {T} defaultVal - 默认值
19
+ * @returns {T} - 最终值
19
20
  */
20
- export function initField(
21
- obj: Record<string, any>,
21
+ export function initField<T>(
22
+ obj: Record<string, T>,
22
23
  field: string,
23
- defaultVal: any
24
- ): void {
24
+ defaultVal: T
25
+ ): T {
25
26
  if (!(field in obj)) obj[field] = defaultVal;
27
+ return obj[field];
28
+ }
29
+
30
+ /**验证路径 文件或文件夹 是否存在 异步
31
+ * @async
32
+ * @param {string} filePath - 待验证的路径
33
+ * @returns {Promise<boolean>} - 是否存在
34
+ */
35
+ export async function pathExists(filePath: string):Promise<boolean>{
36
+ try {
37
+ const stats = await fs.promises.stat(filePath);
38
+ await fs.promises.access(filePath);
39
+ return true;
40
+ } catch (e) {
41
+ return false;
42
+ }
43
+ }
44
+
45
+ /**验证路径 文件或文件夹 是否存在 同步
46
+ * @param {string} filePath - 待验证的路径
47
+ * @returns {boolean} - 是否存在
48
+ */
49
+ export function pathExistsSync(filePath: string):boolean{
50
+ try {
51
+ fs.accessSync(filePath);
52
+ return true;
53
+ } catch (e) {
54
+ return false;
55
+ }
56
+ }
57
+
58
+ /**路径不存在时创建路径 以path.sep结尾时创建文件夹 异步
59
+ * @async
60
+ * @param {string} filePath - 待创建的路径
61
+ * @param {boolean} isDir - 强制创建一个文件夹
62
+ * @returns {Promise<boolean>} - 是否成功创建
63
+ */
64
+ export async function createPath(filePath: string, isDir?:boolean):Promise<boolean>{
65
+ if(isDir==true)
66
+ filePath = path.join(filePath,path.sep);
67
+
68
+ try{
69
+ if(filePath.endsWith(path.sep)){
70
+ await fs.promises.mkdir(filePath, {recursive: true});
71
+ return true;
72
+ }
73
+ await fs.promises.mkdir(path.dirname(filePath), {recursive: true});
74
+ await fs.promises.open(filePath, 'w');
75
+ return true;
76
+ }
77
+ catch(e){
78
+ console.log("createPath 错误");
79
+ console.log(e);
80
+ return false;
81
+ }
82
+ }
83
+
84
+ /**路径不存在时创建路径 以path.sep结尾时创建文件夹 同步
85
+ * @param {string} filePath - 待创建的路径
86
+ * @param {boolean} isDir - 强制创建一个文件夹
87
+ * @returns {boolean} - 是否成功创建
88
+ */
89
+ export function createPathSync(filePath: string, isDir?:boolean):boolean{
90
+ if(isDir==true)
91
+ filePath = path.join(filePath,path.sep);
92
+
93
+ try{
94
+ if(filePath.endsWith(path.sep)){
95
+ fs.mkdirSync(filePath, {recursive: true});
96
+ return true;
97
+ }
98
+ fs.mkdirSync(path.dirname(filePath), {recursive: true});
99
+ fs.openSync(filePath, 'w');
100
+ return true;
101
+ }
102
+ catch(e){
103
+ console.log("createPath 错误");
104
+ console.log(e);
105
+ return false;
106
+ }
107
+ }
108
+
109
+ /**确保路径存在 不存在时创建 异步
110
+ * @async
111
+ * @param {string} filePath - 待验证的路径
112
+ * @returns {Promise<boolean>} - 是否成功执行 创建或已存在
113
+ */
114
+ export async function ensurePathExists(filePath: string):Promise<boolean>{
115
+ if(await pathExists(filePath))
116
+ return true;
117
+ return await createPath(filePath);
118
+ }
119
+
120
+ /**确保路径存在 不存在时创建 同步
121
+ * @param {string} filePath - 待验证的路径
122
+ * @returns {boolean} - 是否成功执行 创建或已存在
123
+ */
124
+ export function ensurePathExistsSync(filePath: string):boolean{
125
+ if(pathExistsSync(filePath))
126
+ return true;
127
+ return createPathSync(filePath);
26
128
  }
27
129
 
28
130
  /**加载json文件 同步
@@ -36,11 +138,8 @@ export function loadJSONFileSync(filePath: string): JObject {
36
138
  let str = "";
37
139
 
38
140
  // 判断文件路径是否存在
39
- try {
40
- fs.accessSync(filePath);
41
- // 读取文件
141
+ if(pathExistsSync(filePath))
42
142
  str = fs.readFileSync(filePath, "utf-8");
43
- }catch(err) {}
44
143
 
45
144
  if (str == "" || str == null) str = "{}";
46
145
  return JSON.parse(str);
@@ -57,11 +156,8 @@ export async function loadJSONFile(filePath: string): Promise<JObject> {
57
156
  let str = "";
58
157
 
59
158
  // 判断文件路径是否存在
60
- try {
61
- await fs.promises.access(filePath);
62
- // 读取文件
159
+ if(await pathExists(filePath))
63
160
  str = await fs.promises.readFile(filePath, "utf-8");
64
- } catch (err) {}
65
161
 
66
162
  if (str == "" || str == null) str = "{}";
67
163
  return JSON.parse(str);
@@ -81,19 +177,10 @@ export async function writeJSONFile(
81
177
  let str = JSON.stringify(obj, null, "\t");
82
178
  if (path.extname(filePath) !== '.json') filePath += '.json';
83
179
 
84
- // 判断文件路径是否存在
85
- try {
86
- await fs.promises.access(filePath);
87
- } catch (err) {
88
- // 不存在则创建
89
- try {
90
- await fs.promises.mkdir(path.dirname(filePath), {
91
- recursive: true,
92
- });
93
- } catch (e) {
94
- console.log("创建文件错误:" + e);
95
- }
96
- }
180
+ // 判断文件路径是否存在 不存在则创建
181
+ if(!(await pathExists(filePath)))
182
+ await createPath(filePath);
183
+
97
184
  // 写入文件
98
185
  try {
99
186
  await fs.promises.writeFile(filePath, str);
@@ -126,7 +213,8 @@ export function deepClone<T>(obj: T): T {
126
213
  */
127
214
  export function isSafeNumber(num: number): boolean {
128
215
  if (num === undefined || num == null || isNaN(num)) return false;
129
- return true;
216
+ if(typeof num === 'number') return true;
217
+ return false;
130
218
  }
131
219
 
132
220
  /**等待 timeMs 毫秒
@@ -157,7 +245,7 @@ export function fileSearch(folder: string, traitRegex: string) {
157
245
 
158
246
  //判断是否是文件夹,递归调用
159
247
  if (stat.isDirectory()) {
160
- let subMap = fileSearch(path.join(subFilePath, "/"), traitRegex);
248
+ let subMap = fileSearch(path.join(subFilePath, path.sep), traitRegex);
161
249
  for (let key in subMap) outMap[key] = subMap[key];
162
250
  continue;
163
251
  }