]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/currency.js
2 Copyright (c) 2004-2011, 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");
11 dojo
.require("dojo.number");
12 dojo
.require("dojo.i18n");
13 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,ro,ru,sk,sl,sv,th,tr,zh,zh-hant,zh-hk,zh-tw");
14 dojo
.require("dojo.cldr.monetary");
16 dojo
.getObject("currency", true, dojo
);
20 // summary: localized formatting and parsing routines for currencies
22 // description: extends dojo.number to provide culturally-appropriate formatting of values
23 // in various world currencies, including use of a currency symbol. The currencies are specified
24 // by a three-letter international symbol in all uppercase, and support for the currencies is
25 // provided by the data in `dojo.cldr`. The scripts generating dojo.cldr specify which
26 // currency support is included. A fixed number of decimal places is determined based
27 // on the currency type and is not determined by the 'pattern' argument. The fractional
28 // portion is optional, by default, and variable length decimals are not supported.
32 dojo
.currency
._mixInDefaults = function(options
){
33 options
= options
|| {};
34 options
.type
= "currency";
36 // Get locale-dependent currency data, like the symbol
37 var bundle
= dojo
.i18n
.getLocalization("dojo.cldr", "currency", options
.locale
) || {};
39 // Mixin locale-independent currency data, like # of places
40 var iso
= options
.currency
;
41 var data
= dojo
.cldr
.monetary
.getData(iso
);
43 dojo
.forEach(["displayName","symbol","group","decimal"], function(prop
){
44 data
[prop
] = bundle
[iso
+"_"+prop
];
47 data
.fractional
= [true, false];
49 // Mixin with provided options
50 return dojo
.mixin(data
, options
);
54 dojo.declare("dojo.currency.__FormatOptions", [dojo.number.__FormatOptions], {
56 // Should not be set. Value is assumed to be "currency".
58 // localized currency symbol. The default will be looked up in table of supported currencies in `dojo.cldr`
59 // A [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code will be used if not found.
61 // an [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code, a three letter sequence like "USD".
62 // For use with dojo.currency only.
64 // number of decimal places to show. Default is defined based on which currency is used.
72 dojo
.currency
.format = function(/*Number*/value
, /*dojo.currency.__FormatOptions?*/options
){
74 // Format a Number as a currency, using locale-specific settings
77 // Create a string from a Number using a known, localized pattern.
78 // [Formatting patterns](http://www.unicode.org/reports/tr35/#Number_Elements)
79 // appropriate to the locale are chosen from the [CLDR](http://unicode.org/cldr)
80 // as well as the appropriate symbols and delimiters and number of decimal places.
83 // the number to be formatted.
85 return dojo
.number
.format(value
, dojo
.currency
._mixInDefaults(options
));
88 dojo
.currency
.regexp = function(/*dojo.number.__RegexpOptions?*/options
){
91 // Builds the regular needed to parse a currency value
94 // Returns regular expression with positive and negative match, group and decimal separators
95 // Note: the options.places default, the number of decimal places to accept, is defined by the currency type.
96 return dojo
.number
.regexp(dojo
.currency
._mixInDefaults(options
)); // String
100 dojo.declare("dojo.currency.__ParseOptions", [dojo.number.__ParseOptions], {
102 // Should not be set. Value is assumed to be currency.
104 // an [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code, a three letter sequence like "USD".
105 // For use with dojo.currency only.
107 // localized currency symbol. The default will be looked up in table of supported currencies in `dojo.cldr`
108 // A [ISO4217](http://en.wikipedia.org/wiki/ISO_4217) currency code will be used if not found.
110 // fixed number of decimal places to accept. The default is determined based on which currency is used.
111 // fractional: Boolean?|Array?
112 // Whether to include the fractional portion, where the number of decimal places are implied by the currency
113 // or explicit 'places' parameter. The value [true,false] makes the fractional portion optional.
114 // By default for currencies, it the fractional portion is optional.
123 dojo
.currency
.parse = function(/*String*/expression
, /*dojo.currency.__ParseOptions?*/options
){
126 // Convert a properly formatted currency string to a primitive Number,
127 // using locale-specific settings.
130 // Create a Number from a string using a known, localized pattern.
131 // [Formatting patterns](http://www.unicode.org/reports/tr35/#Number_Format_Patterns)
132 // are chosen appropriate to the locale, as well as the appropriate symbols and delimiters
133 // and number of decimal places.
135 // expression: A string representation of a currency value
137 return dojo
.number
.parse(expression
, dojo
.currency
._mixInDefaults(options
));