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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*!
* Globalize v1.2.3
*
* http://github.com/jquery/globalize
*
* Copyright jQuery Foundation and other contributors
* Released under the MIT license
* http://jquery.org/license
*
* Date: 2017-03-17T01:41Z
*/
(function( root, factory ) {
// UMD returnExports
if ( typeof define === "function" && define.amd ) {
// AMD
define([
"cldr",
"../globalize",
"./number",
"cldr/event",
"cldr/supplemental"
], factory );
} else if ( typeof exports === "object" ) {
// Node, CommonJS
module.exports = factory( require( "cldrjs" ), require( "../globalize" ) );
} else {
// Global
factory( root.Cldr, root.Globalize );
}
}(this, function( Cldr, Globalize ) {
var alwaysArray = Globalize._alwaysArray,
formatMessage = Globalize._formatMessage,
numberNumberingSystem = Globalize._numberNumberingSystem,
numberPattern = Globalize._numberPattern,
runtimeBind = Globalize._runtimeBind,
stringPad = Globalize._stringPad,
validateCldr = Globalize._validateCldr,
validateDefaultLocale = Globalize._validateDefaultLocale,
validateParameterPresence = Globalize._validateParameterPresence,
validateParameterType = Globalize._validateParameterType,
validateParameterTypeNumber = Globalize._validateParameterTypeNumber,
validateParameterTypePlainObject = Globalize._validateParameterTypePlainObject;
var validateParameterTypeCurrency = function( value, name ) {
validateParameterType(
value,
name,
value === undefined || typeof value === "string" && ( /^[A-Za-z]{3}$/ ).test( value ),
"3-letter currency code string as defined by ISO 4217"
);
};
/**
* supplementalOverride( currency, pattern, cldr )
*
* Return pattern with fraction digits overriden by supplemental currency data.
*/
var currencySupplementalOverride = function( currency, pattern, cldr ) {
var digits,
fraction = "",
fractionData = cldr.supplemental([ "currencyData/fractions", currency ]) ||
cldr.supplemental( "currencyData/fractions/DEFAULT" );
digits = +fractionData._digits;
if ( digits ) {
fraction = "." + stringPad( "0", digits ).slice( 0, -1 ) + fractionData._rounding;
}
return pattern.replace( /\.(#+|0*[0-9]|0+[0-9]?)/g, fraction );
};
var objectFilter = function( object, testRe ) {
var key,
copy = {};
for ( key in object ) {
if ( testRe.test( key ) ) {
copy[ key ] = object[ key ];
}
}
return copy;
};
var currencyUnitPatterns = function( cldr ) {
return objectFilter( cldr.main([
"numbers",
"currencyFormats-numberSystem-" + numberNumberingSystem( cldr )
]), /^unitPattern/ );
};
/**
* codeProperties( currency, cldr )
*
* Return number pattern with the appropriate currency code in as literal.
*/
var currencyCodeProperties = function( currency, cldr ) {
var pattern = numberPattern( "decimal", cldr );
// The number of decimal places and the rounding for each currency is not locale-specific. Those
// values overridden by Supplemental Currency Data.
pattern = currencySupplementalOverride( currency, pattern, cldr );
return {
currency: currency,
pattern: pattern,
unitPatterns: currencyUnitPatterns( cldr )
};
};
/**
* nameFormat( formattedNumber, pluralForm, properties )
*
* Return the appropriate name form currency format.
*/
var currencyNameFormat = function( formattedNumber, pluralForm, properties ) {
var displayName, unitPattern,
displayNames = properties.displayNames || {},
unitPatterns = properties.unitPatterns;
displayName = displayNames[ "displayName-count-" + pluralForm ] ||
displayNames[ "displayName-count-other" ] ||
displayNames.displayName ||
properties.currency;
unitPattern = unitPatterns[ "unitPattern-count-" + pluralForm ] ||
unitPatterns[ "unitPattern-count-other" ];
return formatMessage( unitPattern, [ formattedNumber, displayName ]);
};
var currencyFormatterFn = function( numberFormatter, pluralGenerator, properties ) {
var fn;
// Return formatter when style is "code" or "name".
if ( pluralGenerator && properties ) {
fn = function currencyFormatter( value ) {
validateParameterPresence( value, "value" );
validateParameterTypeNumber( value, "value" );
return currencyNameFormat(
numberFormatter( value ),
pluralGenerator( value ),
properties
);
};
// Return formatter when style is "symbol" or "accounting".
} else {
fn = function currencyFormatter( value ) {
return numberFormatter( value );
};
}
return fn;
};
/**
* nameProperties( currency, cldr )
*
* Return number pattern with the appropriate currency code in as literal.
*/
var currencyNameProperties = function( currency, cldr ) {
var properties = currencyCodeProperties( currency, cldr );
properties.displayNames = objectFilter( cldr.main([
"numbers/currencies",
currency
]), /^displayName/ );
return properties;
};
/**
* Unicode regular expression for: everything except math symbols, currency signs, dingbats, and
* box-drawing characters.
*
* Generated by:
*
* regenerate()
* .addRange( 0x0, 0x10FFFF )
* .remove( require( "unicode-7.0.0/categories/S/symbols" ) ).toString();
*
* https://github.com/mathiasbynens/regenerate
* https://github.com/mathiasbynens/unicode-7.0.0
*/
var regexpNotS = /[\0-#%-\*,-;\?-\]_a-\{\}\x7F-\xA1\xA7\xAA\xAB\xAD\xB2\xB3\xB5-\xB7\xB9-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0300-\u0374\u0376-\u0383\u0386-\u03F5\u03F7-\u0481\u0483-\u058C\u0590-\u0605\u0609\u060A\u060C\u060D\u0610-\u06DD\u06DF-\u06E8\u06EA-\u06FC\u06FF-\u07F5\u07F7-\u09F1\u09F4-\u09F9\u09FC-\u0AF0\u0AF2-\u0B6F\u0B71-\u0BF2\u0BFB-\u0C7E\u0C80-\u0D78\u0D7A-\u0E3E\u0E40-\u0F00\u0F04-\u0F12\u0F14\u0F18\u0F19\u0F20-\u0F33\u0F35\u0F37\u0F39-\u0FBD\u0FC6\u0FCD\u0FD0-\u0FD4\u0FD9-\u109D\u10A0-\u138F\u139A-\u17DA\u17DC-\u193F\u1941-\u19DD\u1A00-\u1B60\u1B6B-\u1B73\u1B7D-\u1FBC\u1FBE\u1FC2-\u1FCC\u1FD0-\u1FDC\u1FE0-\u1FEC\u1FF0-\u1FFC\u1FFF-\u2043\u2045-\u2051\u2053-\u2079\u207D-\u2089\u208D-\u209F\u20BE-\u20FF\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2150-\u218F\u2308-\u230B\u2329\u232A\u23FB-\u23FF\u2427-\u243F\u244B-\u249B\u24EA-\u24FF\u2768-\u2793\u27C5\u27C6\u27E6-\u27EF\u2983-\u2998\u29D8-\u29DB\u29FC\u29FD\u2B74\u2B75\u2B96\u2B97\u2BBA-\u2BBC\u2BC9\u2BD2-\u2CE4\u2CEB-\u2E7F\u2E9A\u2EF4-\u2EFF\u2FD6-\u2FEF\u2FFC-\u3003\u3005-\u3011\u3014-\u301F\u3021-\u3035\u3038-\u303D\u3040-\u309A\u309D-\u318F\u3192-\u3195\u31A0-\u31BF\u31E4-\u31FF\u321F-\u3229\u3248-\u324F\u3251-\u325F\u3280-\u3289\u32B1-\u32BF\u32FF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DEFF\uDF2D-\uDF2F\uDF7E\uDF7F\uDFCF-\uDFD3\uDFF8-\uDFFF]|\uD83D[\uDCFF\uDD4B-\uDD4F\uDD7A\uDDA4\uDE43\uDE44\uDED0-\uDEDF\uDEED-\uDEEF\uDEF4-\uDEFF\uDF74-\uDF7F\uDFD5-\uDFFF]|\uD83E[\uDC0C-\uDC0F\uDC48-\uDC4F\uDC5A-\uDC5F\uDC88-\uDC8F\uDCAE-\uDFFF]|[\uD800-\uDBFF]/;
/**
* symbolProperties( currency, cldr )
*
* Return pattern replacing `¤` with the appropriate currency symbol literal.
*/
var currencySymbolProperties = 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
};
};
/**
* objectOmit( object, keys )
*
* Return a copy of the object, filtered to omit the blacklisted key or array of keys.
*/
var objectOmit = function( object, keys ) {
var key,
copy = {};
keys = alwaysArray( keys );
for ( key in object ) {
if ( keys.indexOf( key ) === -1 ) {
copy[ key ] = object[ key ];
}
}
return copy;
};
function validateRequiredCldr( path, value ) {
validateCldr( path, value, {
skip: [ /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;
}));