Permalink
Browse files

console: improve console.group()

Preserve indentation for multiline strings, objects that span multiple
lines, etc.

also make groupIndent non-enumerable

Hide the internal `groupIndent` key a bit by making it non-enumerable
and non-configurable.

PR-URL: #14999
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
  • Loading branch information...
Trott authored and MylesBorins committed Aug 24, 2017
1 parent a46e59d commit 3a68b0bb98dc9a9e32d056a212816da83f7203da
Showing with 56 additions and 7 deletions.
  1. +20 −7 lib/console.js
  2. +36 −0 test/parallel/test-console-group.js
View
@@ -59,6 +59,8 @@ function Console(stdout, stderr, ignoreErrors = true) {
Object.defineProperty(this, '_stderrErrorHandler', prop);
this[kCounts] = new Map();
Object.defineProperty(this, kGroupIndent, { writable: true });
this[kGroupIndent] = '';
// bind the prototype functions to this Console instance
@@ -85,7 +87,15 @@ function createWriteErrorHandler(stream) {
};
}
function write(ignoreErrors, stream, string, errorhandler) {
function write(ignoreErrors, stream, string, errorhandler, groupIndent) {
if (groupIndent.length !== 0) {
if (string.indexOf('\n') !== -1) {
string = string.replace(/\n/g, `\n${groupIndent}`);
}
string = groupIndent + string;
}
string += '\n';
if (!ignoreErrors) return stream.write(string);
// There may be an error occurring synchronously (e.g. for files or TTYs
@@ -114,8 +124,9 @@ function write(ignoreErrors, stream, string, errorhandler) {
Console.prototype.log = function log(...args) {
write(this._ignoreErrors,
this._stdout,
`${this[kGroupIndent]}${util.format.apply(null, args)}\n`,
this._stdoutErrorHandler);
util.format.apply(null, args),
this._stdoutErrorHandler,
this[kGroupIndent]);
};
@@ -125,8 +136,9 @@ Console.prototype.info = Console.prototype.log;
Console.prototype.warn = function warn(...args) {
write(this._ignoreErrors,
this._stderr,
`${this[kGroupIndent]}${util.format.apply(null, args)}\n`,
this._stderrErrorHandler);
util.format.apply(null, args),
this._stderrErrorHandler,
this[kGroupIndent]);
};
@@ -137,8 +149,9 @@ Console.prototype.dir = function dir(object, options) {
options = Object.assign({customInspect: false}, options);
write(this._ignoreErrors,
this._stdout,
`${this[kGroupIndent]}${util.inspect(object, options)}\n`,
this._stdoutErrorHandler);
util.inspect(object, options),
this._stdoutErrorHandler,
this[kGroupIndent]);
};
@@ -109,3 +109,39 @@ function teardown() {
assert.strictEqual(stderr, expectedErr);
teardown();
}
// Check that multiline strings and object output are indented properly.
{
setup();
const expectedOut = 'not indented\n' +
' indented\n' +
' also indented\n' +
" { also: 'a',\n" +
" multiline: 'object',\n" +
" should: 'be',\n" +
" indented: 'properly',\n" +
" kthx: 'bai' }\n";
const expectedErr = '';
c.log('not indented');
c.group();
c.log('indented\nalso indented');
c.log({ also: 'a',
multiline: 'object',
should: 'be',
indented: 'properly',
kthx: 'bai' });
assert.strictEqual(stdout, expectedOut);
assert.strictEqual(stderr, expectedErr);
teardown();
}
// Check that the kGroupIndent symbol property is not enumerable
{
const keys = Reflect.ownKeys(console)
.filter((val) => console.propertyIsEnumerable(val))
.map((val) => val.toString());
assert(!keys.includes('Symbol(groupIndent)'),
'groupIndent should not be enumerable');
}

0 comments on commit 3a68b0b

Please sign in to comment.