Skip to content

Commit a2b15a4

Browse files
committed
refactor(node-fs): separate files
1 parent 186ba09 commit a2b15a4

File tree

7 files changed

+475
-460
lines changed

7 files changed

+475
-460
lines changed

packages/node-fs/src/json.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {logger} from './logger';
2+
3+
/**
4+
* Parse json string.
5+
*
6+
* @param content - json string
7+
* @returns json object
8+
* @example
9+
* ```typescript
10+
* const json = parseJson('{"a":1,"b":2}');
11+
* console.log(json.a); // 1
12+
* ```
13+
*/
14+
export function parseJson(content: string) {
15+
try {
16+
return JSON.parse(content);
17+
}
18+
catch (err) {
19+
logger.error('parseJson', 'invalid_json', err);
20+
throw new Error('invalid_json', {cause: (err as Error).cause});
21+
}
22+
}
23+
24+
/**
25+
* Stringify json object.
26+
*
27+
* @param data - json object
28+
* @returns json string
29+
* @example
30+
* ```typescript
31+
* const json = jsonStringify({a:1, b:2});
32+
* console.log(json); // '{"a":1,"b":2}'
33+
* ```
34+
*/
35+
export function jsonStringify(data: unknown): string {
36+
try {
37+
return JSON.stringify(data);
38+
}
39+
catch (err) {
40+
logger.error('jsonStringify', 'stringify_failed', err);
41+
throw new Error('stringify_failed', {cause: (err as Error).cause});
42+
}
43+
}

packages/node-fs/src/logger.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {definePackage} from '@alwatr/logger';
2+
3+
import type {} from '@alwatr/nano-build';
4+
5+
export const logger = definePackage('@alwatr/node-fs', __package_version__);

0 commit comments

Comments
 (0)