Skip to content
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
21 changes: 21 additions & 0 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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

passed through `util.inspect()`.

```js
const inspectTag = require('util').inspectTag;
const obj = {a: 1};

// Without the tag:
console.log(`${obj}`);
// Prints: '[object Object]'
Copy link
Contributor

@mscdex mscdex Feb 3, 2017

Choose a reason for hiding this comment

The 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 }'
Copy link
Member

Choose a reason for hiding this comment

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

Another nit: '{ a: 1 }'

Copy link
Member

Choose a reason for hiding this comment

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

Mind that util.inspect({a: 1}) returns '{ a : 1 }'

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down
15 changes: 15 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1054,3 +1054,18 @@ exports._exceptionWithHostPort = function(err,
// process.versions needs a custom function as some values are lazy-evaluated.
process.versions[exports.inspect.custom] =
(depth) => exports.format(JSON.parse(JSON.stringify(process.versions)));

// A template literal tag that ensures that string literal elements
// are passed through the util.format mechanism.
// Example use:
// inspectTag`This is object ${obj}`
function inspectTag(strings, ...keys) {
var ret = '';
for (var n = 0; n < strings.length; n++) {
ret += strings[n];
if (n < keys.length)
ret += exports.inspect(keys[n]);
}
return ret;
}
exports.inspectTag = inspectTag;
13 changes: 13 additions & 0 deletions test/parallel/test-util-inspecttag.js
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 }');
Copy link
Member

Choose a reason for hiding this comment

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

Worth add a test when the end is a string(n === keys.length), and a test when there are no variables in the template.

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, yes.