http://www.mediawiki.org/wiki/Special:Code/MediaWiki/88310

Revision: 88310
Author:   neilk
Date:     2011-05-17 14:57:41 +0000 (Tue, 17 May 2011)
Log Message:
-----------
clean up varnames, do not concat, better doc, add spec

Modified Paths:
--------------
    trunk/extensions/UploadWizard/resources/mw.units.js

Added Paths:
-----------
    trunk/extensions/UploadWizard/test/jasmine/spec/mw.units.spec.js

Modified: trunk/extensions/UploadWizard/resources/mw.units.js
===================================================================
--- trunk/extensions/UploadWizard/resources/mw.units.js 2011-05-17 14:54:11 UTC 
(rev 88309)
+++ trunk/extensions/UploadWizard/resources/mw.units.js 2011-05-17 14:57:41 UTC 
(rev 88310)
@@ -1,24 +1,25 @@
 ( function( mw ) { 
 
-       var scales = [ 'bytes', 'kilobytes', 'megabytes', 'gigabytes', 
'terabytes' ];
+       var scaleMsgKeys = [ 'size-bytes', 'size-kilobytes', 'size-megabytes', 
'size-gigabytes', 'size-terabytes' ];
 
        mw.units = {
 
                /**
                 * Format a size in bytes for output, using an appropriate
-                * unit (B, KB, MB, GB, or TB) according to the magnitude in 
question
+                * unit (bytes, K, MB, GB, or TB) according to the magnitude in 
question
                 *
+                * Units above K get 2 fixed decimal places.
+                *
                 * @param {Number} size, positive integer
                 * @return {String} formatted size
                 */
                bytes: function ( size ) {
                        var i = 0;
-                       // while the scale is less than terabytes, bit-shift 
size over by 1024
-                       while ( size >= 1024 && i < scales.length ) {
+                       while ( size >= 1024 && i < scaleMsgKeys.length ) {
                                size /= 1024.0;
                                i++;
                        }
-                       return gM( 'size-' + scales[i], size.toFixed( i > 1 ? 2 
: 0 ) );
+                       return gM( scaleMsgKeys[i], size.toFixed( i > 1 ? 2 : 0 
) );
                }
        };
 

Added: trunk/extensions/UploadWizard/test/jasmine/spec/mw.units.spec.js
===================================================================
--- trunk/extensions/UploadWizard/test/jasmine/spec/mw.units.spec.js            
                (rev 0)
+++ trunk/extensions/UploadWizard/test/jasmine/spec/mw.units.spec.js    
2011-05-17 14:57:41 UTC (rev 88310)
@@ -0,0 +1,44 @@
+( function( mw ) {
+
+       mediaWiki.messages.set( {
+               "size-bytes": "$1 bytes",
+               "size-kilobytes": "$1 K",
+               "size-megabytes": "$1 MB",
+               "size-gigabytes": "$1 GB",
+               "size-terabytes": "$1 TB"
+       } );
+
+       window.gM = mw.language.getMessageFunction();
+
+       // assumes english language selected
+       describe( "mw.units.bytes", function() {
+               it( "should say 0 bytes", function() { 
+                       expect( mw.units.bytes( 0 ) ).toEqual( '0 bytes' );
+               } );
+
+               it( "should say 7 bytes", function() { 
+                       expect( mw.units.bytes( 7 ) ).toEqual( '7 bytes' );
+               } );
+
+               it( "should say 900 bytes", function() { 
+                       expect( mw.units.bytes( 900 ) ).toEqual( '900 bytes' );
+               } );
+
+               it( "should say 1 K", function() { 
+                       expect( mw.units.bytes( 1024 ) ).toEqual( '1 K' );
+               } );
+
+               it( "should say 2.00 MB", function() { 
+                       expect( mw.units.bytes( 2 * 1024 * 1024 ) ).toEqual( 
'2.00 MB' );
+               } );
+
+               it( "should say 3.14 GB", function() { 
+                       expect( mw.units.bytes( 3.141592 * 1024 * 1024 * 1024 ) 
).toEqual( '3.14 GB' );
+               } );
+
+
+
+       } );
+
+} )( mediaWiki );
+


Property changes on: 
trunk/extensions/UploadWizard/test/jasmine/spec/mw.units.spec.js
___________________________________________________________________
Added: svn:eol-style
   + native


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to