Last active
August 29, 2015 14:13
-
-
Save JosephClay/81f2a4ee080add6d9913 to your computer and use it in GitHub Desktop.
JS: typecast
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
var typecast = function(val) { | |
// Pulled from AMD | |
var r; | |
if (val === null || val === 'null') { | |
r = null; | |
} else if (val === 'true') { | |
r = true; | |
} else if (val === 'false') { | |
r = false; | |
} else if (val === undefined || val === 'undefined') { | |
r = undefined; | |
} else if (val === '' || isNaN(val)) { | |
// isNaN('') returns false | |
r = val; | |
} else { | |
// parseFloat(null || '') returns NaN | |
r = parseFloat(val); | |
} | |
return r; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment