Skip to content
Permalink
Browse files

Currency: support symbol-alt-narrow (2/2)

Fixes #479
Closes #631
Ref #738
  • Loading branch information...
rxaviers committed May 29, 2017
1 parent 2c69ac8 commit fe246398b92f654da739d8904d93fee1b0a9cab5
@@ -8,11 +8,11 @@ The returned function is invoked with one argument: the Number `value` to be for

#### currency

3-letter currency code as defined by ISO 4217, eg. `USD`.
3-letter currency code as defined by ISO 4217, eg. `"USD"`.

#### options.style

Optional. String `symbol` (default), `accounting`, `code` or `name`. See [`.numberFormatter( [options] )`](../number/number-formatter.md) for more options.
Optional. String `"symbol"` (default), `"accounting"`, `"code"` or `"name"`. See [`.numberFormatter( [options] )`](../number/number-formatter.md) for more options.

#### value

@@ -26,6 +26,8 @@ Prior to using any currency methods, you must load `cldr/main/{locale}/currencie

[CLDR content]: ../../../README.md#2-cldr-content

#### Using the default options

You can use the static method `Globalize.currencyFormatter()`, which uses the default locale.

```javascript
@@ -36,7 +38,6 @@ formatter = Globalize.currencyFormatter( "USD" );
formatter( 9.99 );
// > "$9.99"
```

#### Instance Formatter
@@ -66,6 +67,18 @@ For comparison, follow the formatting output of different symbols in different l
| `.currencyFormatter( "GBP" )( 1 )` | `£1.00` | `1,00 £` | `£ 1.00` |
| `.currencyFormatter( "BRL" )( 1 )` | `R$1.00` | `1,00 R$` | `R$ 1.00` |

#### Using alternative `options.symbolForm`

Using the narrow symbol form, the same symbols may be used for multiple currencies. Thus the symbol may be ambiguous, and should only be used where the context is clear.

```js
Globalize( "en" ).currencyFormatter( "HKD" )( 1 );
// > "HK$1.00"
Globalize( "en" ).currencyFormatter( "HKD", { symbolForm: "narrow" } )( 1 );
// > "$1.00"
```

#### Configuring style

For the accounting variation of the symbol format, use `style: "accounting"`.
@@ -23,7 +23,10 @@ define([

function validateRequiredCldr( path, value ) {
validateCldr( path, value, {
skip: [ /supplemental\/currencyData\/fractions\/[A-Za-z]{3}$/ ]
skip: [
/numbers\/currencies\/[^/]+\/symbol-alt-/,
/supplemental\/currencyData\/fractions\/[A-Za-z]{3}$/
]
});
}

@@ -10,16 +10,25 @@ define([
* Return pattern replacing `¤` with the appropriate currency symbol literal.
*/
return function( currency, cldr, options ) {
var currencySpacing, pattern,
var currencySpacing, pattern, symbol,
symbolEntries = [ "symbol" ],
regexp = {
"[:digit:]": /\d/,
"[:^S:]": regexpNotS
},
currencySymbols = cldr.main([
};

// If options.symbolForm === "narrow" was passed, prepend it.
if ( options.symbolForm === "narrow" ) {
symbolEntries.unshift( "symbol-alt-narrow" );
}

symbolEntries.some(function( symbolEntry ) {
return symbol = cldr.main([
"numbers/currencies",
currency
]),
symbol = currencySymbols[options.symbolForm || "symbol"] || currencySymbols.symbol;
currency,
symbolEntry
]);
});

currencySpacing = [ "beforeCurrency", "afterCurrency" ].map(function( position ) {
return cldr.main([
@@ -19,8 +19,8 @@ define([

var accounting = { style: "accounting" },
code = { style: "code" },
name = { style: "name" },
narrow = { symbolForm: "symbol-alt-narrow" },
name = { style: "name" },
narrow = { symbolForm: "narrow" },
teslaS = 69900;

function extraSetup() {
@@ -89,6 +89,8 @@ 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( "HKD", narrow )( teslaS ), "$69,900.00" );

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" );
@@ -100,15 +102,6 @@ QUnit.test( "should return a currency formatter", function( assert ) {
assert.equal( Globalize.currencyFormatter( "USD", accounting )( -1 ), "($1.00)" );
});

QUnit.test( "should use the supplied symbolForm, falling back to standard if none is found", function( assert ) {

extraSetup();

assert.equal( Globalize.currencyFormatter( "HKD", narrow )( teslaS ), "$69,900.00" );
assert.equal( Globalize.currencyFormatter( "CHF", narrow )( teslaS ), "CHF 69,900.00" );

});

// The number of decimal places and the rounding for each currency is not locale-specific data.
// Those values are overriden by Supplemental Currency Data.
QUnit.test( "should return a currency formatter, overriden by Supplemental Currency Data",
@@ -42,6 +42,7 @@ QUnit.test( "should return pattern replacing `¤` with the appropriate currency
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( "RUB", en, {} ), { pattern: "'RUB' #,##0.00" } );

assert.deepEqual( symbolProperties( "USD", en, {
style: "accounting"
@@ -50,4 +51,19 @@ QUnit.test( "should return pattern replacing `¤` with the appropriate currency
});
});

QUnit.test( "Should use the supplied symbolForm, falling back to standard if none is found", function( assert ) {
assert.deepEqual(
symbolProperties( "RUB", en, { symbolForm: "narrow" } ),
{ pattern: "'₽'#,##0.00" }
);
assert.deepEqual(
symbolProperties( "HKD", en, { symbolForm: "narrow" } ),
{ pattern: "'$'#,##0.00" }
);
assert.deepEqual(
symbolProperties( "CHF", en, { symbolForm: "narrow" } ),
{ pattern: "'CHF' #,##0.00" }
);
});

});

0 comments on commit fe24639

Please sign in to comment.
You can’t perform that action at this time.