util: introduce formatWithOptions()
Identical to `format()` except that it takes an options argument that is passed through to `inspect()`. PR-URL: https://github.com/nodejs/node/pull/19372 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
039cdebe81
commit
681c1d2f2c
@ -254,6 +254,24 @@ intended as a debugging tool. Some input values can have a significant
|
|||||||
performance overhead that can block the event loop. Use this function
|
performance overhead that can block the event loop. Use this function
|
||||||
with care and never in a hot code path.
|
with care and never in a hot code path.
|
||||||
|
|
||||||
|
## util.formatWithOptions(inspectOptions, format[, ...args])
|
||||||
|
<!-- YAML
|
||||||
|
added: REPLACEME
|
||||||
|
-->
|
||||||
|
|
||||||
|
* `inspectOptions` {Object}
|
||||||
|
* `format` {string}
|
||||||
|
|
||||||
|
This function is identical to [`util.format()`][], except in that it takes
|
||||||
|
an `inspectOptions` argument which specifies options that are passed along to
|
||||||
|
[`util.inspect()`][].
|
||||||
|
|
||||||
|
```js
|
||||||
|
util.formatWithOptions({ colors: true }, 'See object %O', { foo: 42 });
|
||||||
|
// Returns 'See object { foo: 42 }', where `42` is colored as a number
|
||||||
|
// when printed to a terminal.
|
||||||
|
```
|
||||||
|
|
||||||
## util.getSystemErrorName(err)
|
## util.getSystemErrorName(err)
|
||||||
<!-- YAML
|
<!-- YAML
|
||||||
added: v9.7.0
|
added: v9.7.0
|
||||||
@ -2054,6 +2072,7 @@ Deprecated predecessor of `console.log`.
|
|||||||
[`Set`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
|
[`Set`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
|
||||||
[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
|
[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
|
||||||
[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
|
[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
|
||||||
|
[`util.format()`]: #util_util_format_format_args
|
||||||
[`util.inspect()`]: #util_util_inspect_object_options
|
[`util.inspect()`]: #util_util_inspect_object_options
|
||||||
[`util.promisify()`]: #util_util_promisify_original
|
[`util.promisify()`]: #util_util_promisify_original
|
||||||
[`util.types.isAnyArrayBuffer()`]: #util_util_types_isanyarraybuffer_value
|
[`util.types.isAnyArrayBuffer()`]: #util_util_types_isanyarraybuffer_value
|
||||||
|
33
lib/util.js
33
lib/util.js
@ -173,23 +173,28 @@ function tryStringify(arg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function format(f) {
|
const emptyOptions = {};
|
||||||
|
function format(...args) {
|
||||||
|
return formatWithOptions(emptyOptions, ...args);
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatWithOptions(inspectOptions, f) {
|
||||||
let i, tempStr;
|
let i, tempStr;
|
||||||
if (typeof f !== 'string') {
|
if (typeof f !== 'string') {
|
||||||
if (arguments.length === 0) return '';
|
if (arguments.length === 1) return '';
|
||||||
let res = '';
|
let res = '';
|
||||||
for (i = 0; i < arguments.length - 1; i++) {
|
for (i = 1; i < arguments.length - 1; i++) {
|
||||||
res += inspect(arguments[i]);
|
res += inspect(arguments[i], inspectOptions);
|
||||||
res += ' ';
|
res += ' ';
|
||||||
}
|
}
|
||||||
res += inspect(arguments[i]);
|
res += inspect(arguments[i], inspectOptions);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arguments.length === 1) return f;
|
if (arguments.length === 2) return f;
|
||||||
|
|
||||||
let str = '';
|
let str = '';
|
||||||
let a = 1;
|
let a = 2;
|
||||||
let lastPos = 0;
|
let lastPos = 0;
|
||||||
for (i = 0; i < f.length - 1; i++) {
|
for (i = 0; i < f.length - 1; i++) {
|
||||||
if (f.charCodeAt(i) === 37) { // '%'
|
if (f.charCodeAt(i) === 37) { // '%'
|
||||||
@ -206,12 +211,17 @@ function format(f) {
|
|||||||
tempStr = `${Number(arguments[a++])}`;
|
tempStr = `${Number(arguments[a++])}`;
|
||||||
break;
|
break;
|
||||||
case 79: // 'O'
|
case 79: // 'O'
|
||||||
tempStr = inspect(arguments[a++]);
|
tempStr = inspect(arguments[a++], inspectOptions);
|
||||||
break;
|
break;
|
||||||
case 111: // 'o'
|
case 111: // 'o'
|
||||||
tempStr = inspect(arguments[a++],
|
{
|
||||||
{ showHidden: true, showProxy: true });
|
const opts = Object.assign({}, inspectOptions, {
|
||||||
|
showHidden: true,
|
||||||
|
showProxy: true
|
||||||
|
});
|
||||||
|
tempStr = inspect(arguments[a++], opts);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case 105: // 'i'
|
case 105: // 'i'
|
||||||
tempStr = `${parseInt(arguments[a++])}`;
|
tempStr = `${parseInt(arguments[a++])}`;
|
||||||
break;
|
break;
|
||||||
@ -244,7 +254,7 @@ function format(f) {
|
|||||||
if ((typeof x !== 'object' && typeof x !== 'symbol') || x === null) {
|
if ((typeof x !== 'object' && typeof x !== 'symbol') || x === null) {
|
||||||
str += ` ${x}`;
|
str += ` ${x}`;
|
||||||
} else {
|
} else {
|
||||||
str += ` ${inspect(x)}`;
|
str += ` ${inspect(x, inspectOptions)}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
@ -1206,6 +1216,7 @@ module.exports = exports = {
|
|||||||
debuglog,
|
debuglog,
|
||||||
deprecate,
|
deprecate,
|
||||||
format,
|
format,
|
||||||
|
formatWithOptions,
|
||||||
getSystemErrorName,
|
getSystemErrorName,
|
||||||
inherits,
|
inherits,
|
||||||
inspect,
|
inspect,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user