Created
August 25, 2015 05:51
-
-
Save gucheen/12b90451f04733078d7f to your computer and use it in GitHub Desktop.
Safe JSON.parse with lodash.js
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 parseLodash(str){ | |
return _.attempt(JSON.parse.bind(null, str)); | |
} |
I know this is an old gist, but could you explain why you used _.attempt(JSON.parse.bind(null, str)) instead of just _.attempt(JSON.parse, str)?
I've seen the _.attempt(JSON.parse.bind(null, str)) used elsewhere, but no explanation has been provided.
ok
@bllevy so that this
is hard bound to our preference and left at the mercy of _.attempt
or a foreign function. I think it's just a protective pattern.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice. You can use this to validate JSON: