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
Next Next commit
util: code cleanup
Remove some dead code plus some minor refactoring for readability.
The constructor can not be an empty string anymore, so just remove
that check.
  • Loading branch information
BridgeAR committed Dec 28, 2018
commit e73a36cbfad472ba17065ecd3c1c49c814a70648
70 changes: 30 additions & 40 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,34 +209,34 @@ Object.defineProperty(inspect, 'defaultOptions', {

// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
inspect.colors = Object.assign(Object.create(null), {
'bold': [1, 22],
'italic': [3, 23],
'underline': [4, 24],
'inverse': [7, 27],
'white': [37, 39],
'grey': [90, 39],
'black': [30, 39],
'blue': [34, 39],
'cyan': [36, 39],
'green': [32, 39],
'magenta': [35, 39],
'red': [31, 39],
'yellow': [33, 39]
bold: [1, 22],
italic: [3, 23],
underline: [4, 24],
inverse: [7, 27],
white: [37, 39],
grey: [90, 39],
black: [30, 39],
blue: [34, 39],
cyan: [36, 39],
green: [32, 39],
magenta: [35, 39],
red: [31, 39],
yellow: [33, 39]
});

// Don't use 'blue' not visible on cmd.exe
inspect.styles = Object.assign(Object.create(null), {
'special': 'cyan',
'number': 'yellow',
'bigint': 'yellow',
'boolean': 'yellow',
'undefined': 'grey',
'null': 'bold',
'string': 'green',
'symbol': 'green',
'date': 'magenta',
special: 'cyan',
number: 'yellow',
bigint: 'yellow',
boolean: 'yellow',
undefined: 'grey',
null: 'bold',
string: 'green',
symbol: 'green',
date: 'magenta',
// "name": intentionally not styling
'regexp': 'red'
regexp: 'red'
});

function addQuotes(str, quotes) {
Expand Down Expand Up @@ -356,14 +356,10 @@ function getPrefix(constructor, tag, fallback) {
return `[${fallback}: null prototype] `;
}

if (constructor !== '') {
if (tag !== '' && constructor !== tag) {
return `${constructor} [${tag}] `;
}
return `${constructor} `;
if (tag !== '' && constructor !== tag) {
return `${constructor} [${tag}] `;
}

return '';
return `${constructor} `;
}

const getBoxedValue = formatPrimitive.bind(null, stylizeNoColor);
Expand Down Expand Up @@ -617,16 +613,12 @@ function formatRaw(ctx, value, recurseTimes) {
braces = ['{', '}'];
if (constructor === 'Object') {
if (isArgumentsObject(value)) {
if (keys.length === 0)
return '[Arguments] {}';
braces[0] = '[Arguments] {';
} else if (tag !== '') {
braces[0] = `${getPrefix(constructor, tag, 'Object')}{`;
if (keys.length === 0) {
return `${braces[0]}}`;
}
} else if (keys.length === 0) {
return '{}';
}
if (keys.length === 0) {
return `${braces[0]}}`;
}
} else if (typeof value === 'function') {
const type = constructor || tag || 'Function';
Expand Down Expand Up @@ -811,9 +803,7 @@ function handleMaxCallStackSize(ctx, err, constructor, tag, indentationLvl) {

function formatNumber(fn, value) {
// Format -0 as '-0'. Checking `value === -0` won't distinguish 0 from -0.
if (Object.is(value, -0))
return fn('-0', 'number');
return fn(`${value}`, 'number');
return fn(Object.is(value, -0) ? '-0' : `${value}`, 'number');
}

function formatBigInt(fn, value) {
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1791,7 +1791,6 @@ assert.strictEqual(
util.inspect(new StorageObject()),
'<[Object: null prototype] {}> {}'
);

}

// Check that the fallback always works.
Expand Down