Skip to content

Commit

Permalink
Currency: Implement currency formatting (name and code mode) (2/2)
Browse files Browse the repository at this point in the history
  • Loading branch information
rxaviers committed Dec 10, 2014
1 parent 0aadce3 commit a2f6865
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 100 deletions.
27 changes: 13 additions & 14 deletions src/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ define([
"./currency/code-properties",
"./currency/name-format",
"./currency/name-properties",
"./currency/symbol-pattern",
"./currency/symbol-properties",
"./util/object/omit",
"./number",
"cldr/event"
], function( Globalize, validateCldr, validateDefaultLocale, validateParameterPresence,
validateParameterTypeNumber, validateParameterTypeCurrency, validateParameterTypePlainObject,
validatePluralModulePresence, currencyCodeProperties, currencyNameFormat,
currencyNameProperties, currencySymbolPattern, objectOmit ) {
currencyNameProperties, currencySymbolProperties, objectOmit ) {

function validateRequiredCldr( path, value ) {
validateCldr( path, value, {
Expand All @@ -39,40 +39,39 @@ function validateRequiredCldr( path, value ) {
*/
Globalize.currencyFormatter =
Globalize.prototype.currencyFormatter = function( currency, options ) {
var cldr, fn, numberFormatter, plural, properties;
var cldr, numberFormatter, plural, properties, style;

validateParameterPresence( currency, "currency" );
validateParameterTypeCurrency( currency, "currency" );

validateParameterTypePlainObject( options, "options" );

options = options || {};
style = options.style || "symbol";
cldr = this.cldr;

validateDefaultLocale( cldr );

cldr.on( "get", validateRequiredCldr );

// Get properties given style ("symbol" default, "code" or "name").
fn = { code: currencyCodeProperties, name: currencyNameProperties };
properties = ( fn[ options.style ] || currencySymbolPattern )( currency, cldr,
options );

cldr.on( "get", validateRequiredCldr );
properties = ({
code: currencyCodeProperties,
name: currencyNameProperties,
symbol: currencySymbolProperties
}[ style ] )( currency, cldr, options );
cldr.off( "get", validateRequiredCldr );

// options = options minus style, plus pattern.
options = objectOmit( options, "style" );
options.pattern = properties.pattern;

// Return formatter when style is "symbol".
if ( typeof properties === "string" ) {

// options = options minus style, plus pattern.
options.pattern = properties;
if ( style === "symbol" ) {
return this.numberFormatter( options );
}

// Return formatter when style is "code" or "name".
validatePluralModulePresence();
options.pattern = properties.pattern;
numberFormatter = this.numberFormatter( options );
plural = this.pluralGenerator();
return function( value ) {
Expand Down
25 changes: 8 additions & 17 deletions src/currency/name-properties.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
define([
"./supplemental-override",
"./unit-patterns",
"../number/pattern",
"./code-properties",
"../util/object/filter"
], function( currencySupplementalOverride, currencyUnitPatterns, numberPattern, objectFilter ) {
], function( currencyCodeProperties, objectFilter ) {

/**
* nameProperties( currency, cldr )
*
* Return number pattern with the appropriate currency code in as literal.
*/
return function( currency, cldr ) {
var pattern = numberPattern( "decimal", cldr );
var properties = currencyCodeProperties( currency, cldr );

// The number of decimal places and the rounding for each currency is not locale-specific. Those
// values overridden by Supplemental Currency Data.
pattern = currencySupplementalOverride( currency, pattern, cldr );
properties.displayNames = objectFilter( cldr.main([
"numbers/currencies",
currency
]), /^displayName/ );

return {
currency: currency,
displayNames: objectFilter( cldr.main([
"numbers/currencies",
currency
]), /^displayName/ ),
pattern: pattern,
unitPatterns: currencyUnitPatterns( cldr )
};
return properties;
};

});
43 changes: 14 additions & 29 deletions test/functional/currency/currency-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ define([
], function( Globalize, deCurrencies, deNumbers, enCurrencies, enNumbers, zhCurrencies,
zhNumbers, currencyData, likelySubtags, plurals, util ) {

var teslaS = 69900;
var code = { style: "code" },
name = { style: "name" },
teslaS = 69900;

function extraSetup() {
Globalize.load(
Expand Down Expand Up @@ -66,6 +68,7 @@ QUnit.test( "should validate CLDR content", function( assert ) {

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

extraSetup();

de = Globalize( "de" );
Expand All @@ -75,29 +78,13 @@ QUnit.test( "should return a currency formatter", function( assert ) {
assert.equal( de.currencyFormatter( "USD" )( teslaS ), "69.900,00 $" );
assert.equal( zh.currencyFormatter( "USD" )( teslaS ), "US$ 69,900.00" );

assert.equal( Globalize.currencyFormatter( "USD", {
style: "code"
})( teslaS ), "69,900.00 USD" );

assert.equal( de.currencyFormatter( "USD", {
style: "code"
})( teslaS ), "69.900,00 USD" );

assert.equal( zh.currencyFormatter( "USD", {
style: "code"
})( teslaS ), "69,900.00USD" );

assert.equal( Globalize.currencyFormatter( "USD", {
style: "name"
})( teslaS ), "69,900.00 US dollars" );
assert.equal( Globalize.currencyFormatter( "USD", code )( teslaS ), "69,900.00 USD" );
assert.equal( de.currencyFormatter( "USD", code )( teslaS ), "69.900,00 USD" );
assert.equal( zh.currencyFormatter( "USD", code )( teslaS ), "69,900.00USD" );

assert.equal( de.currencyFormatter( "USD", {
style: "name"
})( teslaS ), "69.900,00 US-Dollar" );

assert.equal( zh.currencyFormatter( "USD", {
style: "name"
})( teslaS ), "69,900.00美元" );
assert.equal( Globalize.currencyFormatter( "USD", name )( teslaS ), "69,900.00 US dollars" );
assert.equal( de.currencyFormatter( "USD", name )( teslaS ), "69.900,00 US-Dollar" );
assert.equal( zh.currencyFormatter( "USD", name )( teslaS ), "69,900.00美元" );
});

// The number of decimal places and the rounding for each currency is not locale-specific data.
Expand All @@ -112,13 +99,11 @@ QUnit.test( "should return a currency formatter, overriden by Supplemental Curre
assert.equal( Globalize.currencyFormatter( "ZWD" )( 12345.67 ), "ZWD 12,345" );
assert.equal( Globalize.currencyFormatter( "JPY" )( 12345.67 ), "¥12,345" );

assert.equal( Globalize.currencyFormatter( "CLF", {
style: "code"
})( 12345.67 ), "12,345.6700 CLF" );
assert.equal( Globalize.currencyFormatter( "CLF", code )( 12345.67 ),
"12,345.6700 CLF" );

assert.equal( Globalize.currencyFormatter( "CLF", {
style: "name"
})( 12345.67 ), "12,345.6700 Chilean units of account (UF)" );
assert.equal( Globalize.currencyFormatter( "CLF", name )( 12345.67 ),
"12,345.6700 Chilean units of account (UF)" );
});

// User options should override everything.
Expand Down
73 changes: 33 additions & 40 deletions test/unit/currency/name-format.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
define([
"src/currency/name-format"
], function( format ) {
"cldr",
"src/currency/name-format",
"src/currency/name-properties",
"json!cldr-data/main/en/currencies.json",
"json!cldr-data/main/en/numbers.json",
"json!cldr-data/main/zh/currencies.json",
"json!cldr-data/main/zh/numbers.json",
"json!cldr-data/supplemental/currencyData.json",
"json!cldr-data/supplemental/likelySubtags.json"
], function( Cldr, format, properties, enCurrencies, enNumbers, zhCurrencies, zhNumbers,
currencyData, likelySubtags ) {

var en, zh;

Cldr.load(
currencyData,
enCurrencies,
enNumbers,
likelySubtags,
zhCurrencies,
zhNumbers
);

en = new Cldr( "en" );
zh = new Cldr( "zh" );

QUnit.module( "Currency Name Foramt" );

QUnit.test( "should return appropriate properties", function( assert ) {
assert.deepEqual( format( "1", "one", {
"currency": "USD",
"displayNames": {
"displayName": "US Dollar",
"displayName-count-one": "US dollar",
"displayName-count-other": "US dollars"
},
"unitPatterns": {
"unitPattern-count-one": "{0} {1}",
"unitPattern-count-other": "{0} {1}"
}
}), "1 US dollar" );

assert.deepEqual( format( "2", "other", {
"currency": "USD",
"displayNames": {
"displayName": "US Dollar",
"displayName-count-one": "US dollar",
"displayName-count-other": "US dollars"
},
"unitPatterns": {
"unitPattern-count-one": "{0} {1}",
"unitPattern-count-other": "{0} {1}"
}
}), "2 US dollars" );
assert.deepEqual( format( "1", "one", properties( "USD", en ) ), "1 US dollar" );
assert.deepEqual( format( "2", "other", properties( "USD", en ) ), "2 US dollars" );

assert.deepEqual( format( "1", "anything", {
// Test the fallback to displayNames by the lack of displayNames-count-*.
assert.deepEqual( format( "1", "something", {
"displayNames": {
"displayName": "US Dollar",
},
Expand All @@ -40,25 +41,17 @@ QUnit.test( "should return appropriate properties", function( assert ) {
}
}), "1 US Dollar" );

assert.deepEqual( format( "1", "anything", {
// Test the fallback to currency by the lack of displayName and displayName-count-*.
assert.deepEqual( format( "1", "something", {
"currency": "USD",
"unitPatterns": {
"unitPattern-count-other": "{0} {1}"
}
}), "1 USD" );

assert.deepEqual( format( "10", "other", {
"currency": "CNY",
"displayNames": {
"displayName": "人民币",
"displayName-count-other": "人民币"
},
"pattern": "#,##0.###",
"unitPatterns": {
"unitPattern-count-other": "{0}{1}"
}
}), "10人民币" );
assert.deepEqual( format( "10", "other", properties( "CNY", zh ) ), "10人民币" );

// Testing inverted unitPattern {1} {0}.
assert.deepEqual( format( "1", "other", {
"currency": "TZS",
"unitPatterns": {
Expand Down

0 comments on commit a2f6865

Please sign in to comment.