-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
lib: add console.{group,groupCollapsed,groupEnd}() #1727
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| var common = require('../common'); | ||
| var assert = require('assert'); | ||
|
|
||
| assert.ok(process.stdout.writable); | ||
| assert.ok(process.stderr.writable); | ||
| // Support legacy API | ||
| assert.equal('number', typeof process.stdout.fd); | ||
| assert.equal('number', typeof process.stderr.fd); | ||
|
|
||
| assert.doesNotThrow(function () { | ||
| console.group('label'); | ||
| console.groupEnd(); | ||
| }); | ||
|
|
||
| assert.doesNotThrow(function () { | ||
| console.groupCollapsed('label'); | ||
| console.groupEnd(); | ||
| }); | ||
|
|
||
| // test multiple calls to console.groupEnd() | ||
| assert.doesNotThrow(function () { | ||
| console.groupEnd(); | ||
| console.groupEnd(); | ||
| console.groupEnd(); | ||
| }); | ||
|
|
||
| var stdout_write = global.process.stdout.write; | ||
| var strings = []; | ||
| global.process.stdout.write = function(string) { | ||
| strings.push(string); | ||
| }; | ||
| console._stderr = process.stdout; | ||
|
|
||
| // test console.group(), console.groupCollapsed() and console.groupEnd() | ||
| console.log('foo'); | ||
| console.group('bar'); | ||
| console.log('foo bar'); | ||
| console.groupEnd(); | ||
| console.groupCollapsed('group 1'); | ||
| console.log('log'); | ||
| console.warn('warn'); | ||
| console.error('error'); | ||
| console.dir({ foo: true }); | ||
| console.group('group 2'); | ||
| console.log('foo bar hop'); | ||
| console.log('line 1\nline 2\nline 3'); | ||
| console.groupEnd(); | ||
| console.log('end 2'); | ||
| console.groupEnd(); | ||
| console.log('end 1'); | ||
|
|
||
| global.process.stdout.write = stdout_write; | ||
|
|
||
| assert.equal('foo\n', strings.shift()); | ||
| assert.equal('bar\n', strings.shift()); | ||
| assert.equal(' foo bar\n', strings.shift()); | ||
| assert.equal("group 1\n", strings.shift()); | ||
| assert.equal(' log\n', strings.shift()); | ||
| assert.equal(' warn\n', strings.shift()); | ||
| assert.equal(' error\n', strings.shift()); | ||
| assert.equal(' { foo: true }\n', strings.shift()); | ||
| assert.equal(' group 2\n', strings.shift()); | ||
| assert.equal(' foo bar hop\n', strings.shift()); | ||
| assert.equal(' line 1\n line 2\n line 3\n', strings.shift()); | ||
| assert.equal(' end 2\n', strings.shift()); | ||
| assert.equal('end 1\n', strings.shift()); | ||
| assert.equal(strings.length, 0); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
' '.repeat(this._group * 2)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just wanted to keep
consoleas lightweight and performant as possible.String.prototype.repeatis bit slower thanArray.joinmethod I chose.See http://jsperf.com/faster-string-repeat/15
I do agree that
' '.repeat()is more readable in code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want performance, define
this._groupas string.in
groupfunction dothis._group += ' 'in this function do:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a check that grouping is used before allocating all these arrays and strings
I also think it's better to just write the indentation, instead of concatenating it and then writing it anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This jsperf is using a shim for String.prototype.repeat. Native is faster
Anyway both show that Array#join is the slowest of all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for reviewing and providing input. I tried to be minimal and not introduce lot of new code and variables to core lib.
I'll update the PR soon based on your comments.
@3y3 For tracking multiple levels of grouping I'd still keep numeric
_groupbut generate a fixed prefix string which would be conditionally concatenated as @petkaantonov suggested. This adds variables and code, but I think it pleases everyone?