Skip to content

Commit 69ace6f

Browse files
committed
Properly fix out-of-order formatting substitution
Fixes #736
1 parent 8e6aea8 commit 69ace6f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

javascript/base/format.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const kNumberPlaceholders = new Set('dfi$'.split(''));
2323

2424
// Regular expression used to fully understand the syntax of a placeholder.
2525
const kPlaceholderExpression =
26-
/^(?:\{([^)]+)\})?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([bdfoisxX\$])/;
26+
/^(?:\{([^}]+)\})?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([bdfoisxX\$])/;
2727

2828
// Type of substitution that represents a literal passthrough for some text.
2929
const kTypePassthrough = '📝';

javascript/base/format.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,14 @@ describe('format', it => {
117117
assert.equal(format('[%b]', 25), '[11001]');
118118
assert.equal(format('[%o]', 25), '[31]');
119119
assert.equal(format('[%X]', 16776960), '[FFFF00]');
120+
assert.equal(format('[%%%s]', 25), '[%25]');
120121
assert.equal(format('[%s%%]', 25), '[25%]');
121122
assert.equal(format('[%d%d]', 1, 5), '[15]');
123+
124+
// Multi-parameter formats
125+
assert.equal(format('%d %s', 255, 'Foo Bar'), '255 Foo Bar');
126+
assert.equal(format('%{0}d %{1}s', 255, 'Foo Bar'), '255 Foo Bar');
127+
assert.equal(format('%{1}d %{0}s', 'Foo Bar', 255), '255 Foo Bar');
122128
});
123129

124130
it('it able to parse messages to formatting lists', assert => {
@@ -186,5 +192,13 @@ describe('format', it => {
186192
},
187193
{ type: '📝', text: 'b' },
188194
]);
195+
196+
assert.deepEqual(parseMessageToFormattingList(`a %{1}s b %{0}$ c`), [
197+
{ type: '📝', text: 'a ' },
198+
{ type: 's', index: 1 },
199+
{ type: '📝', text: ' b ' },
200+
{ type: '$', index: 0 },
201+
{ type: '📝', text: ' c' },
202+
]);
189203
});
190204
});

0 commit comments

Comments
 (0)