diff --git a/src/currency.js b/src/currency.js index 9192f9e66..63831f830 100644 --- a/src/currency.js +++ b/src/currency.js @@ -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" ); diff --git a/src/date.js b/src/date.js index db19c99c6..4453a914f 100644 --- a/src/date.js +++ b/src/date.js @@ -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. @@ -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 ); diff --git a/src/number.js b/src/number.js index 0e4bf83fc..1d6f0f932 100644 --- a/src/number.js +++ b/src/number.js @@ -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 ) { diff --git a/src/plural.js b/src/plural.js index b9781546c..a67e9aa7a 100644 --- a/src/plural.js +++ b/src/plural.js @@ -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 ); diff --git a/src/relative-time.js b/src/relative-time.js index 4c36886af..a4d5f8314 100644 --- a/src/relative-time.js +++ b/src/relative-time.js @@ -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(); diff --git a/test/functional/currency/currency-formatter.js b/test/functional/currency/currency-formatter.js index d4ef833c8..5ced5df02 100644 --- a/test/functional/currency/currency-formatter.js +++ b/test/functional/currency/currency-formatter.js @@ -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; diff --git a/test/functional/date/date-parser.js b/test/functional/date/date-parser.js index b9f9305d8..12625be07 100644 --- a/test/functional/date/date-parser.js +++ b/test/functional/date/date-parser.js @@ -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" }, diff --git a/test/functional/date/format-date-to-parts.js b/test/functional/date/format-date-to-parts.js index cae9fcd53..024039f5c 100644 --- a/test/functional/date/format-date-to-parts.js +++ b/test/functional/date/format-date-to-parts.js @@ -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(); diff --git a/test/functional/number/format-number.js b/test/functional/number/format-number.js index a171ca9c0..343c9e1ca 100644 --- a/test/functional/number/format-number.js +++ b/test/functional/number/format-number.js @@ -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(); diff --git a/test/functional/plural/plural.js b/test/functional/plural/plural.js index 3f628ea4a..c91858247 100644 --- a/test/functional/plural/plural.js +++ b/test/functional/plural/plural.js @@ -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" ); diff --git a/test/functional/relative-time/format-relative-time.js b/test/functional/relative-time/format-relative-time.js index b898c3ccd..f0bfa2c25 100644 --- a/test/functional/relative-time/format-relative-time.js +++ b/test/functional/relative-time/format-relative-time.js @@ -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,