Skip to content

Commit

Permalink
lib: remove usage of require('util')
Browse files Browse the repository at this point in the history
Remove usage of public `require('util').inspect` and
`require('util').formatWithOptions`.

PR-URL: #26777
Refs: #26546
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
dnlup authored and targos committed Mar 27, 2019
1 parent 977f5ac commit 64a9229
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ const {
} = require('internal/errors');
const { previewEntries } = internalBinding('util');
const { Buffer: { isBuffer } } = require('buffer');
const util = require('util');
const {
inspect,
formatWithOptions
} = require('internal/util/inspect');
const {
isTypedArray, isSet, isMap, isSetIterator, isMapIterator,
} = util.types;
} = require('internal/util/types');
const kCounts = Symbol('counts');

const kTraceConsoleCategory = 'node,node.console';
Expand Down Expand Up @@ -271,12 +274,12 @@ Console.prototype[kGetInspectOptions] = function(stream) {

Console.prototype[kFormatForStdout] = function(args) {
const opts = this[kGetInspectOptions](this._stdout);
return util.formatWithOptions(opts, ...args);
return formatWithOptions(opts, ...args);
};

Console.prototype[kFormatForStderr] = function(args) {
const opts = this[kGetInspectOptions](this._stderr);
return util.formatWithOptions(opts, ...args);
return formatWithOptions(opts, ...args);
};

const consoleMethods = {
Expand All @@ -290,7 +293,7 @@ const consoleMethods = {
},

dir(object, options) {
this[kWriteToConsole](kUseStdout, util.inspect(object, {
this[kWriteToConsole](kUseStdout, inspect(object, {
customInspect: false,
...this[kGetInspectOptions](this._stdout),
...options
Expand Down Expand Up @@ -404,7 +407,7 @@ const consoleMethods = {
if (cliTable === undefined) cliTable = require('internal/cli_table');
const final = (k, v) => this.log(cliTable(k, v));

const inspect = (v) => {
const _inspect = (v) => {
const depth = v !== null &&
typeof v === 'object' &&
!isArray(v) &&
Expand All @@ -414,10 +417,10 @@ const consoleMethods = {
maxArrayLength: 3,
...this[kGetInspectOptions](this._stdout)
};
return util.inspect(v, opt);
return inspect(v, opt);
};
const getIndexArray = (length) => ArrayFrom(
{ length }, (_, i) => inspect(i));
{ length }, (_, i) => _inspect(i));

const mapIter = isMapIterator(tabularData);
let isKeyValue = false;
Expand All @@ -434,14 +437,14 @@ const consoleMethods = {
let length = 0;
if (mapIter) {
for (; i < tabularData.length / 2; ++i) {
keys.push(inspect(tabularData[i * 2]));
values.push(inspect(tabularData[i * 2 + 1]));
keys.push(_inspect(tabularData[i * 2]));
values.push(_inspect(tabularData[i * 2 + 1]));
length++;
}
} else {
for (const [k, v] of tabularData) {
keys.push(inspect(k));
values.push(inspect(v));
keys.push(_inspect(k));
values.push(_inspect(v));
length++;
}
}
Expand All @@ -463,7 +466,7 @@ const consoleMethods = {
const values = [];
let length = 0;
for (const v of tabularData) {
values.push(inspect(v));
values.push(_inspect(v));
length++;
}
return final([iterKey, valuesKey], [getIndexArray(length), values]);
Expand All @@ -480,7 +483,7 @@ const consoleMethods = {
(typeof item !== 'function' && typeof item !== 'object');
if (properties === undefined && primitive) {
hasPrimitives = true;
valuesKeyArray[i] = inspect(item);
valuesKeyArray[i] = _inspect(item);
} else {
const keys = properties || ObjectKeys(item);
for (const key of keys) {
Expand All @@ -489,7 +492,7 @@ const consoleMethods = {
if ((primitive && properties) || !hasOwnProperty(item, key))
map[key][i] = '';
else
map[key][i] = item == null ? item : inspect(item[key]);
map[key][i] = item == null ? item : _inspect(item[key]);
}
}
}
Expand Down

0 comments on commit 64a9229

Please sign in to comment.