Skip to content

Commit 5ed4cf6

Browse files
committed
Currency: support symbol-alt-narrow (2/2)
Fixes globalizejs#479 Closes globalizejs#631 Ref globalizejs#738
1 parent 0326be7 commit 5ed4cf6

File tree

5 files changed

+55
-21
lines changed

5 files changed

+55
-21
lines changed

doc/api/currency/currency-formatter.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ The returned function is invoked with one argument: the Number `value` to be for
88

99
#### currency
1010

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

1313
#### options.style
1414

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

1717
#### value
1818

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

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

29+
#### Using the default options
30+
2931
You can use the static method `Globalize.currencyFormatter()`, which uses the default locale.
3032

3133
```javascript
@@ -36,7 +38,6 @@ formatter = Globalize.currencyFormatter( "USD" );
3638

3739
formatter( 9.99 );
3840
// > "$9.99"
39-
4041
```
4142

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

70+
#### Using alternative `options.symbolForm`
71+
72+
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.
73+
74+
```js
75+
Globalize( "en" ).currencyFormatter( "HKD" )( 1 );
76+
// > "HK$1.00"
77+
78+
Globalize( "en" ).currencyFormatter( "HKD", { symbolForm: "narrow" } )( 1 );
79+
// > "$1.00"
80+
```
81+
6982
#### Configuring style
7083

7184
For the accounting variation of the symbol format, use `style: "accounting"`.

src/currency.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ define([
2323

2424
function validateRequiredCldr( path, value ) {
2525
validateCldr( path, value, {
26-
skip: [ /supplemental\/currencyData\/fractions\/[A-Za-z]{3}$/ ]
26+
skip: [
27+
/numbers\/currencies\/[^/]+\/symbol-alt-/,
28+
/supplemental\/currencyData\/fractions\/[A-Za-z]{3}$/
29+
]
2730
});
2831
}
2932

src/currency/symbol-properties.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,25 @@ define([
1010
* Return pattern replacing `¤` with the appropriate currency symbol literal.
1111
*/
1212
return function( currency, cldr, options ) {
13-
var currencySpacing, pattern,
13+
var currencySpacing, pattern, symbol,
14+
symbolEntries = [ "symbol" ],
1415
regexp = {
1516
"[:digit:]": /\d/,
1617
"[:^S:]": regexpNotS
17-
},
18-
currencySymbols = cldr.main([
18+
};
19+
20+
// If options.symbolForm === "narrow" was passed, prepend it.
21+
if ( options.symbolForm === "narrow" ) {
22+
symbolEntries.unshift( "symbol-alt-narrow" );
23+
}
24+
25+
symbolEntries.some(function( symbolEntry ) {
26+
return symbol = cldr.main([
1927
"numbers/currencies",
20-
currency
21-
]),
22-
symbol = currencySymbols[options.symbolForm || "symbol"] || currencySymbols.symbol;
28+
currency,
29+
symbolEntry
30+
]);
31+
});
2332

2433
currencySpacing = [ "beforeCurrency", "afterCurrency" ].map(function( position ) {
2534
return cldr.main([

test/functional/currency/currency-formatter.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ define([
1919

2020
var accounting = { style: "accounting" },
2121
code = { style: "code" },
22-
name = { style: "name" },
23-
narrow = { symbolForm: "symbol-alt-narrow" },
22+
name = { style: "name" },
23+
narrow = { symbolForm: "narrow" },
2424
teslaS = 69900;
2525

2626
function extraSetup() {
@@ -89,6 +89,8 @@ QUnit.test( "should return a currency formatter", function( assert ) {
8989
assert.equal( de.currencyFormatter( "USD" )( -teslaS ), "-69.900,00 $" );
9090
assert.equal( zh.currencyFormatter( "USD" )( -teslaS ), "-US$69,900.00" );
9191

92+
assert.equal( Globalize.currencyFormatter( "HKD", narrow )( teslaS ), "$69,900.00" );
93+
9294
assert.equal( Globalize.currencyFormatter( "USD", code )( teslaS ), "69,900.00 USD" );
9395
assert.equal( de.currencyFormatter( "USD", code )( teslaS ), "69.900,00 USD" );
9496
assert.equal( zh.currencyFormatter( "USD", code )( teslaS ), "69,900.00USD" );
@@ -100,15 +102,6 @@ QUnit.test( "should return a currency formatter", function( assert ) {
100102
assert.equal( Globalize.currencyFormatter( "USD", accounting )( -1 ), "($1.00)" );
101103
});
102104

103-
QUnit.test( "should use the supplied symbolForm, falling back to standard if none is found", function( assert ) {
104-
105-
extraSetup();
106-
107-
assert.equal( Globalize.currencyFormatter( "HKD", narrow )( teslaS ), "$69,900.00" );
108-
assert.equal( Globalize.currencyFormatter( "CHF", narrow )( teslaS ), "CHF 69,900.00" );
109-
110-
});
111-
112105
// The number of decimal places and the rounding for each currency is not locale-specific data.
113106
// Those values are overriden by Supplemental Currency Data.
114107
QUnit.test( "should return a currency formatter, overriden by Supplemental Currency Data",

test/unit/currency/symbol-properties.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ QUnit.test( "should return pattern replacing `¤` with the appropriate currency
4242
assert.deepEqual( symbolProperties( "EUR", de, {} ), { pattern: "#,##0.00 '€'" } );
4343
assert.deepEqual( symbolProperties( "USD", zh, {} ), { pattern: "'US$'#,##0.00" } );
4444
assert.deepEqual( symbolProperties( "EUR", zh, {} ), { pattern: "'€'#,##0.00" } );
45+
assert.deepEqual( symbolProperties( "RUB", en, {} ), { pattern: "'RUB' #,##0.00" } );
4546

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

54+
QUnit.test( "Should use the supplied symbolForm, falling back to standard if none is found", function( assert ) {
55+
assert.deepEqual(
56+
symbolProperties( "RUB", en, { symbolForm: "narrow" } ),
57+
{ pattern: "'₽'#,##0.00" }
58+
);
59+
assert.deepEqual(
60+
symbolProperties( "HKD", en, { symbolForm: "narrow" } ),
61+
{ pattern: "'$'#,##0.00" }
62+
);
63+
assert.deepEqual(
64+
symbolProperties( "CHF", en, { symbolForm: "narrow" } ),
65+
{ pattern: "'CHF' #,##0.00" }
66+
);
67+
});
68+
5369
});

0 commit comments

Comments
 (0)