Created
September 18, 2015 23:20
-
-
Save chuck0523/d87dc6be6944512068ce to your computer and use it in GitHub Desktop.
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
log = (x) -> | |
console.log x | |
usage = 'Usage : \n' + | |
'1st arg : data to convert. \n ' + | |
'2nd arg : data type to convert(not necessary).\n' + | |
' ex) cnv(false,\'number\');' | |
canBeArgs = '* You can also use 2nd arg as data type.' | |
typeErr = '* Type error! number, boolean or string are valid as 2nd arg.' | |
something = 'Something happened, something' | |
hasData = (x) -> | |
x != undefined && | |
( x || x == null || x == 0 || isNaN(x) || x == '') | |
convert = (x, y) -> | |
switch y | |
when 'number' | |
parseInt x | |
when 'boolean' | |
!!x | |
when 'string' | |
x + '' | |
when undefined | |
log(canBeArgs) | |
'{ number : ' + -(-x) + | |
' , boolean : ' + !!x + | |
' , string : ' + x + '' + ' }'; | |
else | |
log(typeErr); | |
null | |
showResult = (x, y) -> | |
data = if ( x != '' ) then x else '(empty string)' | |
dataType = if ( y ) then '(' + y + ')' else '' | |
convertedData = convert(x, y) | |
convertedData != null && log(dataType + data + ' -> ' + convertedData ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment