Permalink
Browse files

Number: Fix parser to test the whole grammar statement

Fixes #660
Ref #292
Ref #353
  • Loading branch information...
1 parent 1daba30 commit 2b329f4da3d35b225653c3e36373e78d08174329 @rxaviers rxaviers committed Dec 30, 2016
Showing with 8 additions and 6 deletions.
  1. +5 −6 src/number/parse.js
  2. +3 −0 test/unit/number/parse.js
View
@@ -98,19 +98,18 @@ return function( value, properties ) {
function tokenizeNParse( _value, grammar ) {
return grammar.some(function( statement ) {
var value = _value;
- statement.every(function( type ) {
+
+ // The whole grammar statement should be used (i.e., .every() return true) and value be
+ // entirely consumed (i.e., !value.length).
+ return statement.every(function( type ) {
if ( value.match( tokenizer[ type ] ) === null ) {
return false;
}
// Consume and parse it.
value = value.replace( tokenizer[ type ], parse( type ) );
return true;
- });
-
- // Note: .every() return value above is ignored, because what counts in the end is
- // whether value is entirely consumed.
- return !value.length;
+ }) && !value.length;
});
}
@@ -58,6 +58,8 @@ QUnit.test( "should parse grouping separators", function( assert ) {
assert.equal( parse( "12735", properties( "#,##0.#", en ) ), 12735 );
assert.equal( parse( "1,2,7,35", properties( "#,#,#0.#", en ) ), 12735 );
assert.equal( parse( "12.735", properties( "#,##0", es ) ), 12735 );
+ assert.equal( parse( "1000", properties( "#,##0", en ) ), 1000 );
+ assert.equal( parse( "1000", properties( "#,##,##0", en ) ), 1000 );
});
QUnit.test( "should parse invalid grouping separators as NaN", function( assert ) {
@@ -131,6 +133,7 @@ QUnit.test( "should parse percent", function( assert ) {
assert.equal( parse( "0.5%", properties( "##0.#%", en ) ), 0.005 );
assert.equal( parse( "0.5%", properties( "##0.#%", en ) ), 0.005 );
assert.equal( parse( "%100", properties( "%0", en ) ), 1 );
+ assert.deepEqual( parse( "1", properties( "0%", en ) ), NaN );
});
QUnit.test( "should localize percent symbol (%)", function( assert ) {

0 comments on commit 2b329f4

Please sign in to comment.