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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
83a5eef6f2
commit
365c24591c
20
lib/util.js
20
lib/util.js
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user