Index: trunk/extensions/UploadWizard/test/jasmine/SpecRunner.html |
— | — | @@ -19,6 +19,7 @@ |
20 | 20 | <script type="text/javascript" src="../../resources/mw.Api.js"></script> |
21 | 21 | <script type="text/javascript" src="../../resources/mw.Api.edit.js"></script> |
22 | 22 | <script type="text/javascript" src="../../resources/mw.Title.js"></script> |
| 23 | + <script type="text/javascript" src="../../resources/mw.units.js"></script> |
23 | 24 | |
24 | 25 | <script type="text/javascript" src="spec/mediawiki.language.parser.spec.data.js"></script> |
25 | 26 | |
— | — | @@ -31,6 +32,7 @@ |
32 | 33 | <script type="text/javascript" src="spec/mediawiki.language.parser.spec.js"></script> |
33 | 34 | |
34 | 35 | <script type="text/javascript" src="spec/mw.Title.spec.js"></script> |
| 36 | + <script type="text/javascript" src="spec/mw.units.spec.js"></script> |
35 | 37 | |
36 | 38 | </head> |
37 | 39 | <body> |
Index: trunk/extensions/UploadWizard/resources/mw.units.js |
— | — | @@ -1,35 +1,26 @@ |
2 | | -/** |
3 | | - * Format a size in bytes for output, using an appropriate |
4 | | - * unit (B, KB, MB or GB) according to the magnitude in question |
5 | | - * |
6 | | - * @param size Size to format |
7 | | - * @return string Plain text (not HTML) |
8 | | - */ |
9 | | -mw.units.bytes = function ( size ) { |
10 | | - // For small sizes no decimal places are necessary |
11 | | - var round = 0; |
12 | | - var msg = ''; |
13 | | - if ( size > 1024 ) { |
14 | | - size = size / 1024; |
15 | | - if ( size > 1024 ) { |
16 | | - size = size / 1024; |
17 | | - // For MB and bigger two decimal places are smarter |
18 | | - round = 2; |
19 | | - if ( size > 1024 ) { |
20 | | - size = size / 1024; |
21 | | - msg = 'mwe-size-gigabytes'; |
22 | | - } else { |
23 | | - msg = 'mwe-size-megabytes'; |
| 2 | +( function( mw ) { |
| 3 | + |
| 4 | + var scales = [ 'bytes', 'kilobytes', 'megabytes', 'gigabytes', 'terabytes' ]; |
| 5 | + |
| 6 | + mw.units = { |
| 7 | + |
| 8 | + /** |
| 9 | + * Format a size in bytes for output, using an appropriate |
| 10 | + * unit (B, KB, MB, GB, or TB) according to the magnitude in question |
| 11 | + * |
| 12 | + * @param {Number} size, positive integer |
| 13 | + * @return {String} formatted size |
| 14 | + */ |
| 15 | + bytes: function ( size ) { |
| 16 | + var i = 0; |
| 17 | + // while the scale is less than terabytes, bit-shift size over by 1024 |
| 18 | + while ( size >= 1024 && i < scales.length ) { |
| 19 | + size /= 1024.0; |
| 20 | + i++; |
24 | 21 | } |
25 | | - } else { |
26 | | - msg = 'mwe-size-kilobytes'; |
| 22 | + return gM( 'size-' + scales[i], size.toFixed( i > 1 ? 2 : 0 ) ); |
27 | 23 | } |
28 | | - } else { |
29 | | - msg = 'mwe-size-bytes'; |
30 | | - } |
31 | | - // JavaScript does not let you choose the precision when rounding |
32 | | - var p = Math.pow( 10, round ); |
33 | | - size = Math.round( size * p ) / p; |
34 | | - return gM( msg , size ); |
35 | | -}; |
| 24 | + }; |
36 | 25 | |
| 26 | +} )( mediaWiki ); |
| 27 | + |