-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
util: refactor format method. #12407
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
Changes from 5 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 |
|---|---|---|
|
|
@@ -75,55 +75,25 @@ exports.format = function(f) { | |
| var str = ''; | ||
| var a = 1; | ||
| var lastPos = 0; | ||
| for (var i = 0; i < f.length;) { | ||
| var i = 0; | ||
| var value; | ||
| while (i < f.length) { | ||
| if (f.charCodeAt(i) === 37/*'%'*/ && i + 1 < f.length) { | ||
| switch (f.charCodeAt(i + 1)) { | ||
| case 100: // 'd' | ||
| if (a >= argLen) | ||
| break; | ||
| if (lastPos < i) | ||
| str += f.slice(lastPos, i); | ||
| str += Number(arguments[a++]); | ||
| lastPos = i = i + 2; | ||
| continue; | ||
| case 105: // 'i' | ||
| if (a >= argLen) | ||
| break; | ||
| if (lastPos < i) | ||
| str += f.slice(lastPos, i); | ||
| str += parseInt(arguments[a++]); | ||
| lastPos = i = i + 2; | ||
| continue; | ||
| case 102: // 'f' | ||
| if (a >= argLen) | ||
| break; | ||
| if (lastPos < i) | ||
| str += f.slice(lastPos, i); | ||
| str += parseFloat(arguments[a++]); | ||
| lastPos = i = i + 2; | ||
| continue; | ||
| case 106: // 'j' | ||
| if (a >= argLen) | ||
| break; | ||
| if (lastPos < i) | ||
| str += f.slice(lastPos, i); | ||
| str += tryStringify(arguments[a++]); | ||
|
Contributor
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
Author
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; | ||
| case 115: // 's' | ||
| if (a >= argLen) | ||
| break; | ||
| if (lastPos < i) | ||
| str += f.slice(lastPos, i); | ||
| str += String(arguments[a++]); | ||
| lastPos = i = i + 2; | ||
| continue; | ||
| case 37: // '%' | ||
| if (lastPos < i) | ||
| str += f.slice(lastPos, i); | ||
| str += '%'; | ||
| lastPos = i = i + 2; | ||
| continue; | ||
| if (f.charCodeAt(i + 1) === 37 || a < argLen) { | ||
| switch (f.charCodeAt(i + 1)) { | ||
| case 100/*'d'*/: value = Number(arguments[a++]); break; | ||
| case 105/*'i'*/: value = parseInt(arguments[a++]); break; | ||
| case 102/*'f'*/: value = parseFloat(arguments[a++]); break; | ||
| case 106/*'j'*/: value = tryStringify(arguments[a++]); break; | ||
| case 115/*'s'*/: value = String(arguments[a++]); break; | ||
| case 37/*'%'*/: value = '%'; break; | ||
| default: ++i; continue; | ||
| } | ||
| if (lastPos < i) | ||
| str += f.slice(lastPos, i); | ||
| str += value; | ||
| lastPos = i = i + 2; | ||
| continue; | ||
| } | ||
| } | ||
| ++i; | ||
|
|
@@ -143,7 +113,6 @@ exports.format = function(f) { | |
| return str; | ||
| }; | ||
|
|
||
|
|
||
|
Contributor
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 prefer this style because
lastPoswill always be either less than or equal toi.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.
Sorry, I didn't understand this point. You meain only remain
lastPos < iinstead ofi > lastPos? Thank you!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 mean I prefer
lastPos < i