Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
nits n stuff
  • Loading branch information
monsanto committed Apr 23, 2015
commit 79627ae18edb973d0a9742341c3e301c7b0db035
27 changes: 13 additions & 14 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ function formatValue(ctx, value, recurseTimes) {
}
}

var base = '', empty, braces, formatter;
var base = '', empty = false, braces, formatter;

if (Array.isArray(value)) {
braces = ['[', ']'];
Expand All @@ -302,26 +302,25 @@ function formatValue(ctx, value, recurseTimes) {
formatter = formatSet;
} else if (value instanceof Map) {
braces = ['Map {', '}'];
// ditto
// Ditto.
if (ctx.showHidden)
keys.unshift('size');
empty = value.size === 0;
formatter = formatMap;
} else {
// Only create a mirror if the object superficially looks like a Promise
// Only create a mirror if the object superficially looks like a Promise.
var promiseInternals = value instanceof Promise && inspectPromise(value);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, is there a reason to keep the instanceof check out of inspectPromise?

if (promiseInternals) {
braces = ['Promise {', '}'];
empty = false;
formatter = formatPromise;
} else {
braces = ['{', '}'];
empty = true; // no other data than keys
empty = true; // No other data than keys.
formatter = formatObject;
}
}

empty = empty && keys.length === 0;
empty = empty === true && keys.length === 0;

// Make functions say that they are functions
if (typeof value === 'function') {
Expand Down Expand Up @@ -362,7 +361,7 @@ function formatValue(ctx, value, recurseTimes) {
base = ' ' + '[Boolean: ' + formatted + ']';
}

if (empty) {
if (empty === true) {
return braces[0] + base + braces[1];
}

Expand Down Expand Up @@ -459,8 +458,8 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
function formatSet(ctx, value, recurseTimes, visibleKeys, keys) {
var output = [];
value.forEach(function(v) {
var str = formatValue(ctx, v,
recurseTimes === null ? null : recurseTimes - 1);
var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
var str = formatValue(ctx, v, nextRecurseTimes);
output.push(str);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we should be able to do something like for (var v of value) { here instead of creating a closure.

keys.forEach(function(key) {
Expand All @@ -474,10 +473,10 @@ function formatSet(ctx, value, recurseTimes, visibleKeys, keys) {
function formatMap(ctx, value, recurseTimes, visibleKeys, keys) {
var output = [];
value.forEach(function(v, k) {
var str = formatValue(ctx, k,
recurseTimes === null ? null : recurseTimes - 1);
var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
var str = formatValue(ctx, k, nextRecurseTimes);
str += ' => ';
str += formatValue(ctx, v, recurseTimes === null ? null : recurseTimes - 1);
str += formatValue(ctx, v, nextRecurseTimes);
output.push(str);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly, for (var tuple of value) { var k = tuple[0], v = tuple[1] }

keys.forEach(function(key) {
Expand All @@ -493,8 +492,8 @@ function formatPromise(ctx, value, recurseTimes, visibleKeys, keys) {
if (internals.status === 'pending') {
output.push('<pending>');
} else {
var str = formatValue(ctx, internals.value,
recurseTimes === null ? null : recurseTimes - 1);
var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
var str = formatValue(ctx, internals.value, nextRecurseTimes);
if (internals.status === 'rejected') {
output.push('<rejected> ' + str);
} else {
Expand Down
16 changes: 9 additions & 7 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,18 @@ if (typeof Symbol !== 'undefined') {
// test Set
assert.equal(util.inspect(new Set), 'Set {}');
assert.equal(util.inspect(new Set([1, 2, 3])), 'Set { 1, 2, 3 }');
var set = new Set(["foo"]);
var set = new Set(['foo']);
set.bar = 42;
assert.equal(util.inspect(set, true), 'Set { \'foo\', [size]: 1, bar: 42 }');

// test Map
assert.equal(util.inspect(new Map), 'Map {}');
assert.equal(util.inspect(new Map([[1, 'a'], [2, 'b'], [3, 'c']])), 'Map { 1 => \'a\', 2 => \'b\', 3 => \'c\' }');
var map = new Map([["foo", null]]);
assert.equal(util.inspect(new Map([[1, 'a'], [2, 'b'], [3, 'c']])),
'Map { 1 => \'a\', 2 => \'b\', 3 => \'c\' }');
var map = new Map([['foo', null]]);
map.bar = 42;
assert.equal(util.inspect(map, true), 'Map { \'foo\' => null, [size]: 1, bar: 42 }');
assert.equal(util.inspect(map, true),
'Map { \'foo\' => null, [size]: 1, bar: 42 }');

// test Promise
assert.equal(util.inspect(Promise.resolve(3)), 'Promise { 3 }');
Expand All @@ -271,13 +273,13 @@ global.Promise = oldPromise;
// Assumes that the first numeric character is the start of an item.

function checkAlignment(container) {
var lines = util.inspect(container).split("\n");
var lines = util.inspect(container).split('\n');
var pos;
lines.forEach(function(line) {
var npos = line.search(/\d/);
if (npos !== -1) {
if (pos !== undefined)
assert.equal(pos, npos, "container items not aligned");
assert.equal(pos, npos, 'container items not aligned');
pos = npos;
}
});
Expand All @@ -289,7 +291,7 @@ for (var i = 0; i < 100; i++) {
}

checkAlignment(big_array);
checkAlignment(function(){
checkAlignment(function() {
var obj = {};
big_array.forEach(function(v) {
obj[v] = null;
Expand Down