feat: util.format() supports java array

This commit is contained in:
hyb1996
2018-02-24 20:42:01 +08:00
parent dfc75dab94
commit 011008df93
5 changed files with 16 additions and 10 deletions

View File

@@ -213,9 +213,8 @@ function arrayToHash(array) {
function formatValue(ctx, value, recurseTimes) {
// Provide a hook for user-specified inspect functions.
// Check that value is an object with an inspect function on it
if (ctx.customInspect &&
value &&
isFunction(value.inspect) &&
if (ctx.customInspect && value && value instanceof Object &&
("inspect" in value) && isFunction(value.inspect) &&
// Filter out the util module, it's inspect function is special
value.inspect !== exports.inspect &&
// Also filter out any prototype objects using the circular check.
@@ -227,6 +226,10 @@ function formatValue(ctx, value, recurseTimes) {
return ret;
}
if(value && isFunction(value.getClass) && value.getClass().isArray()){
return formatJavaArray(value);
}
// Primitive types cannot have properties
var primitive = formatPrimitive(ctx, value);
if (primitive) {
@@ -345,6 +348,9 @@ function formatPrimitive(ctx, value) {
return ctx.stylize('null', 'null');
}
function formatJavaArray(javaArray){
return java.util.Arrays.toString(javaArray);
}
function formatError(value) {
return '[' + Error.prototype.toString.call(value) + ']';