Last active
April 27, 2019 13:51
-
-
Save moimikey/398a99ac682ad6819f6b to your computer and use it in GitHub Desktop.
30 bytes to coerce "23" to Number, keep "apple" as String
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 coerce(x) { | |
* return ''+x===x?(p=~~+x?r=+x:r=x):r=x | |
* } | |
* > undefined | |
* coerce(123) | |
* > 123 | |
* coerce('123') | |
* > 123 | |
* coerce('apple') | |
* > "apple" | |
* coerce(2.343344555) | |
* > 2.343344555 | |
* coerce('2.3243499348') | |
* > 2.3243499348 | |
*/ | |
''+x===x?(p=~~+x?r=+x:r=x):r=x |
Author
moimikey
commented
Sep 10, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment