Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console: improve console.group() #14999

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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
Expand All @@ -86,7 +88,15 @@ function createWriteErrorHandler(stream) {
};
}

function write(ignoreErrors, stream, string, errorhandler) {
function write(ignoreErrors, stream, string, errorhandler, groupIndent) {
if (groupIndent.length !== 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also check for \n first by adding a indexOf guard. Using indexOf for cases were there is no newline it is a lot faster and almost negligible in case there is one.

if (groupIndent.length !== 0 && string.indexOf("\n") !== -1) {
  // ...

(Yes, indexOf is still faster then includes)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have to move the prepending of groupIndent if I did that so the result would probably be:

if (groupIndent.length !== 0) {
  if (string.indexOf('\n') !== -1 {
    string = string.replace(/\n/g, `\n${groupIndent}`);
  }
  string = groupIndent + string;
}
string += '\n';

...or...

if (groupIndent.length !== 0 && string.indexOf("\n") !== -1) {
  string = string.replace(/\n/g, `\n${groupIndent}`);
}
string = groupIndent + string + '\n';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do the latter while I would actually not replace the string again but simply place it like this

if (!ignoreErrors) return stream.write(`${groupIndent}${string}\n`);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I went with the first option in my above comment.)

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
Expand Down Expand Up @@ -115,8 +125,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]);
};


Expand All @@ -126,8 +137,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]);
};


Expand All @@ -138,8 +150,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]);
};


Expand Down
36 changes: 36 additions & 0 deletions test/parallel/test-console-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}