Cuma, Ağustos 08, 2008

Javascript object dump

function str_repeat(str, repeat) {
var output = '';

for (var i = 0; i < repeat; i++) {
output += str;
}

return output;
}

var MAX_DEPTH = 10;

function dump(obj, indent, depth) {
var ws = '    ';
var output = '';
indent = (!indent) ? 0 : indent;
depth = (!depth) ? 0 : depth;

if (depth > MAX_DEPTH) {
return str_repeat(ws, indent) + '*Maximum Depth Reached*\n';
}

if (typeof(obj) == "object") {
output += (indent == 0) ? typeof(obj) + '\n(\n' : '';
indent++;
var child = '';

for (var key in obj) {
try {child = obj[key];}
catch (e) {child = '*Unable To Evaluate*';}

output += str_repeat(ws, indent) + '['+key+'] => ';

if (typeof(child) == "object") {
indent++;
output += typeof(child) + '\n';
output += str_repeat(ws, indent) + '(\n';
output += print_r(child, indent, depth+1);
output += str_repeat(ws, indent) + ')\n';
indent--;
}

else {
output += child + '\n';
}
}

indent--;
output += (indent == 0) ? ')\n' : '';
return output;
}else {
return str_repeat(ws, indent) + obj + '\n';
}
}

Hiç yorum yok: