jsonc-simple-parser
Version:
A simple JSON parser that supports comments and optional trailing commas.
49 lines (24 loc) • 925 B
text/typescript
/* IMPORT */
import strip from './strip';
/* HELPERS */
const _parse = JSON.parse;
/* MAIN */
const parse = ( text: string, reviver?: ( this: any, key: string, value: any ) => any ): any => {
text = `${text}`; // "text" can actually be anything, but we need a string here
if ( reviver ) { // A "reviver" could have side effects, it may not be safe to call it twice
return _parse ( strip ( text ), reviver );
} else {
try { // Shortcut in case there are no comments or trailing commas
return _parse ( text );
} catch ( error ) { // Stripping out any potential comments and trailing commas and trying again
const textStripped = strip ( text );
if ( text === textStripped ) { // Parsing it again would inevitably lead to the same error
throw error;
} else {
return _parse ( textStripped );
}
}
}
};
/* EXPORT */
export default parse;