Skip to content

Commit

Permalink
console: make console.table() use colored inspect
Browse files Browse the repository at this point in the history
This makes `console.table()` inspect objects with color if available
like `console.log()`.

PR-URL: #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>
  • Loading branch information
makenowjust authored and MylesBorins committed May 8, 2018
1 parent 9211def commit 3b5c6f1
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,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) {
Expand All @@ -336,6 +327,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);
Expand Down

0 comments on commit 3b5c6f1

Please sign in to comment.