-
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 3 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 |
---|---|---|
|
@@ -59,6 +59,16 @@ function tryStringify(arg) { | |
} | ||
} | ||
|
||
function getFormatFn(code) { | ||
switch (code) { | ||
case 100/*'d'*/: return Number; | ||
case 105/*'i'*/: return parseInt; | ||
case 102/*'f'*/: return parseFloat; | ||
case 106/*'j'*/: return tryStringify; | ||
case 115/*'s'*/: return String; | ||
} | ||
} | ||
|
||
exports.format = function(f) { | ||
if (typeof f !== 'string') { | ||
const objects = new Array(arguments.length); | ||
|
@@ -75,55 +85,25 @@ exports.format = function(f) { | |
var str = ''; | ||
var a = 1; | ||
var lastPos = 0; | ||
for (var i = 0; i < f.length;) { | ||
var i = 0; | ||
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++]); | ||
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; | ||
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 (f.charCodeAt(i + 1) === 37) { | ||
if (lastPos < i) | ||
str += f.slice(lastPos, i); | ||
str += '%'; | ||
lastPos = i = i + 2; | ||
continue; | ||
} | ||
if (a < argLen) { | ||
var fn = getFormatFn(f.charCodeAt(i + 1)); | ||
if (fn) { | ||
if (lastPos < i) | ||
str += f.slice(lastPos, i); | ||
str += '%'; | ||
str += fn(arguments[a++]); | ||
lastPos = i = i + 2; | ||
continue; | ||
} | ||
} | ||
} | ||
++i; | ||
|
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
lastPos
will 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 < i
instead 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