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.

PR-URL: https://github.com/nodejs/node/pull/25255
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Ruben Bridgewater 2018-12-28 02:28:49 +01:00 committed by Daniel Bevenius
parent eca2760ab1
commit 76fa37af75
2 changed files with 30 additions and 41 deletions

View File

@ -211,34 +211,34 @@ Object.defineProperty(inspect, 'defaultOptions', {
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
inspect.colors = Object.assign(Object.create(null), { inspect.colors = Object.assign(Object.create(null), {
'bold': [1, 22], bold: [1, 22],
'italic': [3, 23], italic: [3, 23],
'underline': [4, 24], underline: [4, 24],
'inverse': [7, 27], inverse: [7, 27],
'white': [37, 39], white: [37, 39],
'grey': [90, 39], grey: [90, 39],
'black': [30, 39], black: [30, 39],
'blue': [34, 39], blue: [34, 39],
'cyan': [36, 39], cyan: [36, 39],
'green': [32, 39], green: [32, 39],
'magenta': [35, 39], magenta: [35, 39],
'red': [31, 39], red: [31, 39],
'yellow': [33, 39] yellow: [33, 39]
}); });
// Don't use 'blue' not visible on cmd.exe // Don't use 'blue' not visible on cmd.exe
inspect.styles = Object.assign(Object.create(null), { inspect.styles = Object.assign(Object.create(null), {
'special': 'cyan', special: 'cyan',
'number': 'yellow', number: 'yellow',
'bigint': 'yellow', bigint: 'yellow',
'boolean': 'yellow', boolean: 'yellow',
'undefined': 'grey', undefined: 'grey',
'null': 'bold', null: 'bold',
'string': 'green', string: 'green',
'symbol': 'green', symbol: 'green',
'date': 'magenta', date: 'magenta',
// "name": intentionally not styling // "name": intentionally not styling
'regexp': 'red' regexp: 'red'
}); });
function addQuotes(str, quotes) { function addQuotes(str, quotes) {
@ -358,14 +358,10 @@ function getPrefix(constructor, tag, fallback) {
return `[${fallback}: null prototype] `; return `[${fallback}: null prototype] `;
} }
if (constructor !== '') { if (tag !== '' && constructor !== tag) {
if (tag !== '' && constructor !== tag) { return `${constructor} [${tag}] `;
return `${constructor} [${tag}] `;
}
return `${constructor} `;
} }
return `${constructor} `;
return '';
} }
const getBoxedValue = formatPrimitive.bind(null, stylizeNoColor); const getBoxedValue = formatPrimitive.bind(null, stylizeNoColor);
@ -619,16 +615,12 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
braces = ['{', '}']; braces = ['{', '}'];
if (constructor === 'Object') { if (constructor === 'Object') {
if (isArgumentsObject(value)) { if (isArgumentsObject(value)) {
if (keys.length === 0)
return '[Arguments] {}';
braces[0] = '[Arguments] {'; braces[0] = '[Arguments] {';
} else if (tag !== '') { } else if (tag !== '') {
braces[0] = `${getPrefix(constructor, tag, 'Object')}{`; braces[0] = `${getPrefix(constructor, tag, 'Object')}{`;
if (keys.length === 0) { }
return `${braces[0]}}`; if (keys.length === 0) {
} return `${braces[0]}}`;
} else if (keys.length === 0) {
return '{}';
} }
} else if (typeof value === 'function') { } else if (typeof value === 'function') {
const type = constructor || tag || 'Function'; const type = constructor || tag || 'Function';
@ -822,9 +814,7 @@ function handleMaxCallStackSize(ctx, err, constructor, tag, indentationLvl) {
function formatNumber(fn, value) { function formatNumber(fn, value) {
// Format -0 as '-0'. Checking `value === -0` won't distinguish 0 from -0. // Format -0 as '-0'. Checking `value === -0` won't distinguish 0 from -0.
if (Object.is(value, -0)) return fn(Object.is(value, -0) ? '-0' : `${value}`, 'number');
return fn('-0', 'number');
return fn(`${value}`, 'number');
} }
function formatBigInt(fn, value) { function formatBigInt(fn, value) {

View File

@ -1853,7 +1853,6 @@ assert.strictEqual(
util.inspect(new StorageObject()), util.inspect(new StorageObject()),
'<[Object: null prototype] {}> {}' '<[Object: null prototype] {}> {}'
); );
} }
// Check that the fallback always works. // Check that the fallback always works.