console: make console.table() use colored inspect

This makes `console.table()` inspect objects with color if available
like `console.log()`.

PR-URL: https://github.com/nodejs/node/pull/20510
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Jackson Tian <shyvo1987@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
TSUYUSATO Kitsune 2018-05-04 12:41:07 +09:00 committed by Vse Mozhet Byt
parent 852c4592b1
commit 6af26b11b7

View File

@ -319,15 +319,6 @@ const iterKey = '(iteration index)';
const isArray = (v) => ArrayIsArray(v) || isTypedArray(v) || isBuffer(v);
const inspect = (v) => {
const opt = { depth: 0, maxArrayLength: 3 };
if (v !== null && typeof v === 'object' &&
!isArray(v) && ObjectKeys(v).length > 2)
opt.depth = -1;
return util.inspect(v, opt);
};
const getIndexArray = (length) => ArrayFrom({ length }, (_, i) => inspect(i));
// https://console.spec.whatwg.org/#table
Console.prototype.table = function(tabularData, properties) {
@ -340,6 +331,16 @@ Console.prototype.table = function(tabularData, properties) {
const final = (k, v) => this.log(cliTable(k, v));
const inspect = (v) => {
const opt = { depth: 0, maxArrayLength: 3 };
if (v !== null && typeof v === 'object' &&
!isArray(v) && ObjectKeys(v).length > 2)
opt.depth = -1;
Object.assign(opt, this[kGetInspectOptions](this._stdout));
return util.inspect(v, opt);
};
const getIndexArray = (length) => ArrayFrom({ length }, (_, i) => inspect(i));
const mapIter = isMapIterator(tabularData);
if (mapIter)
tabularData = previewMapIterator(tabularData);