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 <[email protected]>
Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Jackson Tian <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
makenowjust authored and vsemozhetbyt committed May 8, 2018
1 parent 852c459 commit 6af26b1
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 @@ -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) {
Expand All @@ -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);
Expand Down

0 comments on commit 6af26b1

Please sign in to comment.