Skip to content

Commit

Permalink
util: remove duplicate code in format
Browse files Browse the repository at this point in the history
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 MylesBorins committed Sep 12, 2017
1 parent 605d625 commit 2aec977
Showing 1 changed file with 2 additions and 18 deletions.
20 changes: 2 additions & 18 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,51 +110,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;
Expand Down

0 comments on commit 2aec977

Please sign in to comment.