Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.