Permalink
Browse files

util: remove duplicate code in format

util.format contains an idential if statement within each branch
of switch. Move it before the switch statement for cleaner code
and better performance.

PR-URL: #15098
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information...
apapirovski authored and BridgeAR committed Aug 30, 2017
1 parent 83a5eef commit 365c24591cb0268b13818fe077c63f55de5c8404
Showing with 2 additions and 18 deletions.
  1. +2 −18 lib/util.js
View
@@ -111,51 +111,35 @@ function format(f) {
++i;
continue;
}
if (lastPos < i)
str += f.slice(lastPos, i);
switch (f.charCodeAt(i + 1)) {
case 100: // 'd'
if (lastPos < i)
str += f.slice(lastPos, i);
str += Number(arguments[a++]);
break;
case 105: // 'i'
if (lastPos < i)
str += f.slice(lastPos, i);
str += parseInt(arguments[a++]);
break;
case 102: // 'f'
if (lastPos < i)
str += f.slice(lastPos, i);
str += parseFloat(arguments[a++]);
break;
case 106: // 'j'
if (lastPos < i)
str += f.slice(lastPos, i);
str += tryStringify(arguments[a++]);
break;
case 115: // 's'
if (lastPos < i)
str += f.slice(lastPos, i);
str += String(arguments[a++]);
break;
case 79: // 'O'
if (lastPos < i)
str += f.slice(lastPos, i);
str += inspect(arguments[a++]);
break;
case 111: // 'o'
if (lastPos < i)
str += f.slice(lastPos, i);
str += inspect(arguments[a++],
{ showHidden: true, depth: 4, showProxy: true });
break;
case 37: // '%'
if (lastPos < i)
str += f.slice(lastPos, i);
str += '%';
break;
default: // any other character is not a correct placeholder
if (lastPos < i)
str += f.slice(lastPos, i);
str += '%';
lastPos = i = i + 1;
continue;

0 comments on commit 365c245

Please sign in to comment.