-
Notifications
You must be signed in to change notification settings - Fork 586
Expand file tree
/
Copy pathcurrency.js
More file actions
149 lines (124 loc) · 4.26 KB
/
currency.js
File metadata and controls
149 lines (124 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
define([
"./core",
"./common/runtime-bind",
"./common/validate/cldr",
"./common/validate/default-locale",
"./common/validate/parameter-presence",
"./common/validate/parameter-type/currency",
"./common/validate/parameter-type/number",
"./common/validate/parameter-type/plain-object",
"./currency/code-properties",
"./currency/formatter-fn",
"./currency/name-properties",
"./currency/symbol-properties",
"./util/object/omit",
"./number",
"cldr/event",
"cldr/supplemental"
], function( Globalize, runtimeBind, validateCldr, validateDefaultLocale, validateParameterPresence,
validateParameterTypeCurrency, validateParameterTypeNumber, validateParameterTypePlainObject,
currencyCodeProperties, currencyFormatterFn, currencyNameProperties, currencySymbolProperties,
objectOmit ) {
function validateRequiredCldr( path, value ) {
validateCldr( path, value, {
skip: [
/numbers\/currencies\/[^/]+\/symbol-alt-/,
/supplemental\/currencyData\/fractions\/[A-Za-z]{3}$/
]
});
}
/**
* .currencyFormatter( currency [, options] )
*
* @currency [String] 3-letter currency code as defined by ISO 4217.
*
* @options [Object]:
* - style: [String] "symbol" (default), "accounting", "code" or "name".
* - see also number/format options.
*
* Return a function that formats a currency according to the given options and default/instance
* locale.
*/
Globalize.currencyFormatter =
Globalize.prototype.currencyFormatter = function( currency, options ) {
var args, cldr, numberFormatter, pluralGenerator, properties, returnFn, style;
validateParameterPresence( currency, "currency" );
validateParameterTypeCurrency( currency, "currency" );
validateParameterTypePlainObject( options, "options" );
cldr = this.cldr;
options = options || {};
args = [ currency, options ];
style = options.style || "symbol";
validateDefaultLocale( cldr );
// Get properties given style ("symbol" default, "code" or "name").
cldr.on( "get", validateRequiredCldr );
properties = ({
accounting: currencySymbolProperties,
code: currencyCodeProperties,
name: currencyNameProperties,
symbol: currencySymbolProperties
}[ style ] )( currency, cldr, options );
cldr.off( "get", validateRequiredCldr );
// options = options minus style, plus raw pattern.
options = objectOmit( options, "style" );
options.raw = properties.pattern;
// Return formatter when style is "symbol" or "accounting".
if ( style === "symbol" || style === "accounting" ) {
numberFormatter = this.numberFormatter( options );
returnFn = currencyFormatterFn( numberFormatter );
runtimeBind( args, cldr, returnFn, [ numberFormatter ] );
// Return formatter when style is "code" or "name".
} else {
numberFormatter = this.numberFormatter( options );
pluralGenerator = this.pluralGenerator();
returnFn = currencyFormatterFn( numberFormatter, pluralGenerator, properties );
runtimeBind( args, cldr, returnFn, [ numberFormatter, pluralGenerator, properties ] );
}
return returnFn;
};
/**
* .currencyParser( currency [, options] )
*
* @currency [String] 3-letter currency code as defined by ISO 4217.
*
* @options [Object] see currencyFormatter.
*
* Return the currency parser according to the given options and the default/instance locale.
*/
Globalize.currencyParser =
Globalize.prototype.currencyParser = function( /* currency, options */ ) {
// TODO implement parser.
};
/**
* .formatCurrency( value, currency [, options] )
*
* @value [Number] number to be formatted.
*
* @currency [String] 3-letter currency code as defined by ISO 4217.
*
* @options [Object] see currencyFormatter.
*
* Format a currency according to the given options and the default/instance locale.
*/
Globalize.formatCurrency =
Globalize.prototype.formatCurrency = function( value, currency, options ) {
validateParameterPresence( value, "value" );
validateParameterTypeNumber( value, "value" );
return this.currencyFormatter( currency, options )( value );
};
/**
* .parseCurrency( value, currency [, options] )
*
* @value [String]
*
* @currency [String] 3-letter currency code as defined by ISO 4217.
*
* @options [Object]: See currencyFormatter.
*
* Return the parsed currency or NaN when value is invalid.
*/
Globalize.parseCurrency =
Globalize.prototype.parseCurrency = function( /* value, currency, options */ ) {
};
return Globalize;
});