Skip to content

Commit

Permalink
Currency: Implement currency formatting (symbol mode) (2/2)
Browse files Browse the repository at this point in the history
Ref #238
Ref #351
  • Loading branch information
rxaviers committed Dec 10, 2014
1 parent 4e8fce0 commit 0aadce3
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 136 deletions.
63 changes: 0 additions & 63 deletions src/currency/symbol-pattern.js

This file was deleted.

74 changes: 74 additions & 0 deletions src/currency/symbol-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
define([
"./supplemental-override",
"../number/numbering-system",
"../util/regexp/not-s"
], function( currencySupplementalOverride, numberNumberingSystem, regexpNotS ) {

/**
* symbolProperties( currency, cldr )
*
* Return pattern replacing `¤` with the appropriate currency symbol literal.
*/
return function( currency, cldr, options ) {
var currencySpacing, pattern,
regexp = {
"[:digit:]": /\d/,
"[:^S:]": regexpNotS
},
symbol = cldr.main([
"numbers/currencies",
currency,
"symbol"
]);

currencySpacing = [ "beforeCurrency", "afterCurrency" ].map(function( position ) {
return cldr.main([
"numbers",
"currencyFormats-numberSystem-" + numberNumberingSystem( cldr ),
"currencySpacing",
position
]);
});

pattern = cldr.main([
"numbers",
"currencyFormats-numberSystem-" + numberNumberingSystem( cldr ),
options.style === "accounting" ? "accounting" : "standard"
]);

pattern =

// The number of decimal places and the rounding for each currency is not locale-specific.
// Those values are overridden by Supplemental Currency Data.
currencySupplementalOverride( currency, pattern, cldr )

// Replace "¤" (\u00A4) with the appropriate symbol literal.
.split( ";" ).map(function( pattern ) {

return pattern.split( "\u00A4" ).map(function( part, i ) {
var currencyMatch = regexp[ currencySpacing[ i ].currencyMatch ],
surroundingMatch = regexp[ currencySpacing[ i ].surroundingMatch ],
insertBetween = "";

// For currencyMatch and surroundingMatch definitions, read [1].
// When i === 0, beforeCurrency is being handled. Otherwise, afterCurrency.
// 1: http://www.unicode.org/reports/tr35/tr35-numbers.html#Currencies
currencyMatch = currencyMatch.test( symbol.charAt( i ? symbol.length - 1 : 0 ) );
surroundingMatch = surroundingMatch.test(
part.charAt( i ? 0 : part.length - 1 ).replace( /[#@,.]/g, "0" )
);

if ( currencyMatch && part && surroundingMatch ) {
insertBetween = currencySpacing[ i ].insertBetween;
}

return ( i ? insertBetween : "" ) + part + ( i ? "" : insertBetween );
}).join( "'" + symbol + "'" );
}).join( ";" );

return {
pattern: pattern
};
};

});
17 changes: 0 additions & 17 deletions src/util/object/flat-extend.js

This file was deleted.

5 changes: 5 additions & 0 deletions src/util/object/omit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ define([
"../always-array"
], function( alwaysArray ) {

/**
* objectOmit( object, keys )
*
* Return a copy of the object, filtered to omit the blacklisted key or array of keys.
*/
return function( object, keys ) {
var key,
copy = {};
Expand Down
21 changes: 13 additions & 8 deletions src/common/regexp-not-s.js → src/util/regexp/not-s.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require([
// currency
"./unit/currency/code-properties",
"./unit/currency/name-properties",
"./unit/currency/symbol-pattern",
"./unit/currency/symbol-properties",

"./unit/currency/name-format",

Expand Down
47 changes: 0 additions & 47 deletions test/unit/currency/symbol-pattern.js

This file was deleted.

49 changes: 49 additions & 0 deletions test/unit/currency/symbol-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
define([
"cldr",
"src/currency/symbol-properties",
"json!cldr-data/main/de/currencies.json",
"json!cldr-data/main/de/numbers.json",
"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, symbolProperties, deCurrencies, deNumbers, enCurrencies, enNumbers, zhCurrencies,
zhNumbers, currencyData, likelySubtags ) {

var de, en, zh;

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

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

QUnit.module( "Currency Symbol Properties" );

QUnit.test( "should return pattern replacing `¤` with the appropriate currency symbol literal", function( assert ) {
assert.deepEqual( symbolProperties( "USD", en, {} ), { pattern: "'$'#,##0.00" } );
assert.deepEqual( symbolProperties( "EUR", en, {} ), { pattern: "'€'#,##0.00" } );
assert.deepEqual( symbolProperties( "USD", de, {} ), { pattern: "#,##0.00 '$'" } );
assert.deepEqual( symbolProperties( "EUR", de, {} ), { pattern: "#,##0.00 '€'" } );
assert.deepEqual( symbolProperties( "USD", zh, {} ), { pattern: "'US$' #,##0.00" } );
assert.deepEqual( symbolProperties( "EUR", zh, {} ), { pattern: "'€' #,##0.00" } );

assert.deepEqual( symbolProperties( "USD", en, {
style: "accounting"
}), {
pattern: "'$'#,##0.00;('$'#,##0.00)"
});
});

});

0 comments on commit 0aadce3

Please sign in to comment.