Skip to content

Commit f18f3b6

Browse files
a0viedoMyles Borins
authored and
Myles Borins
committed
util: use template strings
This commit changes string manipulation in favor of template literals in the `util` module. PR-URL: #9120 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 533ce48 commit f18f3b6

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

lib/util.js

+24-28
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ exports.debuglog = function(set) {
9696
debugEnviron = process.env.NODE_DEBUG || '';
9797
set = set.toUpperCase();
9898
if (!debugs[set]) {
99-
if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
99+
if (new RegExp(`\\b${set}\\b`, 'i').test(debugEnviron)) {
100100
var pid = process.pid;
101101
debugs[set] = function() {
102102
var msg = exports.format.apply(exports, arguments);
@@ -181,8 +181,8 @@ function stylizeWithColor(str, styleType) {
181181
var style = inspect.styles[styleType];
182182

183183
if (style) {
184-
return '\u001b[' + inspect.colors[style][0] + 'm' + str +
185-
'\u001b[' + inspect.colors[style][1] + 'm';
184+
return `\u001b[${inspect.colors[style][0]}m${str}` +
185+
`\u001b[${inspect.colors[style][1]}m`;
186186
} else {
187187
return str;
188188
}
@@ -297,8 +297,8 @@ function formatValue(ctx, value, recurseTimes) {
297297
// Some type of object without properties can be shortcutted.
298298
if (keys.length === 0) {
299299
if (typeof value === 'function') {
300-
var name = value.name ? ': ' + value.name : '';
301-
return ctx.stylize('[Function' + name + ']', 'special');
300+
return ctx.stylize(`[Function${value.name ? `: ${value.name}` : ''}]`,
301+
'special');
302302
}
303303
if (isRegExp(value)) {
304304
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
@@ -312,19 +312,19 @@ function formatValue(ctx, value, recurseTimes) {
312312
// now check the `raw` value to handle boxed primitives
313313
if (typeof raw === 'string') {
314314
formatted = formatPrimitiveNoColor(ctx, raw);
315-
return ctx.stylize('[String: ' + formatted + ']', 'string');
315+
return ctx.stylize(`[String: ${formatted}]`, 'string');
316316
}
317317
if (typeof raw === 'symbol') {
318318
formatted = formatPrimitiveNoColor(ctx, raw);
319-
return ctx.stylize('[Symbol: ' + formatted + ']', 'symbol');
319+
return ctx.stylize(`[Symbol: ${formatted}]`, 'symbol');
320320
}
321321
if (typeof raw === 'number') {
322322
formatted = formatPrimitiveNoColor(ctx, raw);
323-
return ctx.stylize('[Number: ' + formatted + ']', 'number');
323+
return ctx.stylize(`[Number: ${formatted}]`, 'number');
324324
}
325325
if (typeof raw === 'boolean') {
326326
formatted = formatPrimitiveNoColor(ctx, raw);
327-
return ctx.stylize('[Boolean: ' + formatted + ']', 'boolean');
327+
return ctx.stylize(`[Boolean: ${formatted}]`, 'boolean');
328328
}
329329
}
330330

@@ -390,8 +390,7 @@ function formatValue(ctx, value, recurseTimes) {
390390

391391
// Make functions say that they are functions
392392
if (typeof value === 'function') {
393-
var n = value.name ? ': ' + value.name : '';
394-
base = ' [Function' + n + ']';
393+
base = ` [Function${value.name ? `: ${value.name}` : ''}]`;
395394
}
396395

397396
// Make RegExps say that they are RegExps
@@ -412,24 +411,24 @@ function formatValue(ctx, value, recurseTimes) {
412411
// Make boxed primitive Strings look like such
413412
if (typeof raw === 'string') {
414413
formatted = formatPrimitiveNoColor(ctx, raw);
415-
base = ' ' + '[String: ' + formatted + ']';
414+
base = ` [String: ${formatted}]`;
416415
}
417416

418417
// Make boxed primitive Numbers look like such
419418
if (typeof raw === 'number') {
420419
formatted = formatPrimitiveNoColor(ctx, raw);
421-
base = ' ' + '[Number: ' + formatted + ']';
420+
base = ` [Number: ${formatted}]`;
422421
}
423422

424423
// Make boxed primitive Booleans look like such
425424
if (typeof raw === 'boolean') {
426425
formatted = formatPrimitiveNoColor(ctx, raw);
427-
base = ' ' + '[Boolean: ' + formatted + ']';
426+
base = ` [Boolean: ${formatted}]`;
428427
}
429428

430429
// Add constructor name if available
431430
if (base === '' && constructor)
432-
braces[0] = constructor.name + ' ' + braces[0];
431+
braces[0] = `${constructor.name} ${braces[0]}`;
433432

434433
if (empty === true) {
435434
return braces[0] + base + braces[1];
@@ -497,7 +496,7 @@ function formatPrimitiveNoColor(ctx, value) {
497496

498497

499498
function formatError(value) {
500-
return '[' + Error.prototype.toString.call(value) + ']';
499+
return `[${Error.prototype.toString.call(value)}]`;
501500
}
502501

503502

@@ -609,9 +608,9 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
609608
}
610609
if (!hasOwnProperty(visibleKeys, key)) {
611610
if (typeof key === 'symbol') {
612-
name = '[' + ctx.stylize(key.toString(), 'symbol') + ']';
611+
name = `[${ctx.stylize(key.toString(), 'symbol')}]`;
613612
} else {
614-
name = '[' + key + ']';
613+
name = `[${key}]`;
615614
}
616615
}
617616
if (!str) {
@@ -649,7 +648,7 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
649648
}
650649
}
651650

652-
return name + ': ' + str;
651+
return `${name}: ${str}`;
653652
}
654653

655654

@@ -664,13 +663,10 @@ function reduceToSingleString(output, base, braces) {
664663
// we need to force the first item to be on the next line or the
665664
// items will not line up correctly.
666665
(base === '' && braces[0].length === 1 ? '' : base + '\n ') +
667-
' ' +
668-
output.join(',\n ') +
669-
' ' +
670-
braces[1];
666+
` ${output.join(',\n ')} ${braces[1]}`;
671667
}
672668

673-
return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
669+
return `${braces[0]}${base} ${output.join(', ')} ${braces[1]}`;
674670
}
675671

676672

@@ -854,7 +850,7 @@ exports.puts = internalUtil.deprecate(function() {
854850

855851

856852
exports.debug = internalUtil.deprecate(function(x) {
857-
process.stderr.write('DEBUG: ' + x + '\n');
853+
process.stderr.write(`DEBUG: ${x}\n`);
858854
}, 'util.debug is deprecated. Use console.error instead.');
859855

860856

@@ -905,7 +901,7 @@ exports.pump = internalUtil.deprecate(function(readStream, writeStream, cb) {
905901

906902
exports._errnoException = function(err, syscall, original) {
907903
var errname = uv.errname(err);
908-
var message = syscall + ' ' + errname;
904+
var message = `${syscall} ${errname}`;
909905
if (original)
910906
message += ' ' + original;
911907
var e = new Error(message);
@@ -923,13 +919,13 @@ exports._exceptionWithHostPort = function(err,
923919
additional) {
924920
var details;
925921
if (port && port > 0) {
926-
details = address + ':' + port;
922+
details = `${address}:${port}`;
927923
} else {
928924
details = address;
929925
}
930926

931927
if (additional) {
932-
details += ' - Local (' + additional + ')';
928+
details += ` - Local (${additional})`;
933929
}
934930
var ex = exports._errnoException(err, syscall, details);
935931
ex.address = address;

0 commit comments

Comments
 (0)