-
Notifications
You must be signed in to change notification settings - Fork 29.1k
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
lib: add console.{group,groupCollapsed,groupEnd}() #1727
Closed
Closed
Changes from all 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 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 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 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.
Is a special case really needed here?
' '.repeat(0) === ''
.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.
Tried to avoid extra function call and use single
''
const that optimizes better than generating a new one for eachgroupEnd
.If you feel this complicates code too much, this conditional can be removed as you suggested.
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 think this is right place for this line:
And about:
generate some levels of indentation
or
trim two characters from indentation level
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.
Oh sorry I missed that where cheched
so if rewrite it with if statement:
I agree with @ChALkeR
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.
@3y3 So to summarize:
( a ? b : c )
notation and replace withif else
structures+= ' '
concatenation ingroup()
to modify_prefix
stringString.slice
ingroupEnd()
to modify_prefix
stringYou have a valid approach, but here I did not choose that. Lets give the maintainers some time to comment on this and give us direction :)
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.
Propose this as final solution for this block:
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.
No I only say, that your
( a ? b : c )
notation has unreachable part.Look at my example.
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.
Actually, It does not have unreachable part, but I do see an extra
> 0
comparison that could be left out completely for optimization. Can you please define what is the unreachable part you see.(this._group ? ' '.repeat(this._group) : '');
evaluates to''
empty string const only if_group
has just been decreased to zero. Otherwise it just regenerates the_prefix
with new string. It does not useslice
, but it matches the codegroup
uses for generating the prefix.I'm just trying to avoid avoiding unnecessary string concatenations and multiple methods in functions to generate the prefix. I chose
String.prototype.repeat
instead of string concatenation andString.prototype.slice
.