1
+ import JSON5_MOD from "https://cdn.skypack.dev/json5" ;
2
+
3
+ /**
4
+ * Converts a JSON5 string into an object.
5
+ * @template T Type of return value.
6
+ * @param text A valid JSON string.
7
+ * @param reviver A function that transforms the results. This function is called for each member of the object. If a member contains nested objects, the nested objects are transformed before the parent object is.
8
+ */
9
+ export function parse < T = any > ( text : string , reviver ?: ( ( this : any , key : string , value : any ) => any | null ) ) : T {
10
+ return JSON5_MOD . parse ( text , reviver ) ;
11
+ }
12
+
13
+ /**
14
+ * Converts a JavaScript value to a JSON5 string.
15
+ * @param value A JavaScript value, usually an object or array, to be converted.
16
+ * @param replacer A function that transforms the results.
17
+ * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
18
+ */
19
+ export function stringify ( value : any , replacer ?: ( ( this : any , key : string , value : any ) => any ) | null , space ?: string | number | undefined ) : string {
20
+ // @ts -ignore
21
+ return JSON5_MOD . stringify ( value , replacer , parseInt ( space ) || undefined ) ;
22
+ }
23
+
24
+ /**
25
+ * Loads JSON5 from file synchronously
26
+ * @param path File path or url
27
+ */
28
+ export function require ( path : string | URL ) : any {
29
+ const data = Deno . readFileSync ( path ) ;
30
+ const decoder = new TextDecoder ( "utf8" ) ;
31
+ const raw = decoder . decode ( data ) ;
32
+
33
+ return JSON5_MOD . parse ( raw , null ) ;
34
+ }
35
+
36
+ /**
37
+ * Loads JSON5 from file asynchronously
38
+ * @param path File path or url
39
+ */
40
+ export async function requireAsync ( path : string | URL ) : Promise < any > {
41
+ const data = await Deno . readFileSync ( path ) ;
42
+ const decoder = new TextDecoder ( "utf8" ) ;
43
+ const raw = decoder . decode ( data ) ;
44
+
45
+ return JSON5_MOD . parse ( raw , null ) ;
46
+ }
47
+
48
+ // defaults
49
+ const JSON5 = { parse, stringify, require, requireAsync } ;
50
+
51
+ export default JSON5 ;
0 commit comments