-
Notifications
You must be signed in to change notification settings - Fork 29.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
util: refactor format method. #12407
Changes from 7 commits
2d11e81
181ec8a
aa30bb2
bf6f8fe
9d032c2
da7a15e
78118c7
9f762cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,71 +68,59 @@ exports.format = function(f) { | |
return objects.join(' '); | ||
} | ||
|
||
var argLen = arguments.length; | ||
|
||
if (argLen === 1) return f; | ||
if (arguments.length === 1) return f; | ||
|
||
var str = ''; | ||
var a = 1; | ||
var lastPos = 0; | ||
for (var i = 0; i < f.length;) { | ||
if (f.charCodeAt(i) === 37/*'%'*/ && i + 1 < f.length) { | ||
if (f.charCodeAt(i) === 37/*'%'*/ && f.length > i + 1) { | ||
if (f.charCodeAt(i + 1) !== 37 && a >= arguments.length) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
++i; | ||
continue; | ||
} | ||
switch (f.charCodeAt(i + 1)) { | ||
case 100: // 'd' | ||
if (a >= argLen) | ||
break; | ||
if (lastPos < i) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer this style because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I didn't understand this point. You meain only remain There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean I prefer |
||
if (i > lastPos) | ||
str += f.slice(lastPos, i); | ||
str += Number(arguments[a++]); | ||
lastPos = i = i + 2; | ||
continue; | ||
break; | ||
case 105: // 'i' | ||
if (a >= argLen) | ||
break; | ||
if (lastPos < i) | ||
if (i > lastPos) | ||
str += f.slice(lastPos, i); | ||
str += parseInt(arguments[a++]); | ||
lastPos = i = i + 2; | ||
continue; | ||
break; | ||
case 102: // 'f' | ||
if (a >= argLen) | ||
break; | ||
if (lastPos < i) | ||
if (i > lastPos) | ||
str += f.slice(lastPos, i); | ||
str += parseFloat(arguments[a++]); | ||
lastPos = i = i + 2; | ||
continue; | ||
break; | ||
case 106: // 'j' | ||
if (a >= argLen) | ||
break; | ||
if (lastPos < i) | ||
if (i > lastPos) | ||
str += f.slice(lastPos, i); | ||
str += tryStringify(arguments[a++]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems this function was used only here (linter reports There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. I commited the changes. Also passed all tests. |
||
lastPos = i = i + 2; | ||
continue; | ||
break; | ||
case 115: // 's' | ||
if (a >= argLen) | ||
break; | ||
if (lastPos < i) | ||
if (i > lastPos) | ||
str += f.slice(lastPos, i); | ||
str += String(arguments[a++]); | ||
lastPos = i = i + 2; | ||
continue; | ||
break; | ||
case 37: // '%' | ||
if (lastPos < i) | ||
if (i > lastPos) | ||
str += f.slice(lastPos, i); | ||
str += '%'; | ||
lastPos = i = i + 2; | ||
continue; | ||
break; | ||
} | ||
lastPos = i = i + 2; | ||
continue; | ||
} | ||
++i; | ||
} | ||
if (lastPos === 0) | ||
str = f; | ||
else if (lastPos < f.length) | ||
str += f.slice(lastPos); | ||
while (a < argLen) { | ||
while (a < arguments.length) { | ||
const x = arguments[a++]; | ||
if (x === null || (typeof x !== 'object' && typeof x !== 'symbol')) { | ||
str += ' ' + x; | ||
|
@@ -143,7 +131,6 @@ exports.format = function(f) { | |
return str; | ||
}; | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unintended whitespace change? |
||
exports.deprecate = internalUtil.deprecate; | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think having the "static" portion on the right-hand side is more traditional.