Created
November 2, 2016 08:26
-
-
Save vladiibine/d3e309d55bce2c83a44fb961bf3a4614 to your computer and use it in GitHub Desktop.
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
var print = function( o, maxLevel, level ) | |
{ | |
if ( typeof level == "undefined" ) | |
{ | |
level = 0; | |
} | |
if ( typeof maxlevel == "undefined" ) | |
{ | |
maxLevel = 0; | |
} | |
var str = ''; | |
// Remove this if you don't want the pre tag, but make sure to remove | |
// the close pre tag on the bottom as well | |
if ( level == 0 ) | |
{ | |
str = '<pre>'; // can also be <pre> | |
} | |
var levelStr = '<br>'; | |
for ( var x = 0; x < level; x++ ) | |
{ | |
levelStr += ' '; // all those spaces only work with <pre> | |
} | |
if ( maxLevel != 0 && level >= maxLevel ) | |
{ | |
str += levelStr + '...<br>'; | |
return str; | |
} | |
for ( var p in o ) | |
{ | |
switch(typeof o[p]) | |
{ | |
case 'string': | |
case 'number': // .tostring() gets automatically applied | |
case 'boolean': // ditto | |
str += levelStr + p + ': ' + o[p] + ' <br>'; | |
break; | |
case 'object': // this is where we become recursive | |
default: | |
str += levelStr + p + ': [ <br>' + print( o[p], maxLevel, level + 1 ) + levelStr + ']</br>'; | |
break; | |
} | |
} | |
// Remove this if you don't want the pre tag, but make sure to remove | |
// the open pre tag on the top as well | |
if ( level == 0 ) | |
{ | |
str += '</pre>'; // also can be </pre> | |
} | |
return str; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment