]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/currency.js
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
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");
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");
19 // summary: localized formatting and parsing routines for currencies
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.
31 dojo
.currency
._mixInDefaults = function(options
){
32 options
= options
|| {};
33 options
.type
= "currency";
35 // Get locale-dependent currency data, like the symbol
36 var bundle
= dojo
.i18n
.getLocalization("dojo.cldr", "currency", options
.locale
) || {};
38 // Mixin locale-independent currency data, like # of places
39 var iso
= options
.currency
;
40 var data
= dojo
.cldr
.monetary
.getData(iso
);
42 dojo
.forEach(["displayName","symbol","group","decimal"], function(prop
){
43 data
[prop
] = bundle
[iso
+"_"+prop
];
46 data
.fractional
= [true, false];
48 // Mixin with provided options
49 return dojo
.mixin(data
, options
);
53 dojo.declare("dojo.currency.__FormatOptions", [dojo.number.__FormatOptions], {
55 // Should not be set. Value is assumed to be "currency".
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.
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.
63 // number of decimal places to show. Default is defined based on which currency is used.
71 dojo
.currency
.format = function(/*Number*/value
, /*dojo.currency.__FormatOptions?*/options
){
73 // Format a Number as a currency, using locale-specific settings
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.
82 // the number to be formatted.
84 return dojo
.number
.format(value
, dojo
.currency
._mixInDefaults(options
));
87 dojo
.currency
.regexp = function(/*dojo.number.__RegexpOptions?*/options
){
90 // Builds the regular needed to parse a currency value
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
99 dojo.declare("dojo.currency.__ParseOptions", [dojo.number.__ParseOptions], {
101 // Should not be set. Value is assumed to be currency.
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.
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.
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.
122 dojo
.currency
.parse = function(/*String*/expression
, /*dojo.currency.__ParseOptions?*/options
){
125 // Convert a properly formatted currency string to a primitive Number,
126 // using locale-specific settings.
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.
134 // expression: A string representation of a currency value
136 return dojo
.number
.parse(expression
, dojo
.currency
._mixInDefaults(options
));