Skip to content
Permalink
Browse files
Properly fix out-of-order formatting substitution
Fixes #736
  • Loading branch information
RussellLVP committed Jun 21, 2020
1 parent 8e6aea8 commit 69ace6f269addd6ed689cd20adcadec840ca24a5
Showing 2 changed files with 15 additions and 1 deletion.
@@ -23,7 +23,7 @@ const kNumberPlaceholders = new Set('dfi$'.split(''));

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

// Type of substitution that represents a literal passthrough for some text.
const kTypePassthrough = '📝';
@@ -117,8 +117,14 @@ describe('format', it => {
assert.equal(format('[%b]', 25), '[11001]');
assert.equal(format('[%o]', 25), '[31]');
assert.equal(format('[%X]', 16776960), '[FFFF00]');
assert.equal(format('[%%%s]', 25), '[%25]');
assert.equal(format('[%s%%]', 25), '[25%]');
assert.equal(format('[%d%d]', 1, 5), '[15]');

// Multi-parameter formats
assert.equal(format('%d %s', 255, 'Foo Bar'), '255 Foo Bar');
assert.equal(format('%{0}d %{1}s', 255, 'Foo Bar'), '255 Foo Bar');
assert.equal(format('%{1}d %{0}s', 'Foo Bar', 255), '255 Foo Bar');
});

it('it able to parse messages to formatting lists', assert => {
@@ -186,5 +192,13 @@ describe('format', it => {
},
{ type: '📝', text: 'b' },
]);

assert.deepEqual(parseMessageToFormattingList(`a %{1}s b %{0}$ c`), [
{ type: '📝', text: 'a ' },
{ type: 's', index: 1 },
{ type: '📝', text: ' b ' },
{ type: '$', index: 0 },
{ type: '📝', text: ' c' },
]);
});
});

0 comments on commit 69ace6f

Please sign in to comment.