Skip to content

Instantly share code, notes, and snippets.

@focusaurus
Created March 30, 2011 03:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save focusaurus/893797 to your computer and use it in GitHub Desktop.
Save focusaurus/893797 to your computer and use it in GitHub Desktop.
CoffeeScript Introspection Function
debug = (obj, seen)->
printProps = (obj)->
#Edge case to handle is [1,2,3][9] = 'foo'
#Need to factor the conditional out to check if the prop is a number less
#than the array's length
return ((if ! /^\d+$/.test prop then prop + ": " + debug(obj[prop], seen) \
else '') for prop of obj).join(', ')
seen = seen or []
if obj in seen
return '[Circular]'
seen.push obj
switch typeof obj
when 'boolean'
return obj.toString()
when 'number'
return obj.toString()
when 'string'
return obj.toString()
when 'undefined'
return 'undefined'
when 'function'
source = obj.toString()
return source.slice(0, source.indexOf('{')) + ' {...}'
else
if Object.prototype.toString.call(obj) == Object.prototype.toString.call([])
return '['+ ((debug(item, seen) for item in obj).join(', ')) + ']' + printProps obj
else
return (obj or 'null').toString() + printProps obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment