jquery.tablesorter: Minor coding style cleanup and DRY
authorTimo Tijhof <krinklemail@gmail.com>
Tue, 20 Oct 2015 00:45:07 +0000 (01:45 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Tue, 20 Oct 2015 00:59:29 +0000 (01:59 +0100)
* Consistently name 'length' cached variables 'len' (e.g. not 'l').

* Fix invalid documentation syntax for convertSortList().
  (Build is passing because this file is not yet indexed.)

* Remove redundant 'len' variable from pure JavaScript arrays where
  iterations can't change the length. Caching the length is a dated
  practice meant to micro-optimise execution time. For the moment,
  kept any loops with complex iteration bodies as-is, as well as loops
  over structures that are not pure arrays (e.g. NodeList objects).

* Use getParserById() in addParser() instead of duplicating its logic.

Change-Id: I566eff6efd97b5d9672277bacb4cb2b501f4625f

resources/src/jquery/jquery.tablesorter.js

index f8216c9..c39dc55 100644 (file)
  */
 
 ( function ( $, mw ) {
-       /* Local scope */
-
        var ts,
                parsers = [];
 
        /* Parser utility functions */
 
        function getParserById( name ) {
-               var i,
-                       len = parsers.length;
-               for ( i = 0; i < len; i++ ) {
+               var i;
+               for ( i = 0; i < parsers.length; i++ ) {
                        if ( parsers[ i ].id.toLowerCase() === name.toLowerCase() ) {
                                return parsers[ i ];
                        }
                        // Cast any numbers or other stuff to a string, methods
                        // like charAt, toLowerCase and split are expected.
                        return String( data );
-               } else {
-                       if ( !node ) {
-                               return $node.text();
-                       } else if ( node.tagName.toLowerCase() === 'img' ) {
-                               return $node.attr( 'alt' ) || ''; // handle undefined alt
-                       } else {
-                               return $.map( $.makeArray( node.childNodes ), function ( elem ) {
-                                       if ( elem.nodeType === Node.ELEMENT_NODE ) {
-                                               return getElementSortKey( elem );
-                                       } else {
-                                               return $.text( elem );
-                                       }
-                               } ).join( '' );
-                       }
                }
+               if ( !node ) {
+                       return $node.text();
+               }
+               if ( node.tagName.toLowerCase() === 'img' ) {
+                       return $node.attr( 'alt' ) || ''; // handle undefined alt
+               }
+               return $.map( $.makeArray( node.childNodes ), function ( elem ) {
+                       if ( elem.nodeType === Node.ELEMENT_NODE ) {
+                               return getElementSortKey( elem );
+                       }
+                       return $.text( elem );
+               } ).join( '' );
        }
 
        function detectParserForColumn( table, rows, column ) {
                        pos = normalized[ i ][ checkCell ];
 
                        l = row[ pos ].length;
-
                        for ( j = 0; j < l; j++ ) {
                                fragment.appendChild( row[ pos ][ j ] );
                        }
 
        function uniqueElements( array ) {
                var uniques = [];
-               $.each( array, function ( index, elem ) {
+               $.each( array, function ( i, elem ) {
                        if ( elem !== undefined && $.inArray( elem, uniques ) === -1 ) {
                                uniques.push( elem );
                        }
                        exploded,
                        $tableHeaders = $( [] ),
                        $tableRows = $( 'thead:eq(0) > tr', table );
+
                if ( $tableRows.length <= 1 ) {
                        $tableHeaders = $tableRows.children( 'th' );
                } else {
        }
 
        function isValueInArray( v, a ) {
-               var i,
-                               len = a.length;
-               for ( i = 0; i < len; i++ ) {
+               var i;
+               for ( i = 0; i < a.length; i++ ) {
                        if ( a[ i ][ 0 ] === v ) {
                                return true;
                        }
                $headers.removeClass( css[ 0 ] ).removeClass( css[ 1 ] ).attr( 'title', msg[ 1 ] );
 
                for ( var i = 0; i < list.length; i++ ) {
-                       $headers.eq( columnToHeader[ list[ i ][ 0 ] ] )
+                       $headers
+                               .eq( columnToHeader[ list[ i ][ 0 ] ] )
                                .addClass( css[ list[ i ][ 1 ] ] )
                                .attr( 'title', msg[ list[ i ][ 1 ] ] );
                }
 
        function multisort( table, sortList, cache ) {
                var i,
-                       sortFn = [],
-                       len = sortList.length;
-               for ( i = 0; i < len; i++ ) {
+                       sortFn = [];
+
+               for ( i = 0; i < sortList.length; i++ ) {
                        sortFn[ i ] = ( sortList[ i ][ 1 ] ) ? sortTextDesc : sortText;
                }
                cache.normalized.sort( function ( array1, array2 ) {
                        var i, col, ret;
-                       for ( i = 0; i < len; i++ ) {
+                       for ( i = 0; i < sortList.length; i++ ) {
                                col = sortList[ i ][ 0 ];
                                ret = sortFn[ i ].call( this, array1[ col ], array2[ col ] );
                                if ( ret !== 0 ) {
                        ascii = separatorTransformTable[ 0 ].split( '\t' ).concat( digitTransformTable[ 0 ].split( '\t' ) );
                        localised = separatorTransformTable[ 1 ].split( '\t' ).concat( digitTransformTable[ 1 ].split( '\t' ) );
 
-                       // Construct regex for number identification
+                       // Construct regexes for number identification
                        for ( i = 0; i < ascii.length; i++ ) {
                                ts.transformTable[ localised[ i ] ] = ascii[ i ];
                                digits.push( mw.RegExp.escape( localised[ i ] ) );
                $table.find( '> tbody > tr' ).each( function () {
                        var i,
                                col = 0,
-                               l = this.cells.length;
-                       for ( i = 0; i < l; i++ ) {
+                               len = this.cells.length;
+                       for ( i = 0; i < len; i++ ) {
                                $( this.cells[ i ] ).data( 'tablesorter', {
                                        realCellIndex: col,
                                        realRowIndex: this.rowIndex
         * Converts sort objects [ { Integer: String }, ... ] to the internally used nested array
         * structure [ [ Integer , Integer ], ... ]
         *
-        * @param sortObjects {Array} List of sort objects.
+        * @param {Array} sortObjects List of sort objects.
         * @return {Array} List of internal sort definitions.
         */
-
        function convertSortList( sortObjects ) {
                var sortList = [];
                $.each( sortObjects, function ( i, sortObject ) {
                        },
 
                        addParser: function ( parser ) {
-                               var i,
-                                       len = parsers.length,
-                                       a = true;
-                               for ( i = 0; i < len; i++ ) {
-                                       if ( parsers[ i ].id.toLowerCase() === parser.id.toLowerCase() ) {
-                                               a = false;
-                                       }
-                               }
-                               if ( a ) {
+                               if ( !getParserById( parser.id ) ) {
                                        parsers.push( parser );
                                }
                        },
                format: function ( s ) {
                        var i, item,
                                a = s.split( '.' ),
-                               r = '',
-                               len = a.length;
-                       for ( i = 0; i < len; i++ ) {
+                               r = '';
+                       for ( i = 0; i < a.length; i++ ) {
                                item = a[ i ];
                                if ( item.length === 1 ) {
                                        r += '00' + item;
                        return ts.rgx.isoDate[ 0 ].test( s );
                },
                format: function ( s ) {
-                       var isodate,
-                               matches;
+                       var isodate, matches;
                        if ( !Date.prototype.toISOString ) {
                                // Old browsers don't understand iso, Fallback to US date parsing and ignore the time part.
                                matches = $.trim( s ).match( ts.rgx.isoDate[ 1 ] );
-                               if ( matches ) {
-                                       isodate = new Date( matches[ 2 ]  + '/' + matches[ 3 ] + '/' + matches[ 1 ] );
-                               } else {
+                               if ( !matches ) {
                                        return $.tablesorter.formatFloat( 0 );
                                }
+                               isodate = new Date( matches[ 2 ]  + '/' + matches[ 3 ] + '/' + matches[ 1 ] );
                        } else {
                                matches = s.match( ts.rgx.isoDate[ 0 ] );
-                               if ( matches ) {
-                                       isodate = new Date( $.trim( matches[ 0 ] ) );
-                               } else {
+                               if ( !matches ) {
                                        return $.tablesorter.formatFloat( 0 );
                                }
+                               isodate = new Date( $.trim( matches[ 0 ] ) );
                        }
                        return $.tablesorter.formatFloat( ( isodate !== undefined ) ? isodate.getTime() : 0 );
                },