Skip to content

Instantly share code, notes, and snippets.

@dmi3y
Last active February 22, 2017 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmi3y/11237014 to your computer and use it in GitHub Desktop.
Save dmi3y/11237014 to your computer and use it in GitHub Desktop.
eval-ish strip comments from js hash string to valid js object `ala unstrict JSON.parse()`, useful for json configuration files where you want to enable comments.
function hashStr2JSON (str) {
try {
return Function('return ' + str)();
} catch(e) {
// something goes wild
throw new Error('Could not parse hash because of: ' + e.toString())
}
}
var jsonfromhash = hashStr2JSON('{a:/*comment*/"b"}//comment'); // read hash string, it will also work with multiline files
@hronro
Copy link

hronro commented Feb 22, 2017

This may cause XSS attack, is there any more safely way ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment