Skip to content

Commit

Permalink
Number: Fix parser to handle empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
rxaviers committed Feb 23, 2017
1 parent 2568ce7 commit fd84850
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/number/parse-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,14 @@ return function( pattern, cldr, options ) {
if ( !isNaN( minimumSignificantDigits * maximumSignificantDigits ) || /* 1 */
maximumFractionDigits /* 2 */ ) {

// Handle non-padded decimals, e.g., `".12"` => `0.12` by making the integer part optional.
numberTokenizer = "(" + numberTokenizer + ")?";
aux = decimalSymbolRe + digitsRe + "+";
numberTokenizer = numberTokenizer + "(" + aux + ")?" +

numberTokenizer += "(" + decimalSymbolRe + digitsRe + "+)?";
// Handle non-padded decimals, e.g., `".12"` => `0.12` by making the integer part
// optional.
"|(" + numberTokenizer + ")?" + aux;

numberTokenizer = "(" + numberTokenizer + ")";
}

// 0: @invertedSymbolMap [Object] Inverted symbol map.
Expand Down
2 changes: 1 addition & 1 deletion test/functional/number/number-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ QUnit.test( "should allow for runtime compilation", function( assert ) {
nan: /^NaN/,
negativePrefix: /^-/,
negativeSuffix: /^/,
number: /^((\d{1,3}(,\d{3})+|\d+))?(\.\d+)?/,
number: /^((\d{1,3}(,\d{3})+|\d+)(\.\d+)?|((\d{1,3}(,\d{3})+|\d+))?\.\d+)/,
prefix: /^/,
suffix: /^/
}
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+)?/ );
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+)/
);

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+)/
);

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+)/
);

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+)/
);
});

Expand Down
5 changes: 5 additions & 0 deletions test/unit/number/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ QUnit.test( "should parse integers", function( assert ) {
assert.equal( parse( "3", properties( "0", en ) ), 3 );
});

QUnit.test( "should parse invalid integers as NaN", function( assert ) {
assert.deepEqual( parse( "", properties( "0", en ) ), NaN );
});

QUnit.test( "should parse zero-padded integers", function( assert ) {
assert.equal( parse( "003", properties( "000", en ) ), 3 );
});
Expand Down Expand Up @@ -95,6 +99,7 @@ QUnit.test( "should parse decimals", function( assert ) {
});

QUnit.test( "should parse invalid decimals as NaN", function( assert ) {
assert.deepEqual( parse( "", properties( "0.#", en ) ), NaN );
assert.deepEqual( parse( "3,14", properties( "0.#", en ) ), NaN );
assert.deepEqual( parse( "3.14", properties( "0.#", es ) ), NaN );
});
Expand Down

0 comments on commit fd84850

Please sign in to comment.