Change `.forEach()` to `for ()` loop with break
authorFomafix <fomafix@googlemail.com>
Thu, 13 Sep 2018 18:38:09 +0000 (20:38 +0200)
committerFomafix <fomafix@googlemail.com>
Mon, 11 Feb 2019 17:14:59 +0000 (18:14 +0100)
Follow-up to change 1edba8029, which replaced:
  $.each( array, function ( index, value ) { ... } )
by:
  array.forEach( function ( value, index ) { ... } )

`jQuery.each` makes a break of the loop on a `return false`.
`Array.prototype.forEach` ignores the return value and there
is no way to stop or break a `forEach()` loop other than by
throwing an exception. [1]

A `$.each` with a `return false` could be replaced by
`Array.prototype.find`, but `find()` is not part of ES5. [2]

  return this.getItems().find( function ( filterItem ) {
    return filterItem.isSelected() && filterItem.isConflicted();
  } );

So a simple `for ()` loop with a `break` or a `return` is
used in these cases.

Also explicitly specify `undefined` as possible return value
in FiltersViewModel.prototype.getFirstConflictedItem.

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
[2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

Change-Id: I0af89d39ed789f49e06b25cac14ecd995ba58cbd

resources/src/jquery.tablesorter/jquery.tablesorter.js
resources/src/mediawiki.rcfilters/dm/FiltersViewModel.js

index 4882e9e..57fd3ab 100644 (file)
                headerToColumns.forEach( function ( columns, headerIndex ) {
 
                        columns.forEach( function ( columnIndex, i ) {
-                               var header = $headers[ headerIndex ],
+                               var j, sortColumn,
+                                       header = $headers[ headerIndex ],
                                        $header = $( header );
 
                                if ( !isValueInArray( columnIndex, sortList ) ) {
                                        } );
                                } else {
                                        // Column shall be sorted: Apply designated count and order.
-                                       sortList.forEach( function ( sortColumn ) {
+                                       for ( j = 0; j < sortList.length; j++ ) {
+                                               sortColumn = sortList[ j ];
                                                if ( sortColumn[ 0 ] === i ) {
                                                        $header.data( {
                                                                order: sortColumn[ 1 ],
                                                                count: sortColumn[ 1 ] + 1
                                                        } );
-                                                       return false;
+                                                       break;
                                                }
-                                       } );
+                                       }
                                }
                        } );
 
index d89bb28..2e6abab 100644 (file)
        /**
         * Get the first item with a current conflict
         *
-        * @return {mw.rcfilters.dm.FilterItem} Conflicted item
+        * @return {mw.rcfilters.dm.FilterItem|undefined} Conflicted item or undefined when not found
         */
        FiltersViewModel.prototype.getFirstConflictedItem = function () {
-               var conflictedItem;
-
-               this.getItems().forEach( function ( filterItem ) {
+               var i, filterItem, items = this.getItems();
+               for ( i = 0; i < items.length; i++ ) {
+                       filterItem = items[ i ];
                        if ( filterItem.isSelected() && filterItem.isConflicted() ) {
-                               conflictedItem = filterItem;
-                               return false;
+                               return filterItem;
                        }
-               } );
-
-               return conflictedItem;
+               }
        };
 
        /**