Skip to content

Commit

Permalink
Number parser: Lenient about trailing decimal separator
Browse files Browse the repository at this point in the history
  • Loading branch information
rxaviers committed Jun 26, 2017
1 parent 656f4d2 commit 4153526
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/number/parse-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ return function( pattern, cldr, options ) {
if ( !isNaN( minimumSignificantDigits * maximumSignificantDigits ) || /* 1 */
maximumFractionDigits /* 2 */ ) {

// 1: Handle trailing decimal separator, e.g., `"1." => `1``.
aux = decimalSymbolRe + digitsRe + "+";
numberTokenizer = numberTokenizer + "(" + aux + ")?" +
numberTokenizer = numberTokenizer + "(" + aux + "|" + decimalSymbolRe /* 1 */ + ")?" +

// Handle non-padded decimals, e.g., `".12"` => `0.12` by making the integer part
// optional.
Expand Down
10 changes: 5 additions & 5 deletions test/unit/number/parse-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,26 @@ QUnit.test( "should return negativeSuffix tokenizer", function( assert ) {

QUnit.test( "should return number tokenizer", function( assert ) {
assert.deepEqual( properties( "0", en )[ 2 ].number, /^\d+/ );
assert.deepEqual( properties( "0.##", en )[ 2 ].number, /^(\d+(\.\d+)?|(\d+)?\.\d+)/ );
assert.deepEqual( properties( "0.##", en )[ 2 ].number, /^(\d+(\.\d+|\.)?|(\d+)?\.\d+)/ );

assert.deepEqual(
properties( "#,##0.##", en )[ 2 ].number,
/^((\d{1,3}(,\d{3})+|\d+)(\.\d+)?|((\d{1,3}(,\d{3})+|\d+))?\.\d+)/
/^((\d{1,3}(,\d{3})+|\d+)(\.\d+|\.)?|((\d{1,3}(,\d{3})+|\d+))?\.\d+)/
);

assert.deepEqual(
properties( "#,##0.##", es )[ 2 ].number,
/^((\d{1,3}(\.\d{3})+|\d+)(,\d+)?|((\d{1,3}(\.\d{3})+|\d+))?,\d+)/
/^((\d{1,3}(\.\d{3})+|\d+)(,\d+|,)?|((\d{1,3}(\.\d{3})+|\d+))?,\d+)/
);

assert.deepEqual(
properties( "#,##,##0.##", en )[ 2 ].number,
/^((\d{1,2}((,\d{2})*(,\d{3}))|\d+)(\.\d+)?|((\d{1,2}((,\d{2})*(,\d{3}))|\d+))?\.\d+)/
/^((\d{1,2}((,\d{2})*(,\d{3}))|\d+)(\.\d+|\.)?|((\d{1,2}((,\d{2})*(,\d{3}))|\d+))?\.\d+)/
);

assert.deepEqual(
properties( "#,##0.##", sv )[ 2 ].number,
/^((\d{1,3}( \d{3})+|\d+)(,\d+)?|((\d{1,3}( \d{3})+|\d+))?,\d+)/
/^((\d{1,3}( \d{3})+|\d+)(,\d+|,)?|((\d{1,3}( \d{3})+|\d+))?,\d+)/
);
});

Expand Down
6 changes: 6 additions & 0 deletions test/unit/number/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ QUnit.test( "should parse zero-padded decimals", function( assert ) {
assert.equal( parse( "0.10", properties( "0.00", en ) ), 0.1 );
});

QUnit.test( "should parse trailing decimal separator", function( assert ) {
assert.equal( parse( "1.", properties( "0.0", en ) ), 1 );
assert.equal( parse( "١٫", properties( "0.0", ar ) ), 1 );
assert.equal( parse( "1,", properties( "0.0", pt ) ), 1 );
});

QUnit.test( "should parse non-padded decimals", function( assert ) {
assert.equal( parse( ".14159", properties( "0.0", en ) ), 0.14159 );
assert.equal( parse( ".752", properties( "0.0", en ) ), 0.752 );
Expand Down

0 comments on commit 4153526

Please sign in to comment.