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: implement minimal console.group() #14910

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
console: add groupCollapsed()
  • Loading branch information
Trott committed Aug 22, 2017
commit c1d5bcc26746df47507880571df3e19c7406993a
8 changes: 8 additions & 0 deletions doc/api/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ Increases indentation of subsequent lines by two spaces.
If one or more `label`s are provided, those are printed first without the
additional indentation.

### console.groupCollapsed()
<!-- YAML
added: REPLACEME
-->

An alias for [`console.group()`][].

### console.groupEnd()
<!-- YAML
added: REPLACEME
Expand Down Expand Up @@ -409,6 +416,7 @@ added: v0.1.100
The `console.warn()` function is an alias for [`console.error()`][].

[`console.error()`]: #console_console_error_data_args
[`console.group()`]: #console_console_group_label
[`console.log()`]: #console_console_log_data_args
[`console.time()`]: #console_console_time_label
[`console.timeEnd()`]: #console_console_timeend_label
Expand Down
2 changes: 2 additions & 0 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ Console.prototype.group = function group(...data) {
this[groupIndent] += ' ';
};

Console.prototype.groupCollapsed = Console.prototype.group;

Console.prototype.groupEnd = function groupEnd() {
this[groupIndent] = this[groupIndent].slice(0, this[groupIndent].length - 2);
};
Expand Down
57 changes: 38 additions & 19 deletions test/parallel/test-console-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const common = require('../common');
const assert = require('assert');
const Console = require('console').Console;

let stdout, stderr;
let c, stdout, stderr;

function setup() {
stdout = '';
Expand All @@ -16,6 +16,8 @@ function setup() {
common.hijackStderr(function(data) {
stderr += data;
});

c = new Console(process.stdout, process.stderr);
}

function teardown() {
Expand All @@ -36,18 +38,18 @@ function teardown() {

const expectedErr = ' More of level 3\n';

console.log('This is the outer level');
console.group();
console.log('Level 2');
console.group();
console.log('Level 3');
console.warn('More of level 3');
console.groupEnd();
console.log('Back to level 2');
console.groupEnd();
console.log('Back to the outer level');
console.groupEnd();
console.log('Still at the outer level');
c.log('This is the outer level');
c.group();
c.log('Level 2');
c.group();
c.log('Level 3');
c.warn('More of level 3');
c.groupEnd();
c.log('Back to level 2');
c.groupEnd();
c.log('Back to the outer level');
c.groupEnd();
c.log('Still at the outer level');

assert.strictEqual(stdout, expectedOut);
assert.strictEqual(stderr, expectedErr);
Expand All @@ -63,12 +65,11 @@ function teardown() {
'But the second one does not\n';
const expectedErr = '';

const c1 = new Console(process.stdout, process.stderr);
const c2 = new Console(process.stdout, process.stderr);
c1.log('No indentation');
c.log('No indentation');
c2.log('None here either');
c1.group();
c1.log('Now the first console is indenting');
c.group();
c.log('Now the first console is indenting');
c2.log('But the second one does not');

assert.strictEqual(stdout, expectedOut);
Expand All @@ -83,8 +84,26 @@ function teardown() {
' And this is the data for that label\n';
const expectedErr = '';

console.group('This is a label');
console.log('And this is the data for that label');
c.group('This is a label');
c.log('And this is the data for that label');

assert.strictEqual(stdout, expectedOut);
assert.strictEqual(stderr, expectedErr);
teardown();
}

// Check that console.groupCollapsed() is an alias of console.group()
{
setup();
const expectedOut = 'Label\n' +
' Level 2\n' +
' Level 3\n';
const expectedErr = '';

c.groupCollapsed('Label');
c.log('Level 2');
c.groupCollapsed();
c.log('Level 3');

assert.strictEqual(stdout, expectedOut);
assert.strictEqual(stderr, expectedErr);
Expand Down