From 3b5c6f1c39a17fad41e3f3b3da23f70f31293beb Mon Sep 17 00:00:00 2001 From: TSUYUSATO Kitsune Date: Fri, 4 May 2018 12:41:07 +0900 Subject: [PATCH] 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 Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Trivikram Kamat Reviewed-By: Ruben Bridgewater Reviewed-By: Jackson Tian Reviewed-By: James M Snell --- lib/console.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/console.js b/lib/console.js index a0158ec6643782..0f08e37fa7e22c 100644 --- a/lib/console.js +++ b/lib/console.js @@ -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) { @@ -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);