Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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