mediawiki.language: Fix infinite loop in commafy() when pattern has no grouping
authorSanthosh Thottingal <santhosh.thottingal@gmail.com>
Sat, 23 Aug 2014 12:52:03 +0000 (18:22 +0530)
committerSanthosh Thottingal <santhosh.thottingal@gmail.com>
Mon, 1 Sep 2014 11:57:42 +0000 (17:27 +0530)
When the decimal part of the pattern has no grouping, commafy was entering
into an infinite loop. Corrected the logic to handle this case.

Also added qunit tests.

Bug: 69835
Change-Id: I0f9e274411aead6a4e95402fd38c54ac6b76df53

resources/src/mediawiki.language/mediawiki.language.numbers.js
tests/qunit/suites/resources/mediawiki/mediawiki.language.test.js

index 56fa0da..8bd2de9 100644 (file)
@@ -34,7 +34,7 @@
        }
 
        /**
-        * Efficiently replicate a string `n` times.
+        * Replicate a string 'n' times.
         *
         * @private
         * @param {string} str The string to replicate
@@ -47,9 +47,8 @@
                }
 
                var buf = [];
-               while (num) {
+               while ( num-- ) {
                        buf.push( str );
-                       str += str;
                }
                return buf.join( '' );
        }
                }
 
                for ( whole = valueParts[0]; whole; ) {
-                       off = whole.length - groupSize;
-
+                       off = groupSize ? whole.length - groupSize : 0;
                        pieces.push( ( off > 0 ) ? whole.substr( off ) : whole );
                        whole = ( off > 0 ) ? whole.slice( 0, off ) : '';
 
                        if ( groupSize2 ) {
                                groupSize = groupSize2;
+                               groupSize2 = null;
                        }
                }
                valueParts[0] = pieces.reverse().join( options.group );
index 3bfabe4..d43baee 100644 (file)
                assert.equal( mw.language.getData( 'en', 'invalidkey' ), undefined, 'Getter setter test for mw.language with invalid key' );
        } );
 
+       QUnit.test( 'mw.language.commafy test', 9, function ( assert ) {
+               // Number grouping patterns are as per http://cldr.unicode.org/translation/number-patterns
+               assert.equal( mw.language.commafy( 1234.567, '###0.#####' ), '1234.567', 'Pattern with no digit grouping separator defined' );
+               assert.equal( mw.language.commafy( 123456789.567, '###0.#####' ), '123456789.567', 'Pattern with no digit grouping seperator defined, bigger decimal part' );
+               assert.equal( mw.language.commafy( 0.567, '###0.#####' ), '0.567', 'Decimal part 0' );
+               assert.equal( mw.language.commafy( '.567', '###0.#####' ), '0.567', 'Decimal part missing. replace with zero' );
+               assert.equal( mw.language.commafy( 1234, '##,#0.#####' ), '12,34', 'Pattern with no fractional part' );
+               assert.equal( mw.language.commafy( -1234.567, '###0.#####' ), '-1234.567', 'Negative number' );
+               assert.equal( mw.language.commafy( -1234.567, '#,###.00' ), '-1,234.56', 'Fractional part bigger than pattern.' );
+               assert.equal( mw.language.commafy( 123456789.567, '###,##0.00' ), '123,456,789.56', 'Decimal part as group of 3' );
+               assert.equal( mw.language.commafy( 123456789.567, '###,###,#0.00' ), '1,234,567,89.56', 'Decimal part as group of 3 and last one 2' );
+       } );
+
        function grammarTest( langCode, test ) {
                // The test works only if the content language is opt.language
                // because it requires [lang].js to be loaded.