Last active
November 17, 2017 17:34
-
-
Save lowmess/e4edc273d4207d914b000f32e51c4697 to your computer and use it in GitHub Desktop.
Convert boolean and integer strings to literals
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
// Convert boolean and integer strings to literals | |
// convertString("true") => true | |
// convertString("12") => 12 | |
// convertString("string") => "string" | |
function convertString(str) { | |
try { | |
JSON.parse(str); | |
} catch { | |
return str; | |
} | |
return JSON.parse(str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment