Created
May 20, 2015 12:43
-
-
Save nawawi/e7f813ecf227a751d15e to your computer and use it in GitHub Desktop.
dump_object
This file contains 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
function dump_object(array) { | |
var pad_char = ' ', pad_val = 2; | |
function repeat_char(len, pad_char) { | |
var str = ''; | |
for (var i = 0; i < len; i++) { | |
str += pad_char; | |
} | |
return str; | |
}; | |
function formatArray(obj, cur_depth, pad_val, pad_char) { | |
if (cur_depth > 0) { | |
cur_depth++; | |
} | |
var base_pad = repeat_char(pad_val * cur_depth, pad_char); | |
var thick_pad = repeat_char(pad_val * (cur_depth + 1), pad_char); | |
var str = ''; | |
if (typeof obj === 'object' && obj !== null && obj.constructor) { | |
str += 'Array\n' + base_pad + '(\n'; | |
for (var key in obj) { | |
str += thick_pad + '[' + key + '] => ' + formatArray(obj[key], cur_depth + 1, pad_val, pad_char) + '\n'; | |
} | |
str += base_pad + ')\n'; | |
} else if (obj === null || obj === undefined) { | |
str = ''; | |
} else { | |
str = obj.toString(); | |
if ( str.match(/function\s/) ) { | |
str = str.replace(/\n|\t|\r|\s\s\s\s/g,''); | |
} | |
} | |
return str; | |
}; | |
return formatArray(array, 0, pad_val, pad_char); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
menarik