Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Created September 18, 2015 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuck0523/d87dc6be6944512068ce to your computer and use it in GitHub Desktop.
Save chuck0523/d87dc6be6944512068ce to your computer and use it in GitHub Desktop.
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