-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
util: add inspectTag template literal tag #11130
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -371,6 +371,27 @@ added: v6.6.0 | |
| A Symbol that can be used to declare custom inspect functions, see | ||
| [Custom inspection functions on Objects][]. | ||
|
|
||
| ## util.inspectTag | ||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
|
|
||
| A template literal tag that ensures each replacement in a template string is | ||
| passed through `util.inspect()`. | ||
|
|
||
| ```js | ||
| const inspectTag = require('util').inspectTag; | ||
| const obj = {a: 1}; | ||
|
|
||
| // Without the tag: | ||
| console.log(`${obj}`); | ||
| // Prints: '[object Object]' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we place these before the line of code it's referring to? To me that seems to be the traditional place for such comments. Also the indentation makes it look out of place. |
||
|
|
||
| // With the tag: | ||
| console.log(inspectTag`${obj}`); | ||
| // Prints: '{ a : 1 }' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another nit:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mind that
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. > util.inspect({a: 1})
'{ a: 1 }'It's also this case in the test that comes with it.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, sorry, don't know where this comes from :/ Could be lack of coffee |
||
| ``` | ||
|
|
||
| ## Deprecated APIs | ||
|
|
||
| The following APIs have been deprecated and should no longer be used. Existing | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| 'use strict'; | ||
|
|
||
| require('../common'); | ||
| const assert = require('assert'); | ||
| const util = require('util'); | ||
|
|
||
| const inspectTag = util.inspectTag; | ||
|
|
||
| const obj = {a: 1}; | ||
|
|
||
| assert.strictEqual(`${obj}`, '[object Object]'); | ||
| assert.strictEqual(inspectTag`${obj}`, '{ a: 1 }'); | ||
| assert.strictEqual(inspectTag`${obj}-${obj}`, '{ a: 1 }-{ a: 1 }'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth add a test when the end is a string(
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is not an empty string always in the end of a template literal?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, yes. |
||
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.
Nit: A template literal tag function