]> git.wh0rd.org - tt-rss.git/blob - lib/dojo/currency.js
build custom layer of Dojo to speed up loading of tt-rss (refs #293)
[tt-rss.git] / lib / dojo / currency.js
1 /*
2 Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
5 */
6
7
8 if(!dojo._hasResource["dojo.currency"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9 dojo._hasResource["dojo.currency"] = true;
10 dojo.provide("dojo.currency");
11
12 dojo.require("dojo.number");
13 dojo.require("dojo.i18n");
14 dojo.requireLocalization("dojo.cldr", "currency", null, "ROOT,ar,ca,cs,da,de,el,en,en-au,en-ca,es,fi,fr,he,hu,it,ja,ko,nb,nl,pl,pt,ru,sk,sl,sv,th,tr,zh,zh-tw");
15 dojo.require("dojo.cldr.monetary");
16
17 /*=====
18 dojo.currency = {
19 // summary: localized formatting and parsing routines for currencies
20 //
21 // description: extends dojo.number to provide culturally-appropriate formatting of values
22 // in various world currencies, including use of a currency symbol. The currencies are specified
23 // by a three-letter international symbol in all uppercase, and support for the currencies is
24 // provided by the data in `dojo.cldr`. The scripts generating dojo.cldr specify which
25 // currency support is included. A fixed number of decimal places is determined based
26 // on the currency type and is not determined by the 'pattern' argument. The fractional
27 // portion is optional, by default, and variable length decimals are not supported.
28 }
29 =====*/
30
31 dojo.currency._mixInDefaults = function(options){
32 options = options || {};
33 options.type = "currency";
34
35 // Get locale-dependent currency data, like the symbol
36 var bundle = dojo.i18n.getLocalization("dojo.cldr", "currency", options.locale) || {};
37
38 // Mixin locale-independent currency data, like # of places
39 var iso = options.currency;
40 var data = dojo.cldr.monetary.getData(iso);
41
42 dojo.forEach(["displayName","symbol","group","decimal"], function(prop){
43 data[prop] = bundle[iso+"_"+prop];
44 });
45
46 data.fractional = [true, false];
47
48 // Mixin with provided options
49 return dojo.mixin(data, options);
50 }
51
52 /*=====
53 dojo.declare("dojo.currency.__FormatOptions", [dojo.number.__FormatOptions], {
54 // type: String?
55 // Should not be set. Value is assumed to be "currency".
56 // symbol: String?
57 // localized currency symbol. The default will be looked up in table of supported currencies in `dojo.cldr`
58 // A [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code will be used if not found.
59 // currency: String?
60 // an [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code, a three letter sequence like "USD".
61 // For use with dojo.currency only.
62 // places: Number?
63 // number of decimal places to show. Default is defined based on which currency is used.
64 type: "",
65 symbol: "",
66 currency: "",
67 places: ""
68 });
69 =====*/
70
71 dojo.currency.format = function(/*Number*/value, /*dojo.currency.__FormatOptions?*/options){
72 // summary:
73 // Format a Number as a currency, using locale-specific settings
74 //
75 // description:
76 // Create a string from a Number using a known, localized pattern.
77 // [Formatting patterns](http://www.unicode.org/reports/tr35/#Number_Elements)
78 // appropriate to the locale are chosen from the [CLDR](http://unicode.org/cldr)
79 // as well as the appropriate symbols and delimiters and number of decimal places.
80 //
81 // value:
82 // the number to be formatted.
83
84 return dojo.number.format(value, dojo.currency._mixInDefaults(options));
85 }
86
87 dojo.currency.regexp = function(/*dojo.number.__RegexpOptions?*/options){
88 //
89 // summary:
90 // Builds the regular needed to parse a currency value
91 //
92 // description:
93 // Returns regular expression with positive and negative match, group and decimal separators
94 // Note: the options.places default, the number of decimal places to accept, is defined by the currency type.
95 return dojo.number.regexp(dojo.currency._mixInDefaults(options)); // String
96 }
97
98 /*=====
99 dojo.declare("dojo.currency.__ParseOptions", [dojo.number.__ParseOptions], {
100 // type: String?
101 // Should not be set. Value is assumed to be currency.
102 // currency: String?
103 // an [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code, a three letter sequence like "USD".
104 // For use with dojo.currency only.
105 // symbol: String?
106 // localized currency symbol. The default will be looked up in table of supported currencies in `dojo.cldr`
107 // A [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code will be used if not found.
108 // places: Number?
109 // fixed number of decimal places to accept. The default is determined based on which currency is used.
110 // fractional: Boolean?|Array?
111 // Whether to include the fractional portion, where the number of decimal places are implied by the currency
112 // or explicit 'places' parameter. The value [true,false] makes the fractional portion optional.
113 // By default for currencies, it the fractional portion is optional.
114 type: "",
115 currency: "",
116 symbol: "",
117 places: "",
118 fractional: ""
119 });
120 =====*/
121
122 dojo.currency.parse = function(/*String*/expression, /*dojo.currency.__ParseOptions?*/options){
123 //
124 // summary:
125 // Convert a properly formatted currency string to a primitive Number,
126 // using locale-specific settings.
127 //
128 // description:
129 // Create a Number from a string using a known, localized pattern.
130 // [Formatting patterns](http://www.unicode.org/reports/tr35/#Number_Format_Patterns)
131 // are chosen appropriate to the locale, as well as the appropriate symbols and delimiters
132 // and number of decimal places.
133 //
134 // expression: A string representation of a currency value
135
136 return dojo.number.parse(expression, dojo.currency._mixInDefaults(options));
137 }
138
139 }