ph-utils 0.5.0 → 0.5.1
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/lib/file.d.ts +3 -3
- package/lib/file.js +5 -5
- package/package.json +1 -1
package/lib/file.d.ts
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
/**
|
2
2
|
* 读取文件内容为JSON格式
|
3
3
|
* @param filepath 读取的文件路径
|
4
|
-
* @param
|
4
|
+
* @param defaultValue 读取失败时,提供默认值, 如果不提供传参数则[抛出异常]
|
5
5
|
*
|
6
|
-
* @example <caption>1. 文件不存在时,
|
6
|
+
* @example <caption>1. 文件不存在时, 默认为: null</caption>
|
7
7
|
* await readJSON("./not-exists.json", true);
|
8
8
|
*
|
9
9
|
* @returns Promise<unknown>
|
10
10
|
*/
|
11
|
-
export declare function readJSON<T>(filepath: string,
|
11
|
+
export declare function readJSON<T>(filepath: string, defaultValue?: T): Promise<T>;
|
12
12
|
/**
|
13
13
|
* 写入 JSON 格式的数据到文件
|
14
14
|
* @param file 待写入的文件
|
package/lib/file.js
CHANGED
@@ -4,19 +4,19 @@ import fs from "node:fs";
|
|
4
4
|
/**
|
5
5
|
* 读取文件内容为JSON格式
|
6
6
|
* @param filepath 读取的文件路径
|
7
|
-
* @param
|
7
|
+
* @param defaultValue 读取失败时,提供默认值, 如果不提供传参数则[抛出异常]
|
8
8
|
*
|
9
|
-
* @example <caption>1. 文件不存在时,
|
9
|
+
* @example <caption>1. 文件不存在时, 默认为: null</caption>
|
10
10
|
* await readJSON("./not-exists.json", true);
|
11
11
|
*
|
12
12
|
* @returns Promise<unknown>
|
13
13
|
*/
|
14
|
-
export function readJSON(filepath,
|
14
|
+
export function readJSON(filepath, defaultValue) {
|
15
15
|
return new Promise((resolve, reject) => {
|
16
16
|
fs.readFile(path.resolve(filepath), "utf-8", (err, data) => {
|
17
17
|
if (err) {
|
18
|
-
if (
|
19
|
-
resolve(
|
18
|
+
if (defaultValue !== undefined) {
|
19
|
+
resolve(defaultValue);
|
20
20
|
}
|
21
21
|
else {
|
22
22
|
reject(err);
|