Skip to content

Commit 678f2c2

Browse files
addaleaxjasnell
authored andcommitted
util: introduce formatWithOptions()
Identical to `format()` except that it takes an options argument that is passed through to `inspect()`. PR-URL: #19372 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent a890864 commit 678f2c2

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

doc/api/util.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,24 @@ intended as a debugging tool. Some input values can have a significant
254254
performance overhead that can block the event loop. Use this function
255255
with care and never in a hot code path.
256256

257+
## util.formatWithOptions(inspectOptions, format[, ...args])
258+
<!-- YAML
259+
added: REPLACEME
260+
-->
261+
262+
* `inspectOptions` {Object}
263+
* `format` {string}
264+
265+
This function is identical to [`util.format()`][], except in that it takes
266+
an `inspectOptions` argument which specifies options that are passed along to
267+
[`util.inspect()`][].
268+
269+
```js
270+
util.formatWithOptions({ colors: true }, 'See object %O', { foo: 42 });
271+
// Returns 'See object { foo: 42 }', where `42` is colored as a number
272+
// when printed to a terminal.
273+
```
274+
257275
## util.getSystemErrorName(err)
258276
<!-- YAML
259277
added: v9.7.0
@@ -2054,6 +2072,7 @@ Deprecated predecessor of `console.log`.
20542072
[`Set`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
20552073
[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
20562074
[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
2075+
[`util.format()`]: #util_util_format_format_args
20572076
[`util.inspect()`]: #util_util_inspect_object_options
20582077
[`util.promisify()`]: #util_util_promisify_original
20592078
[`util.types.isAnyArrayBuffer()`]: #util_util_types_isanyarraybuffer_value

lib/util.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,23 +173,28 @@ function tryStringify(arg) {
173173
}
174174
}
175175

176-
function format(f) {
176+
const emptyOptions = {};
177+
function format(...args) {
178+
return formatWithOptions(emptyOptions, ...args);
179+
}
180+
181+
function formatWithOptions(inspectOptions, f) {
177182
let i, tempStr;
178183
if (typeof f !== 'string') {
179-
if (arguments.length === 0) return '';
184+
if (arguments.length === 1) return '';
180185
let res = '';
181-
for (i = 0; i < arguments.length - 1; i++) {
182-
res += inspect(arguments[i]);
186+
for (i = 1; i < arguments.length - 1; i++) {
187+
res += inspect(arguments[i], inspectOptions);
183188
res += ' ';
184189
}
185-
res += inspect(arguments[i]);
190+
res += inspect(arguments[i], inspectOptions);
186191
return res;
187192
}
188193

189-
if (arguments.length === 1) return f;
194+
if (arguments.length === 2) return f;
190195

191196
let str = '';
192-
let a = 1;
197+
let a = 2;
193198
let lastPos = 0;
194199
for (i = 0; i < f.length - 1; i++) {
195200
if (f.charCodeAt(i) === 37) { // '%'
@@ -206,12 +211,17 @@ function format(f) {
206211
tempStr = `${Number(arguments[a++])}`;
207212
break;
208213
case 79: // 'O'
209-
tempStr = inspect(arguments[a++]);
214+
tempStr = inspect(arguments[a++], inspectOptions);
210215
break;
211216
case 111: // 'o'
212-
tempStr = inspect(arguments[a++],
213-
{ showHidden: true, showProxy: true });
217+
{
218+
const opts = Object.assign({}, inspectOptions, {
219+
showHidden: true,
220+
showProxy: true
221+
});
222+
tempStr = inspect(arguments[a++], opts);
214223
break;
224+
}
215225
case 105: // 'i'
216226
tempStr = `${parseInt(arguments[a++])}`;
217227
break;
@@ -244,7 +254,7 @@ function format(f) {
244254
if ((typeof x !== 'object' && typeof x !== 'symbol') || x === null) {
245255
str += ` ${x}`;
246256
} else {
247-
str += ` ${inspect(x)}`;
257+
str += ` ${inspect(x, inspectOptions)}`;
248258
}
249259
}
250260
return str;
@@ -1206,6 +1216,7 @@ module.exports = exports = {
12061216
debuglog,
12071217
deprecate,
12081218
format,
1219+
formatWithOptions,
12091220
getSystemErrorName,
12101221
inherits,
12111222
inspect,

0 commit comments

Comments
 (0)