util: use template strings
This commit changes string manipulation in favor of template literals in the `util` module. PR-URL: https://github.com/nodejs/node/pull/9120 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
parent
3dfc12793f
commit
1ee36f614c
52
lib/util.js
52
lib/util.js
@ -144,7 +144,7 @@ exports.debuglog = function(set) {
|
|||||||
debugEnviron = process.env.NODE_DEBUG || '';
|
debugEnviron = process.env.NODE_DEBUG || '';
|
||||||
set = set.toUpperCase();
|
set = set.toUpperCase();
|
||||||
if (!debugs[set]) {
|
if (!debugs[set]) {
|
||||||
if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
|
if (new RegExp(`\\b${set}\\b`, 'i').test(debugEnviron)) {
|
||||||
var pid = process.pid;
|
var pid = process.pid;
|
||||||
debugs[set] = function() {
|
debugs[set] = function() {
|
||||||
var msg = exports.format.apply(exports, arguments);
|
var msg = exports.format.apply(exports, arguments);
|
||||||
@ -239,8 +239,8 @@ function stylizeWithColor(str, styleType) {
|
|||||||
var style = inspect.styles[styleType];
|
var style = inspect.styles[styleType];
|
||||||
|
|
||||||
if (style) {
|
if (style) {
|
||||||
return '\u001b[' + inspect.colors[style][0] + 'm' + str +
|
return `\u001b[${inspect.colors[style][0]}m${str}` +
|
||||||
'\u001b[' + inspect.colors[style][1] + 'm';
|
`\u001b[${inspect.colors[style][1]}m`;
|
||||||
} else {
|
} else {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
@ -402,8 +402,8 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
// Some type of object without properties can be shortcutted.
|
// Some type of object without properties can be shortcutted.
|
||||||
if (keys.length === 0) {
|
if (keys.length === 0) {
|
||||||
if (typeof value === 'function') {
|
if (typeof value === 'function') {
|
||||||
var name = value.name ? ': ' + value.name : '';
|
return ctx.stylize(`[Function${value.name ? `: ${value.name}` : ''}]`,
|
||||||
return ctx.stylize('[Function' + name + ']', 'special');
|
'special');
|
||||||
}
|
}
|
||||||
if (isRegExp(value)) {
|
if (isRegExp(value)) {
|
||||||
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
|
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
|
||||||
@ -421,19 +421,19 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
// now check the `raw` value to handle boxed primitives
|
// now check the `raw` value to handle boxed primitives
|
||||||
if (typeof raw === 'string') {
|
if (typeof raw === 'string') {
|
||||||
formatted = formatPrimitiveNoColor(ctx, raw);
|
formatted = formatPrimitiveNoColor(ctx, raw);
|
||||||
return ctx.stylize('[String: ' + formatted + ']', 'string');
|
return ctx.stylize(`[String: ${formatted}]`, 'string');
|
||||||
}
|
}
|
||||||
if (typeof raw === 'symbol') {
|
if (typeof raw === 'symbol') {
|
||||||
formatted = formatPrimitiveNoColor(ctx, raw);
|
formatted = formatPrimitiveNoColor(ctx, raw);
|
||||||
return ctx.stylize('[Symbol: ' + formatted + ']', 'symbol');
|
return ctx.stylize(`[Symbol: ${formatted}]`, 'symbol');
|
||||||
}
|
}
|
||||||
if (typeof raw === 'number') {
|
if (typeof raw === 'number') {
|
||||||
formatted = formatPrimitiveNoColor(ctx, raw);
|
formatted = formatPrimitiveNoColor(ctx, raw);
|
||||||
return ctx.stylize('[Number: ' + formatted + ']', 'number');
|
return ctx.stylize(`[Number: ${formatted}]`, 'number');
|
||||||
}
|
}
|
||||||
if (typeof raw === 'boolean') {
|
if (typeof raw === 'boolean') {
|
||||||
formatted = formatPrimitiveNoColor(ctx, raw);
|
formatted = formatPrimitiveNoColor(ctx, raw);
|
||||||
return ctx.stylize('[Boolean: ' + formatted + ']', 'boolean');
|
return ctx.stylize(`[Boolean: ${formatted}]`, 'boolean');
|
||||||
}
|
}
|
||||||
// Fast path for ArrayBuffer and SharedArrayBuffer.
|
// Fast path for ArrayBuffer and SharedArrayBuffer.
|
||||||
// Can't do the same for DataView because it has a non-primitive
|
// Can't do the same for DataView because it has a non-primitive
|
||||||
@ -535,8 +535,7 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
|
|
||||||
// Make functions say that they are functions
|
// Make functions say that they are functions
|
||||||
if (typeof value === 'function') {
|
if (typeof value === 'function') {
|
||||||
var n = value.name ? ': ' + value.name : '';
|
base = ` [Function${value.name ? `: ${value.name}` : ''}]`;
|
||||||
base = ' [Function' + n + ']';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make RegExps say that they are RegExps
|
// Make RegExps say that they are RegExps
|
||||||
@ -557,24 +556,24 @@ function formatValue(ctx, value, recurseTimes) {
|
|||||||
// Make boxed primitive Strings look like such
|
// Make boxed primitive Strings look like such
|
||||||
if (typeof raw === 'string') {
|
if (typeof raw === 'string') {
|
||||||
formatted = formatPrimitiveNoColor(ctx, raw);
|
formatted = formatPrimitiveNoColor(ctx, raw);
|
||||||
base = ' ' + '[String: ' + formatted + ']';
|
base = ` [String: ${formatted}]`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make boxed primitive Numbers look like such
|
// Make boxed primitive Numbers look like such
|
||||||
if (typeof raw === 'number') {
|
if (typeof raw === 'number') {
|
||||||
formatted = formatPrimitiveNoColor(ctx, raw);
|
formatted = formatPrimitiveNoColor(ctx, raw);
|
||||||
base = ' ' + '[Number: ' + formatted + ']';
|
base = ` [Number: ${formatted}]`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make boxed primitive Booleans look like such
|
// Make boxed primitive Booleans look like such
|
||||||
if (typeof raw === 'boolean') {
|
if (typeof raw === 'boolean') {
|
||||||
formatted = formatPrimitiveNoColor(ctx, raw);
|
formatted = formatPrimitiveNoColor(ctx, raw);
|
||||||
base = ' ' + '[Boolean: ' + formatted + ']';
|
base = ` [Boolean: ${formatted}]`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add constructor name if available
|
// Add constructor name if available
|
||||||
if (base === '' && constructor)
|
if (base === '' && constructor)
|
||||||
braces[0] = constructor.name + ' ' + braces[0];
|
braces[0] = `${constructor.name} ${braces[0]}`;
|
||||||
|
|
||||||
if (empty === true) {
|
if (empty === true) {
|
||||||
return braces[0] + base + braces[1];
|
return braces[0] + base + braces[1];
|
||||||
@ -646,7 +645,7 @@ function formatPrimitiveNoColor(ctx, value) {
|
|||||||
|
|
||||||
|
|
||||||
function formatError(value) {
|
function formatError(value) {
|
||||||
return value.stack || '[' + Error.prototype.toString.call(value) + ']';
|
return value.stack || `[${Error.prototype.toString.call(value)}]`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -782,9 +781,9 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
|
|||||||
}
|
}
|
||||||
if (!hasOwnProperty(visibleKeys, key)) {
|
if (!hasOwnProperty(visibleKeys, key)) {
|
||||||
if (typeof key === 'symbol') {
|
if (typeof key === 'symbol') {
|
||||||
name = '[' + ctx.stylize(key.toString(), 'symbol') + ']';
|
name = `[${ctx.stylize(key.toString(), 'symbol')}]`;
|
||||||
} else {
|
} else {
|
||||||
name = '[' + key + ']';
|
name = `[${key}]`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!str) {
|
if (!str) {
|
||||||
@ -822,7 +821,7 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return name + ': ' + str;
|
return `${name}: ${str}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -837,13 +836,10 @@ function reduceToSingleString(output, base, braces, breakLength) {
|
|||||||
// we need to force the first item to be on the next line or the
|
// we need to force the first item to be on the next line or the
|
||||||
// items will not line up correctly.
|
// items will not line up correctly.
|
||||||
(base === '' && braces[0].length === 1 ? '' : base + '\n ') +
|
(base === '' && braces[0].length === 1 ? '' : base + '\n ') +
|
||||||
' ' +
|
` ${output.join(',\n ')} ${braces[1]}`;
|
||||||
output.join(',\n ') +
|
|
||||||
' ' +
|
|
||||||
braces[1];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
|
return `${braces[0]}${base} ${output.join(', ')} ${braces[1]}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -1007,7 +1003,7 @@ exports.puts = internalUtil.deprecate(function() {
|
|||||||
|
|
||||||
|
|
||||||
exports.debug = internalUtil.deprecate(function(x) {
|
exports.debug = internalUtil.deprecate(function(x) {
|
||||||
process.stderr.write('DEBUG: ' + x + '\n');
|
process.stderr.write(`DEBUG: ${x}\n`);
|
||||||
}, 'util.debug is deprecated. Use console.error instead.');
|
}, 'util.debug is deprecated. Use console.error instead.');
|
||||||
|
|
||||||
|
|
||||||
@ -1020,7 +1016,7 @@ exports.error = internalUtil.deprecate(function(x) {
|
|||||||
|
|
||||||
exports._errnoException = function(err, syscall, original) {
|
exports._errnoException = function(err, syscall, original) {
|
||||||
var errname = uv.errname(err);
|
var errname = uv.errname(err);
|
||||||
var message = syscall + ' ' + errname;
|
var message = `${syscall} ${errname}`;
|
||||||
if (original)
|
if (original)
|
||||||
message += ' ' + original;
|
message += ' ' + original;
|
||||||
var e = new Error(message);
|
var e = new Error(message);
|
||||||
@ -1038,13 +1034,13 @@ exports._exceptionWithHostPort = function(err,
|
|||||||
additional) {
|
additional) {
|
||||||
var details;
|
var details;
|
||||||
if (port && port > 0) {
|
if (port && port > 0) {
|
||||||
details = address + ':' + port;
|
details = `${address}:${port}`;
|
||||||
} else {
|
} else {
|
||||||
details = address;
|
details = address;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (additional) {
|
if (additional) {
|
||||||
details += ' - Local (' + additional + ')';
|
details += ` - Local (${additional})`;
|
||||||
}
|
}
|
||||||
var ex = exports._errnoException(err, syscall, details);
|
var ex = exports._errnoException(err, syscall, details);
|
||||||
ex.address = address;
|
ex.address = address;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user