Skip to content

Commit b3c5de1

Browse files
author
Snowflake107
committed
init 🎉
0 parents  commit b3c5de1

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

‎README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Deno JSON5
2+
**[JSON5](https://npmjs.com/package/json5)** for **[Deno](https://deno.land)**.
3+
4+
# Example
5+
6+
`main.ts`
7+
```js
8+
import JSON5 from "https://deno.land/x/json5";
9+
10+
const data = `{
11+
// comments
12+
unquoted: 'and you can quote me on that',
13+
singleQuotes: 'I can use "double quotes" here',
14+
lineBreaks: "Look, Mom! \
15+
No \\n's!",
16+
hexadecimal: 0xdecaf,
17+
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
18+
positiveSign: +1,
19+
trailingComma: 'in objects', andIn: ['arrays',
20+
],
21+
"backwardsCompatible": "with JSON",
22+
}`;
23+
console.log(JSON5.parse(data));
24+
25+
/*
26+
{
27+
unquoted: "and you can quote me on that",
28+
singleQuotes: 'I can use "double quotes" here',
29+
lineBreaks: "Look, Mom! No \\n's!",
30+
hexadecimal: 912559,
31+
leadingDecimalPoint: 0.8675309,
32+
andTrailing: 8675309,
33+
positiveSign: 1,
34+
trailingComma: "in objects",
35+
andIn: [ "arrays" ],
36+
backwardsCompatible: "with JSON"
37+
}
38+
*/
39+
```
40+
41+
# Methods
42+
- `JSON5.stringify`
43+
- `JSON5.parse`
44+
- `JSON5.require`
45+
- `JSON5.requireAsync`

‎mod.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)