@zwa73/utils 1.0.15 → 1.0.16

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,32 @@ 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
+ * @param {string} filePath - 待验证的路径
17
+ * @returns {Promise<boolean>} - 是否存在
18
+ */
19
+ export declare function pathExists(filePath: string): Promise<boolean>;
20
+ /**验证路径 文件或文件夹 是否存在 同步
21
+ * @param {string} filePath - 待验证的路径
22
+ * @returns {boolean} - 是否存在
23
+ */
24
+ export declare function pathExistsSync(filePath: string): boolean;
25
+ /**创建路径 path.sep 结尾时创建文件夹 异步
26
+ * @param {string} filePath - 待创建的路径
27
+ * @returns {Promise<boolean>} - 是否成功创建
28
+ */
29
+ export declare function createPath(filePath: string): Promise<boolean>;
30
+ /**创建路径 path.sep 结尾时创建文件夹 同步
31
+ * @param {string} filePath - 待创建的路径
32
+ * @returns {boolean} - 是否成功创建
33
+ */
34
+ export declare function createPathSync(filePath: string): boolean;
14
35
  /**加载json文件 同步
15
36
  * Object (string)
16
37
  * @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.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,88 @@ 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
+ * @param {string} filePath - 待验证的路径
30
+ * @returns {Promise<boolean>} - 是否存在
31
+ */
32
+ async function pathExists(filePath) {
33
+ try {
34
+ const stats = await fs.promises.stat(filePath);
35
+ await fs.promises.access(filePath);
36
+ return true;
37
+ }
38
+ catch (e) {
39
+ return false;
40
+ }
41
+ }
42
+ exports.pathExists = pathExists;
43
+ /**验证路径 文件或文件夹 是否存在 同步
44
+ * @param {string} filePath - 待验证的路径
45
+ * @returns {boolean} - 是否存在
46
+ */
47
+ function pathExistsSync(filePath) {
48
+ try {
49
+ fs.accessSync(filePath);
50
+ return true;
51
+ }
52
+ catch (e) {
53
+ return false;
54
+ }
55
+ }
56
+ exports.pathExistsSync = pathExistsSync;
57
+ /**创建路径 path.sep 结尾时创建文件夹 异步
58
+ * @param {string} filePath - 待创建的路径
59
+ * @returns {Promise<boolean>} - 是否成功创建
60
+ */
61
+ async function createPath(filePath) {
62
+ try {
63
+ if (filePath.endsWith(path.sep)) {
64
+ await fs.promises.mkdir(filePath, { recursive: true });
65
+ return true;
66
+ }
67
+ await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
68
+ await fs.promises.open(filePath, 'w');
69
+ return true;
70
+ }
71
+ catch (e) {
72
+ console.log("createPath 错误");
73
+ console.log(e);
74
+ return false;
75
+ }
76
+ }
77
+ exports.createPath = createPath;
78
+ /**创建路径 path.sep 结尾时创建文件夹 同步
79
+ * @param {string} filePath - 待创建的路径
80
+ * @returns {boolean} - 是否成功创建
81
+ */
82
+ function createPathSync(filePath) {
83
+ try {
84
+ if (filePath.endsWith(path.sep)) {
85
+ fs.mkdirSync(filePath, { recursive: true });
86
+ return true;
87
+ }
88
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
89
+ fs.openSync(filePath, 'w');
90
+ return true;
91
+ }
92
+ catch (e) {
93
+ console.log("createPath 错误");
94
+ console.log(e);
95
+ return false;
96
+ }
97
+ }
98
+ exports.createPathSync = createPathSync;
26
99
  /**加载json文件 同步
27
100
  * Object (string)
28
101
  * @param {string} filePath - 文件路径
@@ -33,12 +106,8 @@ function loadJSONFileSync(filePath) {
33
106
  filePath += '.json';
34
107
  let str = "";
35
108
  // 判断文件路径是否存在
36
- try {
37
- fs.accessSync(filePath);
38
- // 读取文件
109
+ if (pathExistsSync(filePath))
39
110
  str = fs.readFileSync(filePath, "utf-8");
40
- }
41
- catch (err) { }
42
111
  if (str == "" || str == null)
43
112
  str = "{}";
44
113
  return JSON.parse(str);
@@ -55,12 +124,8 @@ async function loadJSONFile(filePath) {
55
124
  filePath += '.json';
56
125
  let str = "";
57
126
  // 判断文件路径是否存在
58
- try {
59
- await fs.promises.access(filePath);
60
- // 读取文件
127
+ if (await pathExists(filePath))
61
128
  str = await fs.promises.readFile(filePath, "utf-8");
62
- }
63
- catch (err) { }
64
129
  if (str == "" || str == null)
65
130
  str = "{}";
66
131
  return JSON.parse(str);
@@ -77,21 +142,9 @@ async function writeJSONFile(filePath, obj) {
77
142
  let str = JSON.stringify(obj, null, "\t");
78
143
  if (path.extname(filePath) !== '.json')
79
144
  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
- }
145
+ // 判断文件路径是否存在 不存在则创建
146
+ if (!(await pathExists(filePath)))
147
+ await createPath(filePath);
95
148
  // 写入文件
96
149
  try {
97
150
  await fs.promises.writeFile(filePath, str);
@@ -127,7 +180,9 @@ exports.deepClone = deepClone;
127
180
  function isSafeNumber(num) {
128
181
  if (num === undefined || num == null || isNaN(num))
129
182
  return false;
130
- return true;
183
+ if (typeof num === 'number')
184
+ return true;
185
+ return false;
131
186
  }
132
187
  exports.isSafeNumber = isSafeNumber;
133
188
  /**等待 timeMs 毫秒
@@ -157,7 +212,7 @@ function fileSearch(folder, traitRegex) {
157
212
  let stat = fs.lstatSync(subFilePath);
158
213
  //判断是否是文件夹,递归调用
159
214
  if (stat.isDirectory()) {
160
- let subMap = fileSearch(path.join(subFilePath, "/"), traitRegex);
215
+ let subMap = fileSearch(path.join(subFilePath, path.sep), traitRegex);
161
216
  for (let key in subMap)
162
217
  outMap[key] = subMap[key];
163
218
  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.16",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -13,16 +13,87 @@ 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
+ * @param {string} filePath - 待验证的路径
32
+ * @returns {Promise<boolean>} - 是否存在
33
+ */
34
+ export async function pathExists(filePath: string):Promise<boolean>{
35
+ try {
36
+ const stats = await fs.promises.stat(filePath);
37
+ await fs.promises.access(filePath);
38
+ return true;
39
+ } catch (e) {
40
+ return false;
41
+ }
42
+ }
43
+
44
+ /**验证路径 文件或文件夹 是否存在 同步
45
+ * @param {string} filePath - 待验证的路径
46
+ * @returns {boolean} - 是否存在
47
+ */
48
+ export function pathExistsSync(filePath: string):boolean{
49
+ try {
50
+ fs.accessSync(filePath);
51
+ return true;
52
+ } catch (e) {
53
+ return false;
54
+ }
55
+ }
56
+
57
+ /**创建路径 path.sep 结尾时创建文件夹 异步
58
+ * @param {string} filePath - 待创建的路径
59
+ * @returns {Promise<boolean>} - 是否成功创建
60
+ */
61
+ export async function createPath(filePath: string):Promise<boolean>{
62
+ try{
63
+ if(filePath.endsWith(path.sep)){
64
+ await fs.promises.mkdir(filePath, {recursive: true});
65
+ return true;
66
+ }
67
+ await fs.promises.mkdir(path.dirname(filePath), {recursive: true});
68
+ await fs.promises.open(filePath, 'w');
69
+ return true;
70
+ }
71
+ catch(e){
72
+ console.log("createPath 错误");
73
+ console.log(e);
74
+ return false;
75
+ }
76
+ }
77
+
78
+ /**创建路径 path.sep 结尾时创建文件夹 同步
79
+ * @param {string} filePath - 待创建的路径
80
+ * @returns {boolean} - 是否成功创建
81
+ */
82
+ export function createPathSync(filePath: string):boolean{
83
+ try{
84
+ if(filePath.endsWith(path.sep)){
85
+ fs.mkdirSync(filePath, {recursive: true});
86
+ return true;
87
+ }
88
+ fs.mkdirSync(path.dirname(filePath), {recursive: true});
89
+ fs.openSync(filePath, 'w');
90
+ return true;
91
+ }
92
+ catch(e){
93
+ console.log("createPath 错误");
94
+ console.log(e);
95
+ return false;
96
+ }
26
97
  }
27
98
 
28
99
  /**加载json文件 同步
@@ -36,11 +107,8 @@ export function loadJSONFileSync(filePath: string): JObject {
36
107
  let str = "";
37
108
 
38
109
  // 判断文件路径是否存在
39
- try {
40
- fs.accessSync(filePath);
41
- // 读取文件
110
+ if(pathExistsSync(filePath))
42
111
  str = fs.readFileSync(filePath, "utf-8");
43
- }catch(err) {}
44
112
 
45
113
  if (str == "" || str == null) str = "{}";
46
114
  return JSON.parse(str);
@@ -57,11 +125,8 @@ export async function loadJSONFile(filePath: string): Promise<JObject> {
57
125
  let str = "";
58
126
 
59
127
  // 判断文件路径是否存在
60
- try {
61
- await fs.promises.access(filePath);
62
- // 读取文件
128
+ if(await pathExists(filePath))
63
129
  str = await fs.promises.readFile(filePath, "utf-8");
64
- } catch (err) {}
65
130
 
66
131
  if (str == "" || str == null) str = "{}";
67
132
  return JSON.parse(str);
@@ -81,19 +146,10 @@ export async function writeJSONFile(
81
146
  let str = JSON.stringify(obj, null, "\t");
82
147
  if (path.extname(filePath) !== '.json') filePath += '.json';
83
148
 
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
- }
149
+ // 判断文件路径是否存在 不存在则创建
150
+ if(!(await pathExists(filePath)))
151
+ await createPath(filePath);
152
+
97
153
  // 写入文件
98
154
  try {
99
155
  await fs.promises.writeFile(filePath, str);
@@ -126,7 +182,8 @@ export function deepClone<T>(obj: T): T {
126
182
  */
127
183
  export function isSafeNumber(num: number): boolean {
128
184
  if (num === undefined || num == null || isNaN(num)) return false;
129
- return true;
185
+ if(typeof num === 'number') return true;
186
+ return false;
130
187
  }
131
188
 
132
189
  /**等待 timeMs 毫秒
@@ -157,7 +214,7 @@ export function fileSearch(folder: string, traitRegex: string) {
157
214
 
158
215
  //判断是否是文件夹,递归调用
159
216
  if (stat.isDirectory()) {
160
- let subMap = fileSearch(path.join(subFilePath, "/"), traitRegex);
217
+ let subMap = fileSearch(path.join(subFilePath, path.sep), traitRegex);
161
218
  for (let key in subMap) outMap[key] = subMap[key];
162
219
  continue;
163
220
  }