Skip to content

Commit 52a04bb

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 1d54f07 commit 52a04bb

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
@@ -144,7 +144,7 @@ exports.debuglog = function(set) {
144144
debugEnviron = process.env.NODE_DEBUG || '';
145145
set = set.toUpperCase();
146146
if (!debugs[set]) {
147-
if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
147+
if (new RegExp(`\\b${set}\\b`, 'i').test(debugEnviron)) {
148148
var pid = process.pid;
149149
debugs[set] = function() {
150150
var msg = exports.format.apply(exports, arguments);
@@ -239,8 +239,8 @@ function stylizeWithColor(str, styleType) {
239239
var style = inspect.styles[styleType];
240240

241241
if (style) {
242-
return '\u001b[' + inspect.colors[style][0] + 'm' + str +
243-
'\u001b[' + inspect.colors[style][1] + 'm';
242+
return `\u001b[${inspect.colors[style][0]}m${str}` +
243+
`\u001b[${inspect.colors[style][1]}m`;
244244
} else {
245245
return str;
246246
}
@@ -402,8 +402,8 @@ function formatValue(ctx, value, recurseTimes) {
402402
// Some type of object without properties can be shortcutted.
403403
if (keys.length === 0) {
404404
if (typeof value === 'function') {
405-
var name = value.name ? ': ' + value.name : '';
406-
return ctx.stylize('[Function' + name + ']', 'special');
405+
return ctx.stylize(`[Function${value.name ? `: ${value.name}` : ''}]`,
406+
'special');
407407
}
408408
if (isRegExp(value)) {
409409
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
@@ -421,19 +421,19 @@ function formatValue(ctx, value, recurseTimes) {
421421
// now check the `raw` value to handle boxed primitives
422422
if (typeof raw === 'string') {
423423
formatted = formatPrimitiveNoColor(ctx, raw);
424-
return ctx.stylize('[String: ' + formatted + ']', 'string');
424+
return ctx.stylize(`[String: ${formatted}]`, 'string');
425425
}
426426
if (typeof raw === 'symbol') {
427427
formatted = formatPrimitiveNoColor(ctx, raw);
428-
return ctx.stylize('[Symbol: ' + formatted + ']', 'symbol');
428+
return ctx.stylize(`[Symbol: ${formatted}]`, 'symbol');
429429
}
430430
if (typeof raw === 'number') {
431431
formatted = formatPrimitiveNoColor(ctx, raw);
432-
return ctx.stylize('[Number: ' + formatted + ']', 'number');
432+
return ctx.stylize(`[Number: ${formatted}]`, 'number');
433433
}
434434
if (typeof raw === 'boolean') {
435435
formatted = formatPrimitiveNoColor(ctx, raw);
436-
return ctx.stylize('[Boolean: ' + formatted + ']', 'boolean');
436+
return ctx.stylize(`[Boolean: ${formatted}]`, 'boolean');
437437
}
438438
// Fast path for ArrayBuffer and SharedArrayBuffer.
439439
// Can't do the same for DataView because it has a non-primitive
@@ -535,8 +535,7 @@ function formatValue(ctx, value, recurseTimes) {
535535

536536
// Make functions say that they are functions
537537
if (typeof value === 'function') {
538-
var n = value.name ? ': ' + value.name : '';
539-
base = ' [Function' + n + ']';
538+
base = ` [Function${value.name ? `: ${value.name}` : ''}]`;
540539
}
541540

542541
// Make RegExps say that they are RegExps
@@ -557,24 +556,24 @@ function formatValue(ctx, value, recurseTimes) {
557556
// Make boxed primitive Strings look like such
558557
if (typeof raw === 'string') {
559558
formatted = formatPrimitiveNoColor(ctx, raw);
560-
base = ' ' + '[String: ' + formatted + ']';
559+
base = ` [String: ${formatted}]`;
561560
}
562561

563562
// Make boxed primitive Numbers look like such
564563
if (typeof raw === 'number') {
565564
formatted = formatPrimitiveNoColor(ctx, raw);
566-
base = ' ' + '[Number: ' + formatted + ']';
565+
base = ` [Number: ${formatted}]`;
567566
}
568567

569568
// Make boxed primitive Booleans look like such
570569
if (typeof raw === 'boolean') {
571570
formatted = formatPrimitiveNoColor(ctx, raw);
572-
base = ' ' + '[Boolean: ' + formatted + ']';
571+
base = ` [Boolean: ${formatted}]`;
573572
}
574573

575574
// Add constructor name if available
576575
if (base === '' && constructor)
577-
braces[0] = constructor.name + ' ' + braces[0];
576+
braces[0] = `${constructor.name} ${braces[0]}`;
578577

579578
if (empty === true) {
580579
return braces[0] + base + braces[1];
@@ -646,7 +645,7 @@ function formatPrimitiveNoColor(ctx, value) {
646645

647646

648647
function formatError(value) {
649-
return value.stack || '[' + Error.prototype.toString.call(value) + ']';
648+
return value.stack || `[${Error.prototype.toString.call(value)}]`;
650649
}
651650

652651

@@ -782,9 +781,9 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
782781
}
783782
if (!hasOwnProperty(visibleKeys, key)) {
784783
if (typeof key === 'symbol') {
785-
name = '[' + ctx.stylize(key.toString(), 'symbol') + ']';
784+
name = `[${ctx.stylize(key.toString(), 'symbol')}]`;
786785
} else {
787-
name = '[' + key + ']';
786+
name = `[${key}]`;
788787
}
789788
}
790789
if (!str) {
@@ -822,7 +821,7 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
822821
}
823822
}
824823

825-
return name + ': ' + str;
824+
return `${name}: ${str}`;
826825
}
827826

828827

@@ -837,13 +836,10 @@ function reduceToSingleString(output, base, braces, breakLength) {
837836
// we need to force the first item to be on the next line or the
838837
// items will not line up correctly.
839838
(base === '' && braces[0].length === 1 ? '' : base + '\n ') +
840-
' ' +
841-
output.join(',\n ') +
842-
' ' +
843-
braces[1];
839+
` ${output.join(',\n ')} ${braces[1]}`;
844840
}
845841

846-
return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
842+
return `${braces[0]}${base} ${output.join(', ')} ${braces[1]}`;
847843
}
848844

849845

@@ -1007,7 +1003,7 @@ exports.puts = internalUtil.deprecate(function() {
10071003

10081004

10091005
exports.debug = internalUtil.deprecate(function(x) {
1010-
process.stderr.write('DEBUG: ' + x + '\n');
1006+
process.stderr.write(`DEBUG: ${x}\n`);
10111007
}, 'util.debug is deprecated. Use console.error instead.');
10121008

10131009

@@ -1020,7 +1016,7 @@ exports.error = internalUtil.deprecate(function(x) {
10201016

10211017
exports._errnoException = function(err, syscall, original) {
10221018
var errname = uv.errname(err);
1023-
var message = syscall + ' ' + errname;
1019+
var message = `${syscall} ${errname}`;
10241020
if (original)
10251021
message += ' ' + original;
10261022
var e = new Error(message);
@@ -1038,13 +1034,13 @@ exports._exceptionWithHostPort = function(err,
10381034
additional) {
10391035
var details;
10401036
if (port && port > 0) {
1041-
details = address + ':' + port;
1037+
details = `${address}:${port}`;
10421038
} else {
10431039
details = address;
10441040
}
10451041

10461042
if (additional) {
1047-
details += ' - Local (' + additional + ')';
1043+
details += ` - Local (${additional})`;
10481044
}
10491045
var ex = exports._errnoException(err, syscall, details);
10501046
ex.address = address;

0 commit comments

Comments
 (0)