Skip to content

Commit

Permalink
Currency: support symbol-alt-narrow (2/2)
Browse files Browse the repository at this point in the history
Fixes #479
Closes #631
Ref #738
  • Loading branch information
rxaviers committed Jul 17, 2018
1 parent 2c69ac8 commit fe24639
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
19 changes: 16 additions & 3 deletions doc/api/currency/currency-formatter.md
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -36,7 +38,6 @@ formatter = Globalize.currencyFormatter( "USD" );

formatter( 9.99 );
// > "$9.99"

```

#### Instance Formatter
Expand Down Expand Up @@ -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"`.
Expand Down
5 changes: 4 additions & 1 deletion src/currency.js
Expand Up @@ -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}$/
]
});
}

Expand Down
21 changes: 15 additions & 6 deletions src/currency/symbol-properties.js
Expand Up @@ -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([
Expand Down
15 changes: 4 additions & 11 deletions test/functional/currency/currency-formatter.js
Expand Up @@ -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() {
Expand Down Expand Up @@ -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" );
Expand All @@ -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",
Expand Down
16 changes: 16 additions & 0 deletions test/unit/currency/symbol-properties.js
Expand Up @@ -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"
Expand All @@ -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.