Use native ES5 Array prototype methods instead of jQuery
authorFomafix <fomafix@googlemail.com>
Thu, 11 Jan 2018 14:27:08 +0000 (15:27 +0100)
committerFomafix <fomafix@googlemail.com>
Thu, 11 Jan 2018 20:06:14 +0000 (21:06 +0100)
Replace
* $.each( array, function ( index, value ) { ... } ) by
  array.forEach( function ( value, index ) { ... } )

* $.grep( array, function ( value ) { ... } ) by
  array.filter( function ( value ) { ... } )

* $.map( array, function ( value ) { ... } ) by
  array.map( function ( value ) { ... } )

This change is a follow-up to 9d67e9973e6766e057c70ea8c811be8e269fb80a.

Change-Id: I8ef9af8c4d2f440faca65ec7c78a977ea7c31ad2

17 files changed:
resources/src/jquery/jquery.tablesorter.js
resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.FiltersViewModel.js
resources/src/mediawiki.special/mediawiki.special.apisandbox.js
resources/src/mediawiki.widgets.datetime/CalendarWidget.js
resources/src/mediawiki.widgets/mw.widgets.CategoryCapsuleItemWidget.js
resources/src/mediawiki/mediawiki.Uri.js
resources/src/mediawiki/mediawiki.inspect.js
tests/qunit/data/testrunner.js
tests/qunit/suites/resources/jquery/jquery.highlightText.test.js
tests/qunit/suites/resources/jquery/jquery.tablesorter.parsers.test.js
tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js
tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
tests/qunit/suites/resources/mediawiki/mediawiki.RegExp.test.js
tests/qunit/suites/resources/mediawiki/mediawiki.Uri.test.js
tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js
tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js
tests/qunit/suites/resources/startup.test.js

index cac103e..21209f6 100644 (file)
                                } );
                        } );
                        // We want to find the row that has the most columns (ignoring colspan)
-                       $.each( exploded, function ( index, cellArray ) {
+                       exploded.forEach( function ( cellArray, index ) {
                                headerCount = $( uniqueElements( cellArray ) ).filter( 'th' ).length;
                                if ( headerCount >= maxSeen ) {
                                        maxSeen = headerCount;
         */
        function setHeadersOrder( $headers, sortList, headerToColumns ) {
                // Loop through all headers to retrieve the indices of the columns the header spans across:
-               $.each( headerToColumns, function ( headerIndex, columns ) {
+               headerToColumns.forEach( function ( columns, headerIndex ) {
 
-                       $.each( columns, function ( i, columnIndex ) {
+                       columns.forEach( function ( columnIndex, i ) {
                                var header = $headers[ headerIndex ],
                                        $header = $( header );
 
                                        } );
                                } else {
                                        // Column shall be sorted: Apply designated count and order.
-                                       $.each( sortList, function ( j, sortColumn ) {
+                                       sortList.forEach( function ( sortColumn ) {
                                                if ( sortColumn[ 0 ] === i ) {
                                                        $header.data( {
                                                                order: sortColumn[ 1 ],
                                }
                                return ret;
                        } );
-                       $.each( rowspanCells, function () {
-                               $.data( this, 'tablesorter' ).needResort = false;
+                       rowspanCells.forEach( function ( cell ) {
+                               $.data( cell, 'tablesorter' ).needResort = false;
                        } );
                }
                resortCells();
index c9f5924..5564d1e 100644 (file)
        mw.rcfilters.dm.FiltersViewModel.prototype.getFirstConflictedItem = function () {
                var conflictedItem;
 
-               $.each( this.getItems(), function ( index, filterItem ) {
+               this.getItems().forEach( function ( filterItem ) {
                        if ( filterItem.isSelected() && filterItem.isConflicted() ) {
                                conflictedItem = filterItem;
                                return false;
index 534af05..a6450e9 100644 (file)
                                                                label: Util.parseMsg( 'apisandbox-request-selectformat-label' )
                                                        }
                                                ).$element,
-                                               $.map( formatItems, function ( item ) {
+                                               formatItems.map( function ( item ) {
                                                        return item.getData().$element;
                                                } ),
                                                $result
index 54a5a85..fa05991 100644 (file)
                if ( dates instanceof Date ) {
                        dates = [ dates ];
                } else if ( Array.isArray( dates ) ) {
-                       dates = $.grep( dates, function ( dt ) { return dt instanceof Date; } );
+                       dates = dates.filter( function ( dt ) { return dt instanceof Date; } );
                        dates.sort();
                } else {
                        dates = [];
index 488d9e0..42ef1ac 100644 (file)
@@ -61,7 +61,7 @@
                        $.each( response.query.pages, function ( index, page ) {
                                pages[ page.title ] = !page.missing;
                        } );
-                       $.each( titles, function ( index, title ) {
+                       titles.forEach( function ( title ) {
                                var normalizedTitle = title;
                                while ( normalized[ normalizedTitle ] ) {
                                        normalizedTitle = normalized[ normalizedTitle ];
index 59261cd..450b415 100644 (file)
 
                                // Apply parser regex and set all properties based on the result
                                matches = parser[ options.strictMode ? 'strict' : 'loose' ].exec( str );
-                               $.each( properties, function ( i, property ) {
+                               properties.forEach( function ( property, i ) {
                                        uri[ property ] = matches[ i + 1 ];
                                } );
 
                                $.each( this.query, function ( key, val ) {
                                        var k = Uri.encode( key ),
                                                vals = Array.isArray( val ) ? val : [ val ];
-                                       $.each( vals, function ( i, v ) {
+                                       vals.forEach( function ( v ) {
                                                if ( v === null ) {
                                                        args.push( k );
                                                } else if ( k === 'title' ) {
index 006ca1f..f91ffbb 100644 (file)
@@ -55,7 +55,7 @@
                                }
                                graph[ moduleName ].requires = dependencies;
 
-                               $.each( dependencies, function ( depIndex, depName ) {
+                               dependencies.forEach( function ( depName ) {
                                        if ( !hasOwn.call( graph, depName ) ) {
                                                graph[ depName ] = { requiredBy: [] };
                                        }
index e944ef0..5943294 100644 (file)
                                        // Test should use fake XHR, wait for requests, or call abort()
                                        $activeLen = $.active;
                                        if ( $activeLen !== undefined && $activeLen !== 0 ) {
-                                               pending = $.grep( ajaxRequests, function ( ajax ) {
+                                               pending = ajaxRequests.filter( function ( ajax ) {
                                                        return ajax.xhr.state() === 'pending';
                                                } );
                                                if ( pending.length !== $activeLen ) {
                                                        mw.log.warn( 'Pending requests does not match jQuery.active count' );
                                                }
                                                // Force requests to stop to give the next test a clean start
-                                               $.each( ajaxRequests, function ( i, ajax ) {
+                                               ajaxRequests.forEach( function ( ajax, i ) {
                                                        mw.log.warn(
                                                                'AJAX request #' + i + ' (state: ' + ajax.xhr.state() + ')',
                                                                ajax.options
index 0dac22e..277ba3f 100644 (file)
                                }
                        ];
 
-               $.each( cases, function ( i, item ) {
+               cases.forEach( function ( item ) {
                        $fixture = $( '<p>' ).text( item.text ).highlightText( item.highlight );
                        assert.equal(
                                $fixture.html(),
index 01589c3..2865cbb 100644 (file)
@@ -62,7 +62,7 @@
                        }
 
                        parser = $.tablesorter.getParser( parserId );
-                       $.each( data, function ( index, testcase ) {
+                       data.forEach( function ( testcase ) {
                                extractedR = parser.is( testcase[ 0 ] );
                                extractedF = parser.format( testcase[ 0 ] );
 
index 27d7e8d..1db8c61 100644 (file)
                        $tbody = $table.find( 'tbody' ),
                        $tr = $( '<tr>' );
 
-               $.each( header, function ( i, str ) {
+               header.forEach( function ( str ) {
                        var $th = $( '<th>' );
                        $th.text( str ).appendTo( $tr );
                } );
                for ( i = 0; i < data.length; i++ ) {
                        $tr = $( '<tr>' );
                        // eslint-disable-next-line no-loop-func
-                       $.each( data[ i ], function ( j, str ) {
+                       data[ i ].forEach( function ( str ) {
                                var $td = $( '<td>' );
                                $td.text( str ).appendTo( $tr );
                        } );
index 6c2d51e..5a92da4 100644 (file)
                } );
                this.api.abort();
                assert.ok( this.requests.length === 2, 'Check both requests triggered' );
-               $.each( this.requests, function ( i, request ) {
+               this.requests.forEach( function ( request, i ) {
                        assert.ok( request.abort.calledOnce, 'abort request number ' + i );
                } );
        } );
index 97c82fb..4e15cf0 100644 (file)
@@ -1,4 +1,4 @@
-( function ( mw, $ ) {
+( function ( mw ) {
        QUnit.module( 'mediawiki.RegExp' );
 
        QUnit.test( 'escape', function ( assert ) {
                        '0123456789'
                ].join( '' );
 
-               $.each( specials, function ( i, str ) {
+               specials.forEach( function ( str ) {
                        assert.propEqual( str.match( new RegExp( mw.RegExp.escape( str ) ) ), [ str ], 'Match ' + str );
                } );
 
                assert.equal( mw.RegExp.escape( normal ), normal, 'Alphanumerals are left alone' );
        } );
 
-}( mediaWiki, jQuery ) );
+}( mediaWiki ) );
index e56c933..84b8f06 100644 (file)
@@ -1,4 +1,4 @@
-( function ( mw, $ ) {
+( function ( mw ) {
        QUnit.module( 'mediawiki.Uri', QUnit.newMwEnvironment( {
                setup: function () {
                        this.mwUriOrg = mw.Uri;
@@ -10,7 +10,7 @@
                }
        } ) );
 
-       $.each( [ true, false ], function ( i, strictMode ) {
+       [ true, false ].forEach( function ( strictMode ) {
                QUnit.test( 'Basic construction and properties (' + ( strictMode ? '' : 'non-' ) + 'strict mode)', function ( assert ) {
                        var uriString, uri;
                        uriString = 'http://www.ietf.org/rfc/rfc2396.txt';
                href = uri.toString();
                assert.equal( href, testProtocol + testServer + ':' + testPort + testPath, 'Root-relative URL gets host, protocol, and port supplied' );
        } );
-}( mediaWiki, jQuery ) );
+}( mediaWiki ) );
index db51fb3..67ee3b8 100644 (file)
                var queue;
                mw.messages.set( 'formatnum-msg', '{{formatnum:$1}}' );
                mw.messages.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
-               queue = $.map( formatnumTests, function ( test ) {
+               queue = formatnumTests.map( function ( test ) {
                        var done = assert.async();
                        return function ( next, abort ) {
                                getMwLanguage( test.lang )
index 1730575..ef894a9 100644 (file)
@@ -53,7 +53,7 @@
                ];
 
        Array.prototype.push.apply( IPV6_CASES,
-               $.map( [
+               [
                        'fc:100::',
                        'fc:100:a::',
                        'fc:100:a:d::',
@@ -69,8 +69,8 @@
                        '::fc:100:a:d:1:e',
                        '::fc:100:a:d:1:e:ac',
                        'fc:100:a:d:1:e:ac:0'
-               ], function ( el ) {
-                       return [ [ true, el, el + ' is a valid IP' ] ];
+               ].map( function ( el ) {
+                       return [ true, el, el + ' is a valid IP' ];
                } )
        );
 
                        newExperimental = [ 'html5', 'html5-legacy' ];
 
                // Test cases are kept in sync with SanitizerTest.php
-               $.each( [
+               [
                        // Pure legacy: how MW worked before 2017
                        [ legacy, text, legacyEncoded ],
                        // Transition to a new world: legacy links with HTML5 fallback
                        [ experimentalLegacy, text, html5Experimental ],
                        // Migration from $wgExperimentalHtmlIds to modern HTML5
                        [ newExperimental, text, html5Encoded ]
-               ], function ( index, testCase ) {
+               ].forEach( function ( testCase ) {
                        mw.config.set( 'wgFragmentMode', testCase[ 0 ] );
 
                        assert.equal( util.escapeIdForAttribute( testCase[ 1 ] ), testCase[ 2 ] );
                        experimentalLegacy = [ 'html5-legacy', 'legacy' ],
                        newExperimental = [ 'html5', 'html5-legacy' ];
 
-               $.each( [
+               [
                        // Pure legacy: how MW worked before 2017
                        [ legacy, text, legacyEncoded ],
                        // Transition to a new world: legacy links with HTML5 fallback
                        [ experimentalLegacy, text, html5Experimental ],
                        // Migration from wgExperimentalHtmlIds to modern HTML5
                        [ newExperimental, text, html5Encoded ]
-               ], function ( index, testCase ) {
+               ].forEach( function ( testCase ) {
                        mw.config.set( 'wgFragmentMode', testCase[ 0 ] );
 
                        assert.equal( util.escapeIdForLink( testCase[ 1 ] ), testCase[ 2 ] );
        } );
 
        QUnit.test( 'isIPv6Address', function ( assert ) {
-               $.each( IPV6_CASES, function ( i, ipCase ) {
+               IPV6_CASES.forEach( function ( ipCase ) {
                        assert.strictEqual( util.isIPv6Address( ipCase[ 1 ] ), ipCase[ 0 ], ipCase[ 2 ] );
                } );
        } );
 
        QUnit.test( 'isIPv4Address', function ( assert ) {
-               $.each( IPV4_CASES, function ( i, ipCase ) {
+               IPV4_CASES.forEach( function ( ipCase ) {
                        assert.strictEqual( util.isIPv4Address( ipCase[ 1 ] ), ipCase[ 0 ], ipCase[ 2 ] );
                } );
        } );
 
        QUnit.test( 'isIPAddress', function ( assert ) {
-               $.each( IPV4_CASES, function ( i, ipCase ) {
+               IPV4_CASES.forEach( function ( ipCase ) {
                        assert.strictEqual( util.isIPv4Address( ipCase[ 1 ] ), ipCase[ 0 ], ipCase[ 2 ] );
                } );
 
-               $.each( IPV6_CASES, function ( i, ipCase ) {
+               IPV6_CASES.forEach( function ( ipCase ) {
                        assert.strictEqual( util.isIPv6Address( ipCase[ 1 ] ), ipCase[ 0 ], ipCase[ 2 ] );
                } );
        } );
index ee1340d..0866b9e 100644 (file)
@@ -1,5 +1,5 @@
 /* global isCompatible: true */
-( function ( $ ) {
+( function () {
        var testcases = {
                tested: [
                        /* Grade A */
        QUnit.module( 'startup', QUnit.newMwEnvironment() );
 
        QUnit.test( 'isCompatible( featureTestable )', function ( assert ) {
-               $.each( testcases.tested, function ( i, ua ) {
+               testcases.tested.forEach( function ( ua ) {
                        assert.strictEqual( isCompatible( ua ), true, ua );
                } );
        } );
 
        QUnit.test( 'isCompatible( blacklisted )', function ( assert ) {
-               $.each( testcases.blacklisted, function ( i, ua ) {
+               testcases.blacklisted.forEach( function ( ua ) {
                        assert.strictEqual( isCompatible( ua ), false, ua );
                } );
        } );
-}( jQuery ) );
+}() );