@ainc/fs 0.1.13 → 0.1.14

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/README.md CHANGED
@@ -42,6 +42,7 @@ import * as fs from '@ainc/fs';
42
42
  * findFile(name: string, dir = '.', options?: Options): string[];
43
43
  * json(file: string): object | null;
44
44
  * json(file: string, data: object): object | null;
45
+ * parseJSON<T>(source: string, options?: Options): T;
45
46
  * resolveFile(file: string, options?: Options): void | string;
46
47
  * resolveAlias(options: Options): ResolvePath;
47
48
  * isAbsolutePath(source: string): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ainc/fs",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "description": "file system",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -25,7 +25,7 @@
25
25
  "author": "edonet <edonet@163.com>",
26
26
  "license": "MIT",
27
27
  "devDependencies": {
28
- "@types/jest": "^26.0.22",
29
- "@types/node": "^14.14.37"
28
+ "@types/jest": "^27.4.0",
29
+ "@types/node": "^17.0.5"
30
30
  }
31
31
  }
@@ -0,0 +1,17 @@
1
+ /**
2
+ *****************************************
3
+ * Created by edonet@163.com
4
+ * Created on 2022-01-01 20:53:11
5
+ *****************************************
6
+ */
7
+ 'use strict';
8
+
9
+
10
+ /**
11
+ *****************************************
12
+ * 移除【BOM】标识
13
+ *****************************************
14
+ */
15
+ export function stripBOM(content: string): string {
16
+ return content.charCodeAt(0) === 0xFEFF ? content.slice(1) : content;
17
+ }
@@ -100,7 +100,7 @@ function indexBlockCommentEnd(start: number, source: string): number {
100
100
  * 过滤注释
101
101
  *****************************************
102
102
  */
103
- export function normalizeJSON(content: string): string {
103
+ export function stripComments(content: string): string {
104
104
  let result = '';
105
105
  let idx = 0;
106
106
  let len = content.length;
package/src/index.ts CHANGED
@@ -25,6 +25,7 @@ export { isDirectory } from './isDirectory';
25
25
  export { indir } from './indir';
26
26
  export { json } from './json';
27
27
  export { jsonc } from './jsonc';
28
+ export { parseJSON } from './parseJSON';
28
29
  export { copy } from './copy';
29
30
  export { cpdir } from './cpdir';
30
31
  export { find } from './find';
package/src/json.ts CHANGED
@@ -15,6 +15,7 @@
15
15
  import { stat } from './stat';
16
16
  import { readFile } from './readFile';
17
17
  import { writeFile } from './writeFile';
18
+ import { stripBOM } from './helpers/stripBOM';
18
19
 
19
20
 
20
21
  /**
@@ -44,7 +45,7 @@ function json<T = Data>(...args: [string] | [string, T]): T | null {
44
45
  const stats = stat(name);
45
46
 
46
47
  if (stats && stats.isFile()) {
47
- return JSON.parse(readFile(stats.path));
48
+ return JSON.parse(stripBOM(readFile(stats.path)));
48
49
  }
49
50
  }
50
51
 
package/src/jsonc.ts CHANGED
@@ -15,7 +15,7 @@
15
15
  import { stat } from './stat';
16
16
  import { readFile } from './readFile';
17
17
  import { writeFile } from './writeFile';
18
- import { normalizeJSON } from './normalizeJSON';
18
+ import { stripComments } from './helpers/stripComments';
19
19
 
20
20
 
21
21
  /**
@@ -45,7 +45,7 @@ function jsonc<T = Data>(...args: [string] | [string, T]): T | null {
45
45
  const stats = stat(name);
46
46
 
47
47
  if (stats && stats.isFile()) {
48
- const content = normalizeJSON(readFile(stats.path));
48
+ const content = stripComments(readFile(stats.path));
49
49
 
50
50
  // 空白文件
51
51
  if (!content.trim()) {
@@ -0,0 +1,40 @@
1
+ /**
2
+ *****************************************
3
+ * Created by edonet@163.com
4
+ * Created on 2022-01-01 21:01:19
5
+ *****************************************
6
+ */
7
+ 'use strict';
8
+
9
+
10
+ /**
11
+ *****************************************
12
+ * 加载依赖
13
+ *****************************************
14
+ */
15
+ import { stripBOM } from './helpers/stripBOM';
16
+ import { stripComments } from './helpers/stripComments';
17
+
18
+
19
+ /**
20
+ *****************************************
21
+ * 选项
22
+ *****************************************
23
+ */
24
+ interface Options {
25
+ comments?: boolean;
26
+ }
27
+
28
+
29
+ /**
30
+ *****************************************
31
+ * 解析【JSON】文件
32
+ *****************************************
33
+ */
34
+ export function parseJSON<T>(content: string, options: Options = {}): T {
35
+ const normalized = options.comments ? stripComments(content) : stripBOM(content);
36
+ const source = normalized.trim();
37
+
38
+ // 解析内容
39
+ return source ? JSON.parse(source) : undefined;
40
+ }