Skip to content
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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 24 additions & 44 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Copy link
Contributor

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 to i.

Copy link
Author

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 of i > lastPos ? Thank you!

Copy link
Contributor

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

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++]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this function was used only here (linter reports 'tryStringify' is defined but never used)

Copy link
Author

Choose a reason for hiding this comment

The 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;
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-util-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ assert.strictEqual(util.format('%%s%s', 'foo'), '%sfoo');
assert.strictEqual(util.format('%s:%s'), '%s:%s');
assert.strictEqual(util.format('%s:%s', undefined), 'undefined:%s');
assert.strictEqual(util.format('%s:%s', 'foo'), 'foo:%s');
assert.strictEqual(util.format('%s:%i', 'foo'), 'foo:%i');
assert.strictEqual(util.format('%s:%f', 'foo'), 'foo:%f');
assert.strictEqual(util.format('%s:%s', 'foo', 'bar'), 'foo:bar');
assert.strictEqual(util.format('%s:%s', 'foo', 'bar', 'baz'), 'foo:bar baz');
assert.strictEqual(util.format('%%%s%%', 'hi'), '%hi%');
Expand All @@ -104,6 +106,12 @@ assert.strictEqual(util.format('%sbc%%def', 'a'), 'abc%def');
assert.strictEqual(util.format('%d:%d', 12, 30), '12:30');
assert.strictEqual(util.format('%d:%d', 12), '12:%d');
assert.strictEqual(util.format('%d:%d'), '%d:%d');
assert.strictEqual(util.format('%i:%i', 12, 30), '12:30');
assert.strictEqual(util.format('%i:%i', 12), '12:%i');
assert.strictEqual(util.format('%i:%i'), '%i:%i');
assert.strictEqual(util.format('%f:%f', 12, 30), '12:30');
assert.strictEqual(util.format('%f:%f', 12), '12:%f');
assert.strictEqual(util.format('%f:%f'), '%f:%f');
assert.strictEqual(util.format('o: %j, a: %j', {}, []), 'o: {}, a: []');
assert.strictEqual(util.format('o: %j, a: %j', {}), 'o: {}, a: %j');
assert.strictEqual(util.format('o: %j, a: %j'), 'o: %j, a: %j');
Expand Down