@bluelovers/fs-json 1.0.16 → 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.
- package/CHANGELOG.md +10 -0
- package/index.d.ts +154 -1
- package/index.js +123 -0
- package/index.js.map +1 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [1.0.17](https://github.com/bluelovers/ws-iconv/compare/@bluelovers/fs-json@1.0.16...@bluelovers/fs-json@1.0.17) (2026-03-02)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### 📚 Documentation
|
|
11
|
+
|
|
12
|
+
* 為檔案系統相關套件新增雙語 JSDoc 註解與型別標註 ([83f3845](https://github.com/bluelovers/ws-iconv/commit/83f38456ccc0f5eca4f2a6505bee4caf7e634ba6))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
6
16
|
## [1.0.16](https://github.com/bluelovers/ws-iconv/compare/@bluelovers/fs-json@1.0.15...@bluelovers/fs-json@1.0.16) (2026-03-01)
|
|
7
17
|
|
|
8
18
|
**Note:** Version bump only for package @bluelovers/fs-json
|
package/index.d.ts
CHANGED
|
@@ -1,16 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enhanced JSON File Operations Module
|
|
3
|
+
* 增強型 JSON 檔案操作模組
|
|
4
|
+
*
|
|
5
|
+
* 提供增強的 JSON 檔案讀寫功能,支援結尾換行符號(finalEOL)
|
|
6
|
+
* Provides enhanced JSON file read/write operations with final EOL support
|
|
7
|
+
*
|
|
8
|
+
* @module @bluelovers/fs-json
|
|
9
|
+
* @author bluelovers
|
|
10
|
+
*/
|
|
1
11
|
import { readJSON, readJSONSync, WriteOptions } from 'fs-extra';
|
|
2
12
|
import { JFWriteOptions } from 'jsonfile';
|
|
13
|
+
/**
|
|
14
|
+
* 擴充 fs-extra 的 WriteOptions 介面
|
|
15
|
+
* Extend fs-extra WriteOptions interface
|
|
16
|
+
*/
|
|
3
17
|
declare module 'fs-extra' {
|
|
4
18
|
interface WriteOptions extends Exclude<JFWriteOptions, string | null> {
|
|
19
|
+
/**
|
|
20
|
+
* 是否在 JSON 結尾添加換行符號
|
|
21
|
+
* Whether to add final newline to JSON
|
|
22
|
+
* @default true
|
|
23
|
+
*/
|
|
5
24
|
finalEOL?: boolean;
|
|
6
25
|
}
|
|
7
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* 寫入選項類型
|
|
29
|
+
* Write options type
|
|
30
|
+
*
|
|
31
|
+
* 結合 fs-extra 和 jsonfile 的寫入選項
|
|
32
|
+
* Combines fs-extra and jsonfile write options
|
|
33
|
+
*/
|
|
8
34
|
export type IWriteOptions = WriteOptions & Exclude<JFWriteOptions, string | null>;
|
|
35
|
+
/**
|
|
36
|
+
* 處理寫入選項
|
|
37
|
+
* Handle write options
|
|
38
|
+
*
|
|
39
|
+
* 確保選項物件存在並設定 finalEOL 預設值為 true
|
|
40
|
+
* Ensures options object exists and sets finalEOL default to true
|
|
41
|
+
*
|
|
42
|
+
* @param {IWriteOptions} [options] - 寫入選項 / Write options
|
|
43
|
+
* @returns {IWriteOptions} - 處理後的選項 / Processed options
|
|
44
|
+
* @private
|
|
45
|
+
*/
|
|
9
46
|
export declare function _handleWriteOptions(options?: IWriteOptions): IWriteOptions;
|
|
10
|
-
|
|
47
|
+
/**
|
|
48
|
+
* 從 fs-extra 導出的讀取函數
|
|
49
|
+
* Read functions exported from fs-extra
|
|
50
|
+
*/
|
|
51
|
+
export {
|
|
52
|
+
/**
|
|
53
|
+
* 異步讀取 JSON 檔案
|
|
54
|
+
* Read JSON file asynchronously
|
|
55
|
+
*/
|
|
56
|
+
readJSON,
|
|
57
|
+
/**
|
|
58
|
+
* 同步讀取 JSON 檔案
|
|
59
|
+
* Read JSON file synchronously
|
|
60
|
+
*/
|
|
61
|
+
readJSONSync, };
|
|
62
|
+
/**
|
|
63
|
+
* 輸出 JSON 檔案(異步)
|
|
64
|
+
* Output JSON file (async)
|
|
65
|
+
*
|
|
66
|
+
* 寫入 JSON 檔案,如果目錄不存在則自動創建
|
|
67
|
+
* Writes JSON file, creates directory if it doesn't exist
|
|
68
|
+
*
|
|
69
|
+
* @param {string} file - 檔案路徑 / File path
|
|
70
|
+
* @param {any} data - 要寫入的資料 / Data to write
|
|
71
|
+
* @param {IWriteOptions} [options] - 寫入選項 / Write options
|
|
72
|
+
* @returns {Promise<void>} - Promise / Promise
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* await outputJSON('config.json', { key: 'value' });
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
11
79
|
export declare function outputJSON(file: string, data: any, options?: IWriteOptions): Promise<void>;
|
|
80
|
+
/**
|
|
81
|
+
* 輸出 JSON 檔案(同步)
|
|
82
|
+
* Output JSON file (sync)
|
|
83
|
+
*
|
|
84
|
+
* 同步寫入 JSON 檔案,如果目錄不存在則自動創建
|
|
85
|
+
* Synchronously writes JSON file, creates directory if it doesn't exist
|
|
86
|
+
*
|
|
87
|
+
* @param {string} file - 檔案路徑 / File path
|
|
88
|
+
* @param {any} data - 要寫入的資料 / Data to write
|
|
89
|
+
* @param {IWriteOptions} [options] - 寫入選項 / Write options
|
|
90
|
+
* @returns {void}
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* outputJSONSync('config.json', { key: 'value' });
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
12
97
|
export declare function outputJSONSync(file: string, data: any, options?: IWriteOptions): void;
|
|
98
|
+
/**
|
|
99
|
+
* 寫入 JSON 檔案(異步)
|
|
100
|
+
* Write JSON file (async)
|
|
101
|
+
*
|
|
102
|
+
* 異步寫入 JSON 檔案
|
|
103
|
+
* Writes JSON file asynchronously
|
|
104
|
+
*
|
|
105
|
+
* @param {string} file - 檔案路徑 / File path
|
|
106
|
+
* @param {any} data - 要寫入的資料 / Data to write
|
|
107
|
+
* @param {IWriteOptions} [options] - 寫入選項 / Write options
|
|
108
|
+
* @returns {Promise<void>} - Promise / Promise
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```typescript
|
|
112
|
+
* await writeJSON('data.json', { users: [] });
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
13
115
|
export declare function writeJSON(file: string, data: any, options?: IWriteOptions): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* 寫入 JSON 檔案(同步)
|
|
118
|
+
* Write JSON file (sync)
|
|
119
|
+
*
|
|
120
|
+
* 同步寫入 JSON 檔案
|
|
121
|
+
* Writes JSON file synchronously
|
|
122
|
+
*
|
|
123
|
+
* @param {string} file - 檔案路徑 / File path
|
|
124
|
+
* @param {any} data - 要寫入的資料 / Data to write
|
|
125
|
+
* @param {IWriteOptions} [options] - 寫入選項 / Write options
|
|
126
|
+
* @returns {void}
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* writeJSONSync('data.json', { users: [] });
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
14
133
|
export declare function writeJSONSync(file: string, data: any, options?: IWriteOptions): void;
|
|
134
|
+
/**
|
|
135
|
+
* 將資料字串化為 JSON
|
|
136
|
+
* Stringify data to JSON
|
|
137
|
+
*
|
|
138
|
+
* 將 JavaScript 資料轉換為 JSON 字串,支援結尾換行選項
|
|
139
|
+
* Converts JavaScript data to JSON string with final EOL support
|
|
140
|
+
*
|
|
141
|
+
* @param {any} data - 要字串化的資料 / Data to stringify
|
|
142
|
+
* @param {IWriteOptions} [options] - 字串化選項 / Stringify options
|
|
143
|
+
* @returns {string} - JSON 字串 / JSON string
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* const json = stringifyJSON({ key: 'value' });
|
|
148
|
+
* console.log(json); // '{"key":"value"}\n'
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
15
151
|
export declare function stringifyJSON(data: any, options?: IWriteOptions): string;
|
|
152
|
+
/**
|
|
153
|
+
* 解析 JSON 字串
|
|
154
|
+
* Parse JSON string
|
|
155
|
+
*
|
|
156
|
+
* 從字串或 Uint8Array 解析 JSON 資料
|
|
157
|
+
* Parses JSON data from string or Uint8Array
|
|
158
|
+
*
|
|
159
|
+
* @param {string | Uint8Array} stringOrUint8Array - JSON 字串或 Uint8Array / JSON string or Uint8Array
|
|
160
|
+
* @param {Function} [reviver] - 轉換函數 / Reviver function
|
|
161
|
+
* @returns {any} - 解析後的資料 / Parsed data
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* const obj = parseJSON('{"key":"value"}');
|
|
166
|
+
* const obj2 = parseJSON(new Uint8Array([123, 34, 107, 101, 121, 34, 58, 34, 118, 97, 108, 117, 101, 34, 125]));
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
16
169
|
export declare function parseJSON(stringOrUint8Array: string | Uint8Array, reviver?: (this: any, key: string, value: any) => any): string;
|
package/index.js
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Enhanced JSON File Operations Module
|
|
4
|
+
* 增強型 JSON 檔案操作模組
|
|
5
|
+
*
|
|
6
|
+
* 提供增強的 JSON 檔案讀寫功能,支援結尾換行符號(finalEOL)
|
|
7
|
+
* Provides enhanced JSON file read/write operations with final EOL support
|
|
8
|
+
*
|
|
9
|
+
* @module @bluelovers/fs-json
|
|
10
|
+
* @author bluelovers
|
|
11
|
+
*/
|
|
2
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
13
|
exports.readJSONSync = exports.readJSON = void 0;
|
|
4
14
|
exports._handleWriteOptions = _handleWriteOptions;
|
|
@@ -12,32 +22,145 @@ const fs_extra_1 = require("fs-extra");
|
|
|
12
22
|
Object.defineProperty(exports, "readJSON", { enumerable: true, get: function () { return fs_extra_1.readJSON; } });
|
|
13
23
|
Object.defineProperty(exports, "readJSONSync", { enumerable: true, get: function () { return fs_extra_1.readJSONSync; } });
|
|
14
24
|
const utils_1 = require("jsonfile/utils");
|
|
25
|
+
/**
|
|
26
|
+
* 處理寫入選項
|
|
27
|
+
* Handle write options
|
|
28
|
+
*
|
|
29
|
+
* 確保選項物件存在並設定 finalEOL 預設值為 true
|
|
30
|
+
* Ensures options object exists and sets finalEOL default to true
|
|
31
|
+
*
|
|
32
|
+
* @param {IWriteOptions} [options] - 寫入選項 / Write options
|
|
33
|
+
* @returns {IWriteOptions} - 處理後的選項 / Processed options
|
|
34
|
+
* @private
|
|
35
|
+
*/
|
|
15
36
|
function _handleWriteOptions(options) {
|
|
16
37
|
var _a;
|
|
17
38
|
options !== null && options !== void 0 ? options : (options = {});
|
|
18
39
|
(_a = options.finalEOL) !== null && _a !== void 0 ? _a : (options.finalEOL = true);
|
|
19
40
|
return options;
|
|
20
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* 輸出 JSON 檔案(異步)
|
|
44
|
+
* Output JSON file (async)
|
|
45
|
+
*
|
|
46
|
+
* 寫入 JSON 檔案,如果目錄不存在則自動創建
|
|
47
|
+
* Writes JSON file, creates directory if it doesn't exist
|
|
48
|
+
*
|
|
49
|
+
* @param {string} file - 檔案路徑 / File path
|
|
50
|
+
* @param {any} data - 要寫入的資料 / Data to write
|
|
51
|
+
* @param {IWriteOptions} [options] - 寫入選項 / Write options
|
|
52
|
+
* @returns {Promise<void>} - Promise / Promise
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* await outputJSON('config.json', { key: 'value' });
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
21
59
|
function outputJSON(file, data, options) {
|
|
22
60
|
options = _handleWriteOptions(options);
|
|
23
61
|
return (0, fs_extra_1.outputJSON)(file, data, options);
|
|
24
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* 輸出 JSON 檔案(同步)
|
|
65
|
+
* Output JSON file (sync)
|
|
66
|
+
*
|
|
67
|
+
* 同步寫入 JSON 檔案,如果目錄不存在則自動創建
|
|
68
|
+
* Synchronously writes JSON file, creates directory if it doesn't exist
|
|
69
|
+
*
|
|
70
|
+
* @param {string} file - 檔案路徑 / File path
|
|
71
|
+
* @param {any} data - 要寫入的資料 / Data to write
|
|
72
|
+
* @param {IWriteOptions} [options] - 寫入選項 / Write options
|
|
73
|
+
* @returns {void}
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* outputJSONSync('config.json', { key: 'value' });
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
25
80
|
function outputJSONSync(file, data, options) {
|
|
26
81
|
options = _handleWriteOptions(options);
|
|
27
82
|
return (0, fs_extra_1.outputJSONSync)(file, data, options);
|
|
28
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* 寫入 JSON 檔案(異步)
|
|
86
|
+
* Write JSON file (async)
|
|
87
|
+
*
|
|
88
|
+
* 異步寫入 JSON 檔案
|
|
89
|
+
* Writes JSON file asynchronously
|
|
90
|
+
*
|
|
91
|
+
* @param {string} file - 檔案路徑 / File path
|
|
92
|
+
* @param {any} data - 要寫入的資料 / Data to write
|
|
93
|
+
* @param {IWriteOptions} [options] - 寫入選項 / Write options
|
|
94
|
+
* @returns {Promise<void>} - Promise / Promise
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```typescript
|
|
98
|
+
* await writeJSON('data.json', { users: [] });
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
29
101
|
function writeJSON(file, data, options) {
|
|
30
102
|
options = _handleWriteOptions(options);
|
|
31
103
|
return (0, fs_extra_1.writeJSON)(file, data, options);
|
|
32
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* 寫入 JSON 檔案(同步)
|
|
107
|
+
* Write JSON file (sync)
|
|
108
|
+
*
|
|
109
|
+
* 同步寫入 JSON 檔案
|
|
110
|
+
* Writes JSON file synchronously
|
|
111
|
+
*
|
|
112
|
+
* @param {string} file - 檔案路徑 / File path
|
|
113
|
+
* @param {any} data - 要寫入的資料 / Data to write
|
|
114
|
+
* @param {IWriteOptions} [options] - 寫入選項 / Write options
|
|
115
|
+
* @returns {void}
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* writeJSONSync('data.json', { users: [] });
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
33
122
|
function writeJSONSync(file, data, options) {
|
|
34
123
|
options = _handleWriteOptions(options);
|
|
35
124
|
return (0, fs_extra_1.writeJSONSync)(file, data, options);
|
|
36
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* 將資料字串化為 JSON
|
|
128
|
+
* Stringify data to JSON
|
|
129
|
+
*
|
|
130
|
+
* 將 JavaScript 資料轉換為 JSON 字串,支援結尾換行選項
|
|
131
|
+
* Converts JavaScript data to JSON string with final EOL support
|
|
132
|
+
*
|
|
133
|
+
* @param {any} data - 要字串化的資料 / Data to stringify
|
|
134
|
+
* @param {IWriteOptions} [options] - 字串化選項 / Stringify options
|
|
135
|
+
* @returns {string} - JSON 字串 / JSON string
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```typescript
|
|
139
|
+
* const json = stringifyJSON({ key: 'value' });
|
|
140
|
+
* console.log(json); // '{"key":"value"}\n'
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
37
143
|
function stringifyJSON(data, options) {
|
|
38
144
|
options = _handleWriteOptions(options);
|
|
39
145
|
return (0, utils_1.stringify)(data, options);
|
|
40
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* 解析 JSON 字串
|
|
149
|
+
* Parse JSON string
|
|
150
|
+
*
|
|
151
|
+
* 從字串或 Uint8Array 解析 JSON 資料
|
|
152
|
+
* Parses JSON data from string or Uint8Array
|
|
153
|
+
*
|
|
154
|
+
* @param {string | Uint8Array} stringOrUint8Array - JSON 字串或 Uint8Array / JSON string or Uint8Array
|
|
155
|
+
* @param {Function} [reviver] - 轉換函數 / Reviver function
|
|
156
|
+
* @returns {any} - 解析後的資料 / Parsed data
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* const obj = parseJSON('{"key":"value"}');
|
|
161
|
+
* const obj2 = parseJSON(new Uint8Array([123, 34, 107, 101, 121, 34, 58, 34, 118, 97, 108, 117, 101, 34, 125]));
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
41
164
|
function parseJSON(stringOrUint8Array, reviver) {
|
|
42
165
|
return JSON.parse(stringOrUint8Array.toString(), reviver);
|
|
43
166
|
}
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;AAmDH,kDAKC;AAoCD,gCAIC;AAmBD,wCAIC;AAmBD,8BAIC;AAmBD,sCAIC;AAmBD,sCAIC;AAmBD,8BAKC;AAlND,uCAQkB;AAyDjB,yFA9DA,mBAAQ,OA8DA;AAKR,6FAlEA,uBAAY,OAkEA;AA7Db,0CAA2C;AA6B3C;;;;;;;;;;GAUG;AACH,SAAgB,mBAAmB,CAAC,OAAuB;;IAE1D,OAAO,aAAP,OAAO,cAAP,OAAO,IAAP,OAAO,GAAK,EAAE,EAAC;IACf,MAAA,OAAO,CAAC,QAAQ,oCAAhB,OAAO,CAAC,QAAQ,GAAK,IAAI,EAAC;IAC1B,OAAO,OAAO,CAAA;AACf,CAAC;AAmBD;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,UAAU,CAAC,IAAY,EAAE,IAAS,EAAE,OAAuB;IAE1E,OAAO,GAAG,mBAAmB,CAAC,OAAO,CAAC,CAAC;IACvC,OAAO,IAAA,qBAAW,EAAC,IAAI,EAAE,IAAI,EAAE,OAAc,CAAC,CAAA;AAC/C,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,cAAc,CAAC,IAAY,EAAE,IAAS,EAAE,OAAuB;IAE9E,OAAO,GAAG,mBAAmB,CAAC,OAAO,CAAC,CAAC;IACvC,OAAO,IAAA,yBAAe,EAAC,IAAI,EAAE,IAAI,EAAE,OAAc,CAAC,CAAA;AACnD,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,SAAS,CAAC,IAAY,EAAE,IAAS,EAAE,OAAuB;IAEzE,OAAO,GAAG,mBAAmB,CAAC,OAAO,CAAC,CAAC;IACvC,OAAO,IAAA,oBAAU,EAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;AACvC,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,aAAa,CAAC,IAAY,EAAE,IAAS,EAAE,OAAuB;IAE7E,OAAO,GAAG,mBAAmB,CAAC,OAAO,CAAC,CAAC;IACvC,OAAO,IAAA,wBAAc,EAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;AAC3C,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,aAAa,CAAC,IAAS,EAAE,OAAuB;IAE/D,OAAO,GAAG,mBAAmB,CAAC,OAAO,CAAC,CAAC;IACvC,OAAO,IAAA,iBAAS,EAAC,IAAI,EAAE,OAAc,CAAC,CAAA;AACvC,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,SAAS,CAAC,kBAAuC,EAChE,OAAqD;IAGrD,OAAO,IAAI,CAAC,KAAK,CAAC,kBAAkB,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAC,CAAA;AAC1D,CAAC","sourcesContent":["/**\n * Enhanced JSON File Operations Module\n * 增強型 JSON 檔案操作模組\n *\n * 提供增強的 JSON 檔案讀寫功能,支援結尾換行符號(finalEOL)\n * Provides enhanced JSON file read/write operations with final EOL support\n *\n * @module @bluelovers/fs-json\n * @author bluelovers\n */\n\nimport {\n\toutputJSON as _outputJSON,\n\toutputJSONSync as _outputJSONSync,\n\treadJSON,\n\treadJSONSync,\n\twriteJSON as _writeJSON,\n\twriteJSONSync as _writeJSONSync,\n\tWriteOptions,\n} from 'fs-extra';\nimport { stringify } from 'jsonfile/utils';\nimport { JFWriteOptions } from 'jsonfile';\n\n/**\n * 擴充 fs-extra 的 WriteOptions 介面\n * Extend fs-extra WriteOptions interface\n */\ndeclare module 'fs-extra'\n{\n\tinterface WriteOptions extends Exclude<JFWriteOptions, string | null>\n\t{\n\t\t/**\n\t\t * 是否在 JSON 結尾添加換行符號\n\t\t * Whether to add final newline to JSON\n\t\t * @default true\n\t\t */\n\t\tfinalEOL?: boolean\n\t}\n}\n\n/**\n * 寫入選項類型\n * Write options type\n *\n * 結合 fs-extra 和 jsonfile 的寫入選項\n * Combines fs-extra and jsonfile write options\n */\nexport type IWriteOptions = WriteOptions & Exclude<JFWriteOptions, string | null>;\n\n/**\n * 處理寫入選項\n * Handle write options\n *\n * 確保選項物件存在並設定 finalEOL 預設值為 true\n * Ensures options object exists and sets finalEOL default to true\n *\n * @param {IWriteOptions} [options] - 寫入選項 / Write options\n * @returns {IWriteOptions} - 處理後的選項 / Processed options\n * @private\n */\nexport function _handleWriteOptions(options?: IWriteOptions): IWriteOptions\n{\n\toptions ??= {};\n\toptions.finalEOL ??= true;\n\treturn options\n}\n\n/**\n * 從 fs-extra 導出的讀取函數\n * Read functions exported from fs-extra\n */\nexport {\n\t/**\n\t * 異步讀取 JSON 檔案\n\t * Read JSON file asynchronously\n\t */\n\treadJSON,\n\t/**\n\t * 同步讀取 JSON 檔案\n\t * Read JSON file synchronously\n\t */\n\treadJSONSync,\n}\n\n/**\n * 輸出 JSON 檔案(異步)\n * Output JSON file (async)\n *\n * 寫入 JSON 檔案,如果目錄不存在則自動創建\n * Writes JSON file, creates directory if it doesn't exist\n *\n * @param {string} file - 檔案路徑 / File path\n * @param {any} data - 要寫入的資料 / Data to write\n * @param {IWriteOptions} [options] - 寫入選項 / Write options\n * @returns {Promise<void>} - Promise / Promise\n *\n * @example\n * ```typescript\n * await outputJSON('config.json', { key: 'value' });\n * ```\n */\nexport function outputJSON(file: string, data: any, options?: IWriteOptions)\n{\n\toptions = _handleWriteOptions(options);\n\treturn _outputJSON(file, data, options as any)\n}\n\n/**\n * 輸出 JSON 檔案(同步)\n * Output JSON file (sync)\n *\n * 同步寫入 JSON 檔案,如果目錄不存在則自動創建\n * Synchronously writes JSON file, creates directory if it doesn't exist\n *\n * @param {string} file - 檔案路徑 / File path\n * @param {any} data - 要寫入的資料 / Data to write\n * @param {IWriteOptions} [options] - 寫入選項 / Write options\n * @returns {void}\n *\n * @example\n * ```typescript\n * outputJSONSync('config.json', { key: 'value' });\n * ```\n */\nexport function outputJSONSync(file: string, data: any, options?: IWriteOptions)\n{\n\toptions = _handleWriteOptions(options);\n\treturn _outputJSONSync(file, data, options as any)\n}\n\n/**\n * 寫入 JSON 檔案(異步)\n * Write JSON file (async)\n *\n * 異步寫入 JSON 檔案\n * Writes JSON file asynchronously\n *\n * @param {string} file - 檔案路徑 / File path\n * @param {any} data - 要寫入的資料 / Data to write\n * @param {IWriteOptions} [options] - 寫入選項 / Write options\n * @returns {Promise<void>} - Promise / Promise\n *\n * @example\n * ```typescript\n * await writeJSON('data.json', { users: [] });\n * ```\n */\nexport function writeJSON(file: string, data: any, options?: IWriteOptions)\n{\n\toptions = _handleWriteOptions(options);\n\treturn _writeJSON(file, data, options)\n}\n\n/**\n * 寫入 JSON 檔案(同步)\n * Write JSON file (sync)\n *\n * 同步寫入 JSON 檔案\n * Writes JSON file synchronously\n *\n * @param {string} file - 檔案路徑 / File path\n * @param {any} data - 要寫入的資料 / Data to write\n * @param {IWriteOptions} [options] - 寫入選項 / Write options\n * @returns {void}\n *\n * @example\n * ```typescript\n * writeJSONSync('data.json', { users: [] });\n * ```\n */\nexport function writeJSONSync(file: string, data: any, options?: IWriteOptions)\n{\n\toptions = _handleWriteOptions(options);\n\treturn _writeJSONSync(file, data, options)\n}\n\n/**\n * 將資料字串化為 JSON\n * Stringify data to JSON\n *\n * 將 JavaScript 資料轉換為 JSON 字串,支援結尾換行選項\n * Converts JavaScript data to JSON string with final EOL support\n *\n * @param {any} data - 要字串化的資料 / Data to stringify\n * @param {IWriteOptions} [options] - 字串化選項 / Stringify options\n * @returns {string} - JSON 字串 / JSON string\n *\n * @example\n * ```typescript\n * const json = stringifyJSON({ key: 'value' });\n * console.log(json); // '{\"key\":\"value\"}\\n'\n * ```\n */\nexport function stringifyJSON(data: any, options?: IWriteOptions): string\n{\n\toptions = _handleWriteOptions(options);\n\treturn stringify(data, options as any)\n}\n\n/**\n * 解析 JSON 字串\n * Parse JSON string\n *\n * 從字串或 Uint8Array 解析 JSON 資料\n * Parses JSON data from string or Uint8Array\n *\n * @param {string | Uint8Array} stringOrUint8Array - JSON 字串或 Uint8Array / JSON string or Uint8Array\n * @param {Function} [reviver] - 轉換函數 / Reviver function\n * @returns {any} - 解析後的資料 / Parsed data\n *\n * @example\n * ```typescript\n * const obj = parseJSON('{\"key\":\"value\"}');\n * const obj2 = parseJSON(new Uint8Array([123, 34, 107, 101, 121, 34, 58, 34, 118, 97, 108, 117, 101, 34, 125]));\n * ```\n */\nexport function parseJSON(stringOrUint8Array: string | Uint8Array,\n\treviver?: (this: any, key: string, value: any) => any,\n): string\n{\n\treturn JSON.parse(stringOrUint8Array.toString(), reviver)\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bluelovers/fs-json",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.17",
|
|
4
4
|
"description": "Enhanced JSON file operations with final EOL support / 增強型 JSON 檔案操作,支援結尾換行符號",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"create-by-yarn-tool"
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"publishConfig": {
|
|
56
56
|
"access": "public"
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "6c690c4123ff9db0f807fbc9a05f509a832aee69"
|
|
59
59
|
}
|