Created
September 14, 2017 09:24
-
-
Save Leopotam/6216f793982f39571546a606a3a68ea5 to your computer and use it in GitHub Desktop.
Json parsing with single- / multiline comments support.
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
const commentsRegexp = /\/(\/[^\n]*|\*(?:.|\n)*?\*\/)/g | |
/** | |
* Converts a JavaScript Object Notation (JSON) string into an object. | |
* @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 const parse = (text: string, reviver?: (key: any, value: any) => any): any => { | |
return JSON.parse(text.replace(commentsRegexp, ''), reviver) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment