]> git.wh0rd.org - tt-rss.git/blob - lib/dojo/number.js
add dijit/dojo stuff; initial ui mockup
[tt-rss.git] / lib / dojo / number.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.number"]){
9 dojo._hasResource["dojo.number"]=true;
10 dojo.provide("dojo.number");
11 dojo.require("dojo.i18n");
12 dojo.requireLocalization("dojo.cldr","number",null,"ROOT,ar,ca,cs,da,de,el,en,en-au,en-gb,es,fi,fr,he,hu,it,ja,ko,nb,nl,pl,pt,pt-pt,ru,sk,sl,sv,th,tr,zh");
13 dojo.require("dojo.string");
14 dojo.require("dojo.regexp");
15 dojo.number.format=function(_1,_2){
16 _2=dojo.mixin({},_2||{});
17 var _3=dojo.i18n.normalizeLocale(_2.locale),_4=dojo.i18n.getLocalization("dojo.cldr","number",_3);
18 _2.customs=_4;
19 var _5=_2.pattern||_4[(_2.type||"decimal")+"Format"];
20 if(isNaN(_1)||Math.abs(_1)==Infinity){
21 return null;
22 }
23 return dojo.number._applyPattern(_1,_5,_2);
24 };
25 dojo.number._numberPatternRE=/[#0,]*[#0](?:\.0*#*)?/;
26 dojo.number._applyPattern=function(_6,_7,_8){
27 _8=_8||{};
28 var _9=_8.customs.group,_a=_8.customs.decimal,_b=_7.split(";"),_c=_b[0];
29 _7=_b[(_6<0)?1:0]||("-"+_c);
30 if(_7.indexOf("%")!=-1){
31 _6*=100;
32 }else{
33 if(_7.indexOf("‰")!=-1){
34 _6*=1000;
35 }else{
36 if(_7.indexOf("¤")!=-1){
37 _9=_8.customs.currencyGroup||_9;
38 _a=_8.customs.currencyDecimal||_a;
39 _7=_7.replace(/\u00a4{1,3}/,function(_d){
40 var _e=["symbol","currency","displayName"][_d.length-1];
41 return _8[_e]||_8.currency||"";
42 });
43 }else{
44 if(_7.indexOf("E")!=-1){
45 throw new Error("exponential notation not supported");
46 }
47 }
48 }
49 }
50 var _f=dojo.number._numberPatternRE;
51 var _10=_c.match(_f);
52 if(!_10){
53 throw new Error("unable to find a number expression in pattern: "+_7);
54 }
55 if(_8.fractional===false){
56 _8.places=0;
57 }
58 return _7.replace(_f,dojo.number._formatAbsolute(_6,_10[0],{decimal:_a,group:_9,places:_8.places,round:_8.round}));
59 };
60 dojo.number.round=function(_11,_12,_13){
61 var _14=10/(_13||10);
62 return (_14*+_11).toFixed(_12)/_14;
63 };
64 if((0.9).toFixed()==0){
65 (function(){
66 var _15=dojo.number.round;
67 dojo.number.round=function(v,p,m){
68 var d=Math.pow(10,-p||0),a=Math.abs(v);
69 if(!v||a>=d||a*Math.pow(10,p+1)<5){
70 d=0;
71 }
72 return _15(v,p,m)+(v>0?d:-d);
73 };
74 })();
75 }
76 dojo.number._formatAbsolute=function(_16,_17,_18){
77 _18=_18||{};
78 if(_18.places===true){
79 _18.places=0;
80 }
81 if(_18.places===Infinity){
82 _18.places=6;
83 }
84 var _19=_17.split("."),_1a=typeof _18.places=="string"&&_18.places.indexOf(","),_1b=_18.places;
85 if(_1a){
86 _1b=_18.places.substring(_1a+1);
87 }else{
88 if(!(_1b>=0)){
89 _1b=(_19[1]||[]).length;
90 }
91 }
92 if(!(_18.round<0)){
93 _16=dojo.number.round(_16,_1b,_18.round);
94 }
95 var _1c=String(Math.abs(_16)).split("."),_1d=_1c[1]||"";
96 if(_19[1]||_18.places){
97 if(_1a){
98 _18.places=_18.places.substring(0,_1a);
99 }
100 var pad=_18.places!==undefined?_18.places:(_19[1]&&_19[1].lastIndexOf("0")+1);
101 if(pad>_1d.length){
102 _1c[1]=dojo.string.pad(_1d,pad,"0",true);
103 }
104 if(_1b<_1d.length){
105 _1c[1]=_1d.substr(0,_1b);
106 }
107 }else{
108 if(_1c[1]){
109 _1c.pop();
110 }
111 }
112 var _1e=_19[0].replace(",","");
113 pad=_1e.indexOf("0");
114 if(pad!=-1){
115 pad=_1e.length-pad;
116 if(pad>_1c[0].length){
117 _1c[0]=dojo.string.pad(_1c[0],pad);
118 }
119 if(_1e.indexOf("#")==-1){
120 _1c[0]=_1c[0].substr(_1c[0].length-pad);
121 }
122 }
123 var _1f=_19[0].lastIndexOf(","),_20,_21;
124 if(_1f!=-1){
125 _20=_19[0].length-_1f-1;
126 var _22=_19[0].substr(0,_1f);
127 _1f=_22.lastIndexOf(",");
128 if(_1f!=-1){
129 _21=_22.length-_1f-1;
130 }
131 }
132 var _23=[];
133 for(var _24=_1c[0];_24;){
134 var off=_24.length-_20;
135 _23.push((off>0)?_24.substr(off):_24);
136 _24=(off>0)?_24.slice(0,off):"";
137 if(_21){
138 _20=_21;
139 delete _21;
140 }
141 }
142 _1c[0]=_23.reverse().join(_18.group||",");
143 return _1c.join(_18.decimal||".");
144 };
145 dojo.number.regexp=function(_25){
146 return dojo.number._parseInfo(_25).regexp;
147 };
148 dojo.number._parseInfo=function(_26){
149 _26=_26||{};
150 var _27=dojo.i18n.normalizeLocale(_26.locale),_28=dojo.i18n.getLocalization("dojo.cldr","number",_27),_29=_26.pattern||_28[(_26.type||"decimal")+"Format"],_2a=_28.group,_2b=_28.decimal,_2c=1;
151 if(_29.indexOf("%")!=-1){
152 _2c/=100;
153 }else{
154 if(_29.indexOf("‰")!=-1){
155 _2c/=1000;
156 }else{
157 var _2d=_29.indexOf("¤")!=-1;
158 if(_2d){
159 _2a=_28.currencyGroup||_2a;
160 _2b=_28.currencyDecimal||_2b;
161 }
162 }
163 }
164 var _2e=_29.split(";");
165 if(_2e.length==1){
166 _2e.push("-"+_2e[0]);
167 }
168 var re=dojo.regexp.buildGroupRE(_2e,function(_2f){
169 _2f="(?:"+dojo.regexp.escapeString(_2f,".")+")";
170 return _2f.replace(dojo.number._numberPatternRE,function(_30){
171 var _31={signed:false,separator:_26.strict?_2a:[_2a,""],fractional:_26.fractional,decimal:_2b,exponent:false},_32=_30.split("."),_33=_26.places;
172 if(_32.length==1&&_2c!=1){
173 _32[1]="###";
174 }
175 if(_32.length==1||_33===0){
176 _31.fractional=false;
177 }else{
178 if(_33===undefined){
179 _33=_26.pattern?_32[1].lastIndexOf("0")+1:Infinity;
180 }
181 if(_33&&_26.fractional==undefined){
182 _31.fractional=true;
183 }
184 if(!_26.places&&(_33<_32[1].length)){
185 _33+=","+_32[1].length;
186 }
187 _31.places=_33;
188 }
189 var _34=_32[0].split(",");
190 if(_34.length>1){
191 _31.groupSize=_34.pop().length;
192 if(_34.length>1){
193 _31.groupSize2=_34.pop().length;
194 }
195 }
196 return "("+dojo.number._realNumberRegexp(_31)+")";
197 });
198 },true);
199 if(_2d){
200 re=re.replace(/([\s\xa0]*)(\u00a4{1,3})([\s\xa0]*)/g,function(_35,_36,_37,_38){
201 var _39=["symbol","currency","displayName"][_37.length-1],_3a=dojo.regexp.escapeString(_26[_39]||_26.currency||"");
202 _36=_36?"[\\s\\xa0]":"";
203 _38=_38?"[\\s\\xa0]":"";
204 if(!_26.strict){
205 if(_36){
206 _36+="*";
207 }
208 if(_38){
209 _38+="*";
210 }
211 return "(?:"+_36+_3a+_38+")?";
212 }
213 return _36+_3a+_38;
214 });
215 }
216 return {regexp:re.replace(/[\xa0 ]/g,"[\\s\\xa0]"),group:_2a,decimal:_2b,factor:_2c};
217 };
218 dojo.number.parse=function(_3b,_3c){
219 var _3d=dojo.number._parseInfo(_3c),_3e=(new RegExp("^"+_3d.regexp+"$")).exec(_3b);
220 if(!_3e){
221 return NaN;
222 }
223 var _3f=_3e[1];
224 if(!_3e[1]){
225 if(!_3e[2]){
226 return NaN;
227 }
228 _3f=_3e[2];
229 _3d.factor*=-1;
230 }
231 _3f=_3f.replace(new RegExp("["+_3d.group+"\\s\\xa0"+"]","g"),"").replace(_3d.decimal,".");
232 return _3f*_3d.factor;
233 };
234 dojo.number._realNumberRegexp=function(_40){
235 _40=_40||{};
236 if(!("places" in _40)){
237 _40.places=Infinity;
238 }
239 if(typeof _40.decimal!="string"){
240 _40.decimal=".";
241 }
242 if(!("fractional" in _40)||/^0/.test(_40.places)){
243 _40.fractional=[true,false];
244 }
245 if(!("exponent" in _40)){
246 _40.exponent=[true,false];
247 }
248 if(!("eSigned" in _40)){
249 _40.eSigned=[true,false];
250 }
251 var _41=dojo.number._integerRegexp(_40),_42=dojo.regexp.buildGroupRE(_40.fractional,function(q){
252 var re="";
253 if(q&&(_40.places!==0)){
254 re="\\"+_40.decimal;
255 if(_40.places==Infinity){
256 re="(?:"+re+"\\d+)?";
257 }else{
258 re+="\\d{"+_40.places+"}";
259 }
260 }
261 return re;
262 },true);
263 var _43=dojo.regexp.buildGroupRE(_40.exponent,function(q){
264 if(q){
265 return "([eE]"+dojo.number._integerRegexp({signed:_40.eSigned})+")";
266 }
267 return "";
268 });
269 var _44=_41+_42;
270 if(_42){
271 _44="(?:(?:"+_44+")|(?:"+_42+"))";
272 }
273 return _44+_43;
274 };
275 dojo.number._integerRegexp=function(_45){
276 _45=_45||{};
277 if(!("signed" in _45)){
278 _45.signed=[true,false];
279 }
280 if(!("separator" in _45)){
281 _45.separator="";
282 }else{
283 if(!("groupSize" in _45)){
284 _45.groupSize=3;
285 }
286 }
287 var _46=dojo.regexp.buildGroupRE(_45.signed,function(q){
288 return q?"[-+]":"";
289 },true);
290 var _47=dojo.regexp.buildGroupRE(_45.separator,function(sep){
291 if(!sep){
292 return "(?:\\d+)";
293 }
294 sep=dojo.regexp.escapeString(sep);
295 if(sep==" "){
296 sep="\\s";
297 }else{
298 if(sep==" "){
299 sep="\\s\\xa0";
300 }
301 }
302 var grp=_45.groupSize,_48=_45.groupSize2;
303 if(_48){
304 var _49="(?:0|[1-9]\\d{0,"+(_48-1)+"}(?:["+sep+"]\\d{"+_48+"})*["+sep+"]\\d{"+grp+"})";
305 return ((grp-_48)>0)?"(?:"+_49+"|(?:0|[1-9]\\d{0,"+(grp-1)+"}))":_49;
306 }
307 return "(?:0|[1-9]\\d{0,"+(grp-1)+"}(?:["+sep+"]\\d{"+grp+"})*)";
308 },true);
309 return _46+_47;
310 };
311 }