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