Merge pull request #1213 from karalabe/polish-console-prettyprinter

jsre: patch up the pretty printer to have a decent look
This commit is contained in:
Jeffrey Wilcke 2015-06-09 07:29:32 -07:00
commit fda49f2b52

@ -26,19 +26,19 @@ function pp(object, indent) {
} else if(typeof(object) === "object") { } else if(typeof(object) === "object") {
str += "{\n"; str += "{\n";
indent += " "; indent += " ";
var last = getFields(object).pop()
getFields(object).forEach(function (k) { var fields = getFields(object);
str += indent + k + ": "; var last = fields[fields.length - 1];
fields.forEach(function (key) {
str += indent + key + ": ";
try { try {
str += pp(object[k], indent); str += pp(object[key], indent);
} catch (e) { } catch (e) {
str += pp(e, indent); str += pp(e, indent);
} }
if(key !== last) {
if(k !== last) {
str += ","; str += ",";
} }
str += "\n"; str += "\n";
}); });
str += indent.substr(2, indent.length) + "}"; str += indent.substr(2, indent.length) + "}";
@ -49,7 +49,7 @@ function pp(object, indent) {
} else if(typeof(object) === "number") { } else if(typeof(object) === "number") {
str += "\033[31m" + object; str += "\033[31m" + object;
} else if(typeof(object) === "function") { } else if(typeof(object) === "function") {
str += "\033[35m[Function]"; str += "\033[35m" + object.toString().split(" {")[0];
} else { } else {
str += object; str += object;
} }
@ -70,15 +70,32 @@ var redundantFields = [
]; ];
var getFields = function (object) { var getFields = function (object) {
var result = Object.getOwnPropertyNames(object); var members = Object.getOwnPropertyNames(object);
if (object.constructor && object.constructor.prototype) { if (object.constructor && object.constructor.prototype) {
result = result.concat(Object.getOwnPropertyNames(object.constructor.prototype)); members = members.concat(Object.getOwnPropertyNames(object.constructor.prototype));
} }
return result.filter(function (field) {
var fields = members.filter(function (member) {
return !isMemberFunction(object, member)
}).sort()
var funcs = members.filter(function (member) {
return isMemberFunction(object, member)
}).sort()
var results = fields.concat(funcs);
return results.filter(function (field) {
return redundantFields.indexOf(field) === -1; return redundantFields.indexOf(field) === -1;
}); });
}; };
var isMemberFunction = function(object, member) {
try {
return typeof(object[member]) === "function";
} catch(e) {
return false;
}
}
var isBigNumber = function (object) { var isBigNumber = function (object) {
return typeof BigNumber !== 'undefined' && object instanceof BigNumber; return typeof BigNumber !== 'undefined' && object instanceof BigNumber;
}; };