Last active
February 22, 2017 02:06
-
-
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.
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This may cause XSS attack, is there any more safely way ?