Debian Codegraph @

closure-library/closure/goog/i18n/currency.js

raw
1 // Copyright 2009 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15
16 /**
17 * @fileoverview A utility to get better currency format pattern.
18 *
19 * This module implements a new currency format representation model. It
20 * provides 3 currency representation forms: global, portable and local. Local
21 * format is the most popular format people use to represent currency in its
22 * circulating country without worrying about how it should be distinguished
23 * from other currencies. Global format is a formal representation in context
24 * of multiple currencies in same page, it is ISO 4217 currency code. Portable
25 * format is a compromise between global and local. It looks similar to how
26 * people would like to see how their currency is being represented in other
27 * media. While at the same time, it should be distinguishable to world's
28 * popular currencies (like USD, EUR) and currencies somewhat relevant in the
29 * area (like CNY in HK, though native currency is HKD). There is no guarantee
30 * of uniqueness.
31 *
32 */
33
34
35 goog.provide('goog.i18n.currency');
36 goog.provide('goog.i18n.currency.CurrencyInfo');
37 goog.provide('goog.i18n.currency.CurrencyInfoTier2');
38
39
40 /**
41 * The mask of precision field.
42 * @private
43 */
44 goog.i18n.currency.PRECISION_MASK_ = 0x07;
45
46
47 /**
48 * Whether the currency sign should be positioned after the number.
49 * @private
50 */
51 goog.i18n.currency.POSITION_FLAG_ = 0x10;
52
53
54 /**
55 * Whether a space should be inserted between the number and currency sign.
56 * @private
57 */
58 goog.i18n.currency.SPACE_FLAG_ = 0x20;
59
60
61 /**
62 * Whether tier2 was enabled already by calling addTier2Support().
63 * @private
64 */
65 goog.i18n.currency.tier2Enabled_ = false;
66
67
68 /**
69 * Tests if currency is available.
70 *
71 * Note: If the currency is not available it might be in the tier2 currency set:
72 * {@link goog.i18n.currency.CurrencyInfoTier2}. If that is the case call
73 * {@link goog.i18n.currency.addTier2Support} before calling any other function
74 * in this namespace.
75 *
76 * @param {string} currencyCode Currency code to tested.
77 * @return {boolean} If the currency is available.
78 */
79 goog.i18n.currency.isAvailable = function(currencyCode) {
80 return currencyCode in goog.i18n.currency.CurrencyInfo;
81 };
82
83 /**
84 * This function will add tier2 currency support. Be default, only tier1
85 * (most popular currencies) are supported. If an application really needs
86 * to support some of the rarely used currencies, it should call this function
87 * before any other functions in this namespace.
88 */
89 goog.i18n.currency.addTier2Support = function() {
90 // Protection from executing this these again and again.
91 if (!goog.i18n.currency.tier2Enabled_) {
92 for (var key in goog.i18n.currency.CurrencyInfoTier2) {
93 goog.i18n.currency.CurrencyInfo[key] =
94 goog.i18n.currency.CurrencyInfoTier2[key];
95 }
96 goog.i18n.currency.tier2Enabled_ = true;
97 }
98 };
99
100
101 /**
102 * Deprecated.
103 * Global currency pattern always uses ISO-4217 currency code as prefix. Local
104 * currency sign is added if it is different from currency code. Each currency
105 * is unique in this form. The negative side is that ISO code looks weird in
106 * some countries as people normally do not use it. Local currency sign
107 * alleviates the problem, but also makes it a little verbose.
108 *
109 * @param {string} currencyCode ISO-4217 3-letter currency code.
110 * @return {string} Global currency pattern string for given currency.
111 * @deprecated Format numbers using {@link goog.i18n.NumberFormat} with
112 * {@link goog.i18n.NumberFormat.Format.CURRENCY} and
113 * {@link goog.i18n.NumberFormat.CurrencyStyle.GLOBAL}
114 */
115 goog.i18n.currency.getGlobalCurrencyPattern = function(currencyCode) {
116 var info = goog.i18n.currency.CurrencyInfo[currencyCode];
117 var patternNum = info[0];
118 if (currencyCode == info[1]) {
119 return goog.i18n.currency.getCurrencyPattern_(patternNum, info[1]);
120 }
121 return currencyCode + ' ' +
122 goog.i18n.currency.getCurrencyPattern_(patternNum, info[1]);
123 };
124
125
126 /**
127 * Return global currency sign string for those applications
128 * that want to handle currency sign themselves.
129 *
130 * @param {string} currencyCode ISO-4217 3-letter currency code.
131 * @return {string} Global currency sign for given currency.
132 */
133 goog.i18n.currency.getGlobalCurrencySign = function(currencyCode) {
134 var info = goog.i18n.currency.CurrencyInfo[currencyCode];
135 return (currencyCode == info[1]) ? currencyCode :
136 currencyCode + ' ' + info[1];
137 };
138
139
140 /**
141 * Deprecated.
142 * Local currency pattern is the most frequently used pattern in currency's
143 * native region. It does not care about how it is distinguished from other
144 * currencies.
145 *
146 * @param {string} currencyCode ISO-4217 3-letter currency code.
147 * @return {string} Local currency pattern string for given currency.
148 * @deprecated Format numbers using {@link goog.i18n.NumberFormat} with
149 * {@link goog.i18n.NumberFormat.Format.CURRENCY} and
150 * {@link goog.i18n.NumberFormat.CurrencyStyle.LOCAL}
151 */
152 goog.i18n.currency.getLocalCurrencyPattern = function(currencyCode) {
153 var info = goog.i18n.currency.CurrencyInfo[currencyCode];
154 return goog.i18n.currency.getCurrencyPattern_(info[0], info[1]);
155 };
156
157
158 /**
159 * Returns local currency sign string for those applications that need to
160 * handle currency sign separately.
161 *
162 * @param {string} currencyCode ISO-4217 3-letter currency code.
163 * @return {string} Local currency sign for given currency.
164 */
165 goog.i18n.currency.getLocalCurrencySign = function(currencyCode) {
166 return goog.i18n.currency.CurrencyInfo[currencyCode][1];
167 };
168
169
170 /**
171 * Deprecated.
172 * Portable currency pattern is a compromise between local and global. It is
173 * not a mere blend or mid-way between the two. Currency sign is chosen so that
174 * it looks familiar to native users. It also has enough information to
175 * distinguish itself from other popular currencies in its native region.
176 * In this pattern, currency sign symbols that has availability problem in
177 * popular fonts are also avoided.
178 *
179 * @param {string} currencyCode ISO-4217 3-letter currency code.
180 * @return {string} Portable currency pattern string for given currency.
181 * @deprecated Format numbers using {@link goog.i18n.NumberFormat} with
182 * {@link goog.i18n.NumberFormat.Format.CURRENCY} and
183 * {@link goog.i18n.NumberFormat.CurrencyStyle.PORTABLE}
184 */
185 goog.i18n.currency.getPortableCurrencyPattern = function(currencyCode) {
186 var info = goog.i18n.currency.CurrencyInfo[currencyCode];
187 return goog.i18n.currency.getCurrencyPattern_(info[0], info[2]);
188 };
189
190
191 /**
192 * Return portable currency sign string for those applications that need to
193 * handle currency sign themselves.
194 *
195 * @param {string} currencyCode ISO-4217 3-letter currency code.
196 * @return {string} Portable currency sign for given currency.
197 */
198 goog.i18n.currency.getPortableCurrencySign = function(currencyCode) {
199 return goog.i18n.currency.CurrencyInfo[currencyCode][2];
200 };
201
202
203 /**
204 * This function returns the default currency sign's position. Some applications
205 * may want to handle currency sign and currency amount separately. This
206 * function can be used in such situations to correctly position the currency
207 * sign relative to the amount.
208 *
209 * Use {@link goog.i18n.NumberFormat#isCurrencyCodeBeforeValue} for a locale
210 * aware version of this API (recommended). isPrefixSignPosition() returns the
211 * default currency sign's position in the currency's default locale (e.g. 'en'
212 * for 'USD'), but most commonly the position is needed for the locale in which
213 * the number is going to be displayed. For example, in 'fr' 10.10 USD would be
214 * displayed as '10,10 $'.
215 *
216 * @param {string} currencyCode ISO-4217 3-letter currency code.
217 * @return {boolean} true if currency should be positioned before amount field.
218 */
219 goog.i18n.currency.isPrefixSignPosition = function(currencyCode) {
220 return (goog.i18n.currency.CurrencyInfo[currencyCode][0] &
221 goog.i18n.currency.POSITION_FLAG_) == 0;
222 };
223
224
225 /**
226 * This function constructs the currency pattern. Currency sign is provided. The
227 * pattern information is encoded in patternNum.
228 *
229 * @param {number} patternNum Encoded pattern number that has
230 * currency pattern information.
231 * @param {string} sign The currency sign that will be used in pattern.
232 * @return {string} currency pattern string.
233 * @private
234 */
235 goog.i18n.currency.getCurrencyPattern_ = function(patternNum, sign) {
236 var strParts = ['#,##0'];
237 var precision = patternNum & goog.i18n.currency.PRECISION_MASK_;
238 if (precision > 0) {
239 strParts.push('.');
240 for (var i = 0; i < precision; i++) {
241 strParts.push('0');
242 }
243 }
244 if ((patternNum & goog.i18n.currency.POSITION_FLAG_) == 0) {
245 strParts.unshift(
246 (patternNum & goog.i18n.currency.SPACE_FLAG_) ? "' " : "'");
247 strParts.unshift(sign);
248 strParts.unshift("'");
249 } else {
250 strParts.push(
251 (patternNum & goog.i18n.currency.SPACE_FLAG_) ? " '" : "'", sign, "'");
252 }
253 return strParts.join('');
254 };
255
256
257 /**
258 * Modify currency pattern string by adjusting precision for given currency.
259 * Standard currency pattern will have 2 digit after decimal point.
260 * Examples:
261 * $#,##0.00 -> $#,##0 (precision == 0)
262 * $#,##0.00 -> $#,##0.0 (precision == 1)
263 * $#,##0.00 -> $#,##0.000 (precision == 3)
264 *
265 * @param {string} pattern currency pattern string.
266 * @param {string} currencyCode 3-letter currency code.
267 * @return {string} modified currency pattern string.
268 */
269 goog.i18n.currency.adjustPrecision = function(pattern, currencyCode) {
270 var strParts = ['0'];
271 var info = goog.i18n.currency.CurrencyInfo[currencyCode];
272 var precision = info[0] & goog.i18n.currency.PRECISION_MASK_;
273 if (precision > 0) {
274 strParts.push('.');
275 for (var i = 0; i < precision; i++) {
276 strParts.push('0');
277 }
278 }
279 return pattern.replace(/0.00/g, strParts.join(''));
280 };
281
282
283 /**
284 * Tier 1 currency information.
285 *
286 * The first number in the array is a combination of the precision mask and
287 * other flags. The precision mask indicates how many decimal places to show for
288 * the currency. Valid values are [0..7]. The position flag indicates whether
289 * the currency sign should be positioned after the number. Valid values are 0
290 * (before the number) or 16 (after the number). The space flag indicates
291 * whether a space should be inserted between the currency sign and number.
292 * Valid values are 0 (no space) and 32 (space).
293 *
294 * The number in the array is calculated by adding together the mask and flag
295 * values. For example:
296 *
297 * 0: no precision (0), currency sign first (0), no space (0)
298 * 2: two decimals precision (2), currency sign first (0), no space (0)
299 * 18: two decimals precision (2), currency sign last (16), no space (0)
300 * 50: two decimals precision (2), currency sign last (16), space (32)
301 *
302 * It's not recommended to read this data directly. Format numbers using
303 * {@link goog.i18n.NumberFormat} with
304 * {@link goog.i18n.NumberFormat.Format.CURRENCY} instead.
305 *
306 * @const {!Object<!Array<?>>}
307 */
308 goog.i18n.currency.CurrencyInfo = {
309 'AED': [2, 'dh', '\u062f.\u0625.', 'DH'],
310 'ALL': [0, 'Lek', 'Lek'],
311 'AUD': [2, '$', 'AU$'],
312 'BDT': [2, '\u09F3', 'Tk'],
313 'BGN': [2, 'lev', 'lev'],
314 'BRL': [2, 'R$', 'R$'],
315 'CAD': [2, '$', 'C$'],
316 'CDF': [2, 'FrCD', 'CDF'],
317 'CHF': [2, 'CHF', 'CHF'],
318 'CLP': [0, '$', 'CL$'],
319 'CNY': [2, '¥', 'RMB¥'],
320 'COP': [32, '$', 'COL$'],
321 'CRC': [0, '\u20a1', 'CR\u20a1'],
322 'CZK': [50, 'K\u010d', 'K\u010d'],
323 'DKK': [50, 'kr.', 'kr.'],
324 'DOP': [2, 'RD$', 'RD$'],
325 'EGP': [2, '£', 'LE'],
326 'ETB': [2, 'Birr', 'Birr'],
327 'EUR': [2, '€', '€'],
328 'GBP': [2, '£', 'GB£'],
329 'HKD': [2, '$', 'HK$'],
330 'HRK': [2, 'kn', 'kn'],
331 'HUF': [34, 'Ft', 'Ft'],
332 'IDR': [0, 'Rp', 'Rp'],
333 'ILS': [34, '\u20AA', 'IL\u20AA'],
334 'INR': [2, '\u20B9', 'Rs'],
335 'IRR': [0, 'Rial', 'IRR'],
336 'ISK': [0, 'kr', 'kr'],
337 'JMD': [2, '$', 'JA$'],
338 'JPY': [0, '¥', 'JP¥'],
339 'KRW': [0, '\u20A9', 'KR₩'],
340 'LKR': [2, 'Rs', 'SLRs'],
341 'LTL': [2, 'Lt', 'Lt'],
342 'MNT': [0, '\u20AE', 'MN₮'],
343 'MVR': [2, 'Rf', 'MVR'],
344 'MXN': [2, '$', 'Mex$'],
345 'MYR': [2, 'RM', 'RM'],
346 'NOK': [50, 'kr', 'NOkr'],
347 'PAB': [2, 'B/.', 'B/.'],
348 'PEN': [2, 'S/.', 'S/.'],
349 'PHP': [2, '\u20B1', 'PHP'],
350 'PKR': [0, 'Rs', 'PKRs.'],
351 'PLN': [50, 'z\u0142', 'z\u0142'],
352 'RON': [2, 'RON', 'RON'],
353 'RSD': [0, 'din', 'RSD'],
354 'RUB': [50, '\u20bd', 'RUB'],
355 'SAR': [2, 'Rial', 'Rial'],
356 'SEK': [50, 'kr', 'kr'],
357 'SGD': [2, '$', 'S$'],
358 'THB': [2, '\u0e3f', 'THB'],
359 'TRY': [2, '₺', 'TRY'],
360 'TWD': [2, 'NT$', 'NT$'],
361 'TZS': [0, 'TSh', 'TSh'],
362 'UAH': [2, 'грн.', 'UAH'],
363 'USD': [2, '$', 'US$'],
364 'UYU': [2, '$', '$U'],
365 'VND': [48, '\u20AB', 'VN\u20AB'],
366 'YER': [0, 'Rial', 'Rial'],
367 'ZAR': [2, 'R', 'ZAR']
368 };
369
370
371 /**
372 * Tier 2 currency information.
373 *
374 * It's not recommended to read this data directly. Format numbers using
375 * {@link goog.i18n.NumberFormat} with
376 * {@link goog.i18n.NumberFormat.Format.CURRENCY} instead.
377 *
378 * @const {!Object<!Array<?>>}
379 */
380 goog.i18n.currency.CurrencyInfoTier2 = {
381 'AFN': [48, 'Af.', 'AFN'],
382 'AMD': [32, 'Dram', 'dram'],
383 'ANG': [2, 'NAf.', 'ANG'],
384 'AOA': [2, 'Kz', 'Kz'],
385 'ARS': [34, '$', 'AR$'],
386 'AWG': [2, 'Afl.', 'Afl.'],
387 'AZN': [34, '\u20bc', 'AZN'],
388 'BAM': [2, 'KM', 'KM'],
389 'BBD': [2, '$', 'Bds$'],
390 'BHD': [3, 'din', 'din'],
391 'BIF': [0, 'FBu', 'FBu'],
392 'BMD': [2, '$', 'BD$'],
393 'BND': [2, '$', 'B$'],
394 'BOB': [2, 'Bs', 'Bs'],
395 'BSD': [2, '$', 'BS$'],
396 'BTN': [2, 'Nu.', 'Nu.'],
397 'BWP': [2, 'P', 'pula'],
398 'BYN': [50, '\u0440.', 'BYN'],
399 'BYR': [48, '\u0440.', 'BYR'],
400 'BZD': [2, '$', 'BZ$'],
401 'CNH': [2, '¥', 'RMB¥'],
402 'CUC': [1, '$', 'CUC$'],
403 'CUP': [2, '$', 'CU$'],
404 'CVE': [2, 'CVE', 'Esc'],
405 'DJF': [0, 'Fdj', 'Fdj'],
406 'DZD': [2, 'din', 'din'],
407 'ERN': [2, 'Nfk', 'Nfk'],
408 'FJD': [2, '$', 'FJ$'],
409 'FKP': [2, '£', 'FK£'],
410 'GEL': [2, 'GEL', 'GEL'],
411 'GHS': [2, 'GHS', 'GHS'],
412 'GIP': [2, '£', 'GI£'],
413 'GMD': [2, 'GMD', 'GMD'],
414 'GNF': [0, 'FG', 'FG'],
415 'GTQ': [2, 'Q', 'GTQ'],
416 'GYD': [0, '$', 'GY$'],
417 'HNL': [2, 'L', 'HNL'],
418 'HTG': [2, 'HTG', 'HTG'],
419 'IQD': [0, 'din', 'IQD'],
420 'JOD': [3, 'din', 'JOD'],
421 'KES': [2, 'Ksh', 'Ksh'],
422 'KGS': [2, 'KGS', 'KGS'],
423 'KHR': [2, 'Riel', 'KHR'],
424 'KMF': [0, 'CF', 'KMF'],
425 'KPW': [0, '\u20A9KP', 'KPW'],
426 'KWD': [3, 'din', 'KWD'],
427 'KYD': [2, '$', 'KY$'],
428 'KZT': [2, '\u20B8', 'KZT'],
429 'LAK': [0, '\u20AD', '\u20AD'],
430 'LBP': [0, 'L£', 'LBP'],
431 'LRD': [2, '$', 'L$'],
432 'LSL': [2, 'LSL', 'LSL'],
433 'LYD': [3, 'din', 'LD'],
434 'MAD': [2, 'dh', 'MAD'],
435 'MDL': [2, 'MDL', 'MDL'],
436 'MGA': [0, 'Ar', 'MGA'],
437 'MKD': [2, 'din', 'MKD'],
438 'MMK': [0, 'K', 'MMK'],
439 'MOP': [2, 'MOP', 'MOP$'],
440 'MRO': [0, 'MRO', 'MRO'],
441 'MUR': [0, 'MURs', 'MURs'],
442 'MWK': [2, 'MWK', 'MWK'],
443 'MZN': [2, 'MTn', 'MTn'],
444 'NAD': [2, '$', 'N$'],
445 'NGN': [2, '\u20A6', 'NG\u20A6'],
446 'NIO': [2, 'C$', 'C$'],
447 'NPR': [2, 'Rs', 'NPRs'],
448 'NZD': [2, '$', 'NZ$'],
449 'OMR': [3, 'Rial', 'OMR'],
450 'PGK': [2, 'PGK', 'PGK'],
451 'PYG': [16, 'Gs.', 'PYG'],
452 'QAR': [2, 'Rial', 'QR'],
453 'RWF': [0, 'RF', 'RF'],
454 'SBD': [2, '$', 'SI$'],
455 'SCR': [2, 'SCR', 'SCR'],
456 'SDG': [2, 'SDG', 'SDG'],
457 'SHP': [2, '£', 'SH£'],
458 'SLL': [0, 'SLL', 'SLL'],
459 'SOS': [0, 'SOS', 'SOS'],
460 'SRD': [2, '$', 'SR$'],
461 'SSP': [2, '£', 'SSP'],
462 'STD': [0, 'Db', 'Db'],
463 'SYP': [0, '£', 'SY£'],
464 'SZL': [2, 'SZL', 'SZL'],
465 'TJS': [2, 'Som', 'TJS'],
466 'TND': [3, 'din', 'DT'],
467 'TOP': [2, 'T$', 'T$'],
468 'TTD': [2, '$', 'TT$'],
469 'UGX': [0, 'UGX', 'UGX'],
470 'UZS': [0, 'so\u02bcm', 'UZS'],
471 'VEF': [2, 'Bs', 'Bs'],
472 'VUV': [0, 'VUV', 'VUV'],
473 'WST': [2, 'WST', 'WST'],
474 'XAF': [0, 'FCFA', 'FCFA'],
475 'XCD': [2, '$', 'EC$'],
476 'XOF': [0, 'CFA', 'CFA'],
477 'XPF': [48, 'FCFP', 'FCFP'],
478 'ZMW': [0, 'ZMW', 'ZMW'],
479 'ZWD': [0, '$', 'Z$']
480 };