Created
March 30, 2011 03:14
-
-
Save focusaurus/893797 to your computer and use it in GitHub Desktop.
CoffeeScript Introspection Function
This file contains hidden or 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
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