-
-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support Deno's JSON with comments (#523)
- Loading branch information
Showing
5 changed files
with
177 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
ORIGINAL https://www.npmjs.com/package/tiny-jsonc | ||
BY Fabio Spampinato | ||
MIT license | ||
Copied due to the dependency not being compatible with CommonJS | ||
*/ | ||
|
||
import JSONC from './jsonc'; | ||
|
||
const Fixtures = { | ||
errors: { | ||
comment: '// asd', | ||
empty: '', | ||
prefix: 'invalid 123', | ||
suffix: '123 invalid', | ||
multiLineString: ` | ||
{ | ||
"foo": "/* | ||
*/" | ||
} | ||
`, | ||
}, | ||
parse: { | ||
input: ` | ||
// Example // Yes | ||
/* EXAMPLE */ /* YES */ | ||
{ | ||
"one": {}, | ||
"two" :{}, | ||
"three": { | ||
"one": null, | ||
"two" :true, | ||
"three": false, | ||
"four": "asd\\n\\u0022\\"", | ||
"five": -123.123e10, | ||
"six": [ 123, true, [],], | ||
}, | ||
} | ||
// Example // Yes | ||
/* EXAMPLE */ /* YES */ | ||
`, | ||
output: { | ||
one: {}, | ||
two: {}, | ||
three: { | ||
one: null, | ||
two: true, | ||
three: false, | ||
four: 'asd\n\u0022"', | ||
five: -123.123e10, | ||
six: [123, true, []], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
describe('Tiny JSONC', () => { | ||
it('supports strings with comments and trailing commas', () => { | ||
const { input, output } = Fixtures.parse; | ||
|
||
expect(JSONC.parse(input)).toEqual(output); | ||
}); | ||
|
||
it('throws on invalid input', () => { | ||
const { prefix, suffix } = Fixtures.errors; | ||
|
||
expect(() => JSONC.parse(prefix)).toThrow(SyntaxError); | ||
expect(() => JSONC.parse(suffix)).toThrow(SyntaxError); | ||
}); | ||
|
||
it('throws on insufficient input', () => { | ||
const { comment, empty } = Fixtures.errors; | ||
|
||
expect(() => JSONC.parse(comment)).toThrow(SyntaxError); | ||
expect(() => JSONC.parse(empty)).toThrow(SyntaxError); | ||
}); | ||
|
||
it('throws on multi-line strings', () => { | ||
const { multiLineString } = Fixtures.errors; | ||
|
||
expect(() => JSONC.parse(multiLineString)).toThrow(SyntaxError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
ORIGINAL https://www.npmjs.com/package/tiny-jsonc | ||
BY Fabio Spampinato | ||
MIT license | ||
Copied due to the dependency not being compatible with CommonJS | ||
*/ | ||
|
||
/* HELPERS */ | ||
const stringOrCommentRe = /("(?:\\?[^])*?")|(\/\/.*)|(\/\*[^]*?\*\/)/g; | ||
const stringOrTrailingCommaRe = /("(?:\\?[^])*?")|(,\s*)(?=]|})/g; | ||
|
||
/* MAIN */ | ||
const JSONC = { | ||
parse: (text: string) => { | ||
text = String(text); // To be extra safe | ||
|
||
try { | ||
// Fast path for valid JSON | ||
return JSON.parse(text); | ||
} catch { | ||
// Slow path for JSONC and invalid inputs | ||
return JSON.parse( | ||
text.replace(stringOrCommentRe, '$1').replace(stringOrTrailingCommaRe, '$1'), | ||
); | ||
} | ||
}, | ||
stringify: JSON.stringify, | ||
}; | ||
|
||
/* EXPORT */ | ||
export default JSONC; |