Skip to content

Commit

Permalink
Various: Always un-register event listeners for CLDR validation
Browse files Browse the repository at this point in the history
Fixes #878
Ref #889
  • Loading branch information
robaw authored Mar 17, 2020
1 parent daf1651 commit d5e98d1
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 40 deletions.
17 changes: 10 additions & 7 deletions src/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ Globalize.prototype.currencyFormatter = function( currency, options ) {

// Get properties given style ("symbol" default, "code" or "name").
cldr.on( "get", validateRequiredCldr );
properties = ({
accounting: currencySymbolProperties,
code: currencyCodeProperties,
name: currencyNameProperties,
symbol: currencySymbolProperties
}[ style ] )( currency, cldr, options );
cldr.off( "get", validateRequiredCldr );
try {
properties = ({
accounting: currencySymbolProperties,
code: currencyCodeProperties,
name: currencyNameProperties,
symbol: currencySymbolProperties
}[ style] )( currency, cldr, options );
} finally {
cldr.off( "get", validateRequiredCldr );
}

// options = options minus style, plus raw pattern.
options = objectOmit( options, "style" );
Expand Down
41 changes: 23 additions & 18 deletions src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,15 @@ Globalize.prototype.dateToPartsFormatter = function( options ) {
ianaListener = validateRequiredIana( timeZone );
cldr.on( "get", ianaListener );
}
pattern = dateExpandPattern( options, cldr );
validateOptionsSkeleton( pattern, options.skeleton );
properties = dateFormatProperties( pattern, cldr, timeZone );
cldr.off( "get", validateRequiredCldr );
if ( ianaListener ) {
cldr.off( "get", ianaListener );
try {
pattern = dateExpandPattern( options, cldr );
validateOptionsSkeleton( pattern, options.skeleton );
properties = dateFormatProperties( pattern, cldr, timeZone );
} finally {
cldr.off( "get", validateRequiredCldr );
if ( ianaListener ) {
cldr.off( "get", ianaListener );
}
}

// Create needed number formatters.
Expand Down Expand Up @@ -240,19 +243,21 @@ Globalize.prototype.dateParser = function( options ) {

args = [ options ];

cldr.on( "get", validateRequiredCldr );
if ( timeZone ) {
cldr.on( "get", validateRequiredIana( timeZone ) );
}
pattern = dateExpandPattern( options, cldr );
validateOptionsSkeleton( pattern, options.skeleton );
tokenizerProperties = dateTokenizerProperties( pattern, cldr, timeZone );
parseProperties = dateParseProperties( cldr, timeZone );
cldr.off( "get", validateRequiredCldr );
if ( timeZone ) {
cldr.off( "get", validateRequiredIana( timeZone ) );
try {
cldr.on( "get", validateRequiredCldr );
if ( timeZone ) {
cldr.on( "get", validateRequiredIana( timeZone ) );
}
pattern = dateExpandPattern( options, cldr );
validateOptionsSkeleton( pattern, options.skeleton );
tokenizerProperties = dateTokenizerProperties( pattern, cldr, timeZone );
parseProperties = dateParseProperties( cldr, timeZone );
} finally {
cldr.off( "get", validateRequiredCldr );
if ( timeZone ) {
cldr.off( "get", validateRequiredIana( timeZone ) );
}
}

numberParser = this.numberParser({ raw: "0" });

returnFn = dateParserFn( numberParser, parseProperties, tokenizerProperties );
Expand Down
21 changes: 11 additions & 10 deletions src/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,19 @@ Globalize.prototype.numberFormatter = function( options ) {
validateDefaultLocale( cldr );

cldr.on( "get", validateCldr );

if ( options.raw ) {
pattern = options.raw;
} else {
pattern = numberPattern( options.style || "decimal", cldr );
try {
if ( options.raw ) {
pattern = options.raw;
} else {
pattern = numberPattern( options.style || "decimal", cldr );
}

properties = numberFormatProperties( pattern, cldr, options );
fnArgs = [ properties ];
} finally {
cldr.off( "get", validateCldr );
}

properties = numberFormatProperties( pattern, cldr, options );
fnArgs = [ properties ];

cldr.off( "get", validateCldr );

validateDigits( properties );

if ( options.compact ) {
Expand Down
7 changes: 5 additions & 2 deletions src/plural.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ Globalize.prototype.pluralGenerator = function( options ) {
isOrdinal = type === "ordinal";

cldr.on( "get", validateCldr );
cldr.supplemental([ "plurals-type-" + type, "{language}" ]);
cldr.off( "get", validateCldr );
try {
cldr.supplemental([ "plurals-type-" + type, "{language}" ]);
} finally {
cldr.off( "get", validateCldr );
}

MakePlural.rules = {};
MakePlural.rules[ type ] = cldr.supplemental( "plurals-type-" + type );
Expand Down
8 changes: 5 additions & 3 deletions src/relative-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ Globalize.prototype.relativeTimeFormatter = function( unit, options ) {
validateDefaultLocale( cldr );

cldr.on( "get", validateCldr );
properties = relativeTimeProperties( unit, cldr, options );
cldr.off( "get", validateCldr );

try {
properties = relativeTimeProperties( unit, cldr, options );
} finally {
cldr.off( "get", validateCldr );
}
numberFormatter = this.numberFormatter( options );
pluralGenerator = this.pluralGenerator();

Expand Down
8 changes: 8 additions & 0 deletions test/functional/currency/currency-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ QUnit.test( "should validate CLDR content", function( assert ) {
});
});

QUnit.test( "should un-register event listener", function( assert ) {
try {
Globalize.currencyFormatter( "USD" );
} catch ( error ) {
assert.equal( Globalize.cldr.ee.getListeners( "get" ).length, 0 );
}
});

QUnit.test( "should return a currency formatter", function( assert ) {
var de, zh;

Expand Down
8 changes: 8 additions & 0 deletions test/functional/date/date-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ QUnit.test( "should validate CLDR content", function( assert ) {
});
});

QUnit.test( "should un-register event listener", function( assert ) {
try {
Globalize.dateParser({ skeleton: "invalid-stuff" });
} catch ( error ) {
assert.equal( Globalize.cldr.ee.getListeners( "get" ).length, 0 );
}
});

QUnit.test( "should return a parser", function( assert ) {
extraSetup();
assertParseDate( assert, "Wed, Sep 15, 2010 AD", { skeleton: "GyMMMEd" },
Expand Down
8 changes: 8 additions & 0 deletions test/functional/date/format-date-to-parts.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ QUnit.test( "should validate CLDR content", function( assert ) {
});
});

QUnit.test( "should un-register event listener", function( assert ) {
try {
Globalize.formatDateToParts( date, { skeleton: "invalid-stuff" } );
} catch ( error ) {
assert.equal( Globalize.cldr.ee.getListeners( "get" ).length, 0 );
}
});

QUnit.test( "should validate parameters (2/2)", function( assert ) {
extraSetup();

Expand Down
8 changes: 8 additions & 0 deletions test/functional/number/format-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ QUnit.test( "should validate CLDR content", function( assert ) {
});
});

QUnit.test( "should un-register event listener", function( assert ) {
try {
Globalize.formatNumber( pi );
} catch ( error ) {
assert.equal( Globalize.cldr.ee.getListeners( "get" ).length, 0 );
}
});

QUnit.test( "should format decimal style", function( assert ) {
extraSetup();

Expand Down
8 changes: 8 additions & 0 deletions test/functional/plural/plural.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ QUnit.test( "should validate CLDR content", function( assert ) {
});
});

QUnit.test( "should un-register event listener", function( assert ) {
try {
Globalize.plural( 1 );
} catch ( error ) {
assert.equal( Globalize.cldr.ee.getListeners( "get" ).length, 0 );
}
});

QUnit.test( "should return plural form", function( assert ) {
extraSetup();
assert.equal( Globalize.plural( 0 ), "other" );
Expand Down
8 changes: 8 additions & 0 deletions test/functional/relative-time/format-relative-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ QUnit.test( "should validate CLDR content", function( assert ) {
});
});

QUnit.test( "should un-register event listener", function( assert ) {
try {
Globalize.formatRelativeTime( 1, "day" );
} catch ( error ) {
assert.equal( Globalize.cldr.ee.getListeners( "get" ).length, 0 );
}
});

QUnit.module( ".formatRelativeTime( value, unit [, options] )", {
setup: function( ) {
Globalize.load( likelySubtags, enDateFields, deDateFields,
Expand Down

0 comments on commit d5e98d1

Please sign in to comment.