Use native ES5 Array prototype methods instead of jQuery
authorFomafix <fomafix@googlemail.com>
Fri, 12 Jan 2018 15:47:40 +0000 (16:47 +0100)
committerFomafix <fomafix@googlemail.com>
Fri, 12 Jan 2018 18:02:38 +0000 (19:02 +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 ) { ... } )

* $.inArray( value, array ) by
  array.indexOf( value )

This change is a follow-up to 1edba8029a561919febf8b90f5df689d090665dd.

Change-Id: I16134642c52002de0eacb987bed5143f528bf627

13 files changed:
resources/src/jquery/jquery.suggestions.js
resources/src/jquery/jquery.tablesorter.js
resources/src/mediawiki.action/mediawiki.action.edit.stash.js
resources/src/mediawiki.special/mediawiki.special.apisandbox.js
resources/src/mediawiki.special/mediawiki.special.block.js
resources/src/mediawiki.special/mediawiki.special.upload.js
resources/src/mediawiki/api.js
resources/src/mediawiki/mediawiki.jqueryMsg.js
resources/src/mediawiki/page/image-pagination.js
tests/qunit/suites/resources/jquery/jquery.textSelection.test.js
tests/qunit/suites/resources/mediawiki.api/mediawiki.api.options.test.js
tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js
tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js

index 4f4edc9..39c601f 100644 (file)
                                                                        expandFrom = 'left';
 
                                                                // Catch invalid values, default to 'auto'
-                                                               } else if ( $.inArray( expandFrom, [ 'left', 'right', 'start', 'end', 'auto' ] ) === -1 ) {
+                                                               } else if ( [ 'left', 'right', 'start', 'end', 'auto' ].indexOf( expandFrom ) === -1 ) {
                                                                        expandFrom = 'auto';
                                                                }
 
                                                ];
                                                if ( context.data.keypressedCount === 0 &&
                                                        e.which === context.data.keypressed &&
-                                                       $.inArray( e.which, allowed ) !== -1
+                                                       allowed.indexOf( e.which ) !== -1
                                                ) {
                                                        $.suggestions.keypress( e, context, context.data.keypressed );
                                                }
index 21209f6..6d67ade 100644 (file)
 
        function uniqueElements( array ) {
                var uniques = [];
-               $.each( array, function ( i, elem ) {
-                       if ( elem !== undefined && $.inArray( elem, uniques ) === -1 ) {
+               array.forEach( function ( elem ) {
+                       if ( elem !== undefined && uniques.indexOf( elem ) === -1 ) {
                                uniques.push( elem );
                        }
                } );
                                                cell = this;
                                                // Get current column index
                                                columns = config.headerToColumns[ $cell.data( 'headerIndex' ) ];
-                                               newSortList = $.map( columns, function ( c ) {
-                                                       // jQuery "helpfully" flattens the arrays...
-                                                       return [ [ c, $cell.data( 'order' ) ] ];
+                                               newSortList = columns.map( function ( c ) {
+                                                       return [ c, $cell.data( 'order' ) ];
                                                } );
                                                // Index of first column belonging to this header
                                                i = columns[ 0 ];
index 31c22af..5ae91e8 100644 (file)
                        mw.util.getParamValue( 'undo' ) !== null ||
                        // Pressing "show changes" and "preview" also signify that the user will
                        // probably save the page soon
-                       $.inArray( $form.find( '#mw-edit-mode' ).val(), [ 'preview', 'diff' ] ) > -1
+                       [ 'preview', 'diff' ].indexOf( $form.find( '#mw-edit-mode' ).val() ) > -1
                ) {
                        checkStash();
                }
index a6450e9..35d82b2 100644 (file)
                                                        for ( j = 0; j < tmp.length; j++ ) {
                                                                availableFormats[ tmp[ j ] ] = true;
                                                        }
-                                                       pi.parameters[ i ].type = $.grep( tmp, filterFmModules );
+                                                       pi.parameters[ i ].type = tmp.filter( filterFmModules );
                                                        pi.parameters[ i ][ 'default' ] = 'json';
                                                        pi.parameters[ i ].required = true;
                                                }
 
                                // Hide the 'wrappedhtml' parameter on format modules
                                if ( pi.group === 'format' ) {
-                                       pi.parameters = $.grep( pi.parameters, function ( p ) {
+                                       pi.parameters = pi.parameters.filter( function ( p ) {
                                                return p.name !== 'wrappedhtml';
                                        } );
                                }
                                                popup: {
                                                        width: 'auto',
                                                        padded: true,
-                                                       $content: $( '<ul>' ).append( $.map( pi.helpurls, function ( link ) {
+                                                       $content: $( '<ul>' ).append( pi.helpurls.map( function ( link ) {
                                                                return $( '<li>' ).append( $( '<a>' )
                                                                        .attr( { href: link, target: '_blank' } )
                                                                        .text( link )
                                                popup: {
                                                        width: 'auto',
                                                        padded: true,
-                                                       $content: $( '<ul>' ).append( $.map( pi.examples, function ( example ) {
+                                                       $content: $( '<ul>' ).append( pi.examples.map( function ( example ) {
                                                                var a = $( '<a>' )
                                                                        .attr( 'href', '#' + example.query )
                                                                        .html( example.description );
index 491a1ff..49e471e 100644 (file)
@@ -31,8 +31,8 @@
                                expiryValue = expiryWidget.dropdowninput.getValue(),
                                // infinityValues  are the values the SpecialBlock class accepts as infinity (sf. wfIsInfinity)
                                infinityValues = [ 'infinite', 'indefinite', 'infinity', 'never' ],
-                               isIndefinite = $.inArray( expiryValue, infinityValues ) !== -1 ||
-                                       ( expiryValue === 'other' && $.inArray( expiryWidget.textinput.getValue(), infinityValues ) !== -1 );
+                               isIndefinite = infinityValues.indexOf( expiryValue ) !== -1 ||
+                                       ( expiryValue === 'other' && infinityValues.indexOf( expiryWidget.textinput.getValue() ) !== -1 );
 
                        if ( enableAutoblockField ) {
                                enableAutoblockField.toggle( !( isNonEmptyIp ) );
index 214ee60..1a3f26c 100644 (file)
                                if (
                                        mw.config.get( 'wgCheckFileExtensions' ) &&
                                        mw.config.get( 'wgStrictFileExtensions' ) &&
-                                       mw.config.get( 'wgFileExtensions' ) &&
+                                       Array.isArray( mw.config.get( 'wgFileExtensions' ) ) &&
                                        $( this ).attr( 'id' ) !== 'wpUploadFileURL'
                                ) {
                                        if (
                                                fname.lastIndexOf( '.' ) === -1 ||
-                                               $.inArray(
-                                                       fname.slice( fname.lastIndexOf( '.' ) + 1 ).toLowerCase(),
-                                                       $.map( mw.config.get( 'wgFileExtensions' ), function ( element ) {
-                                                               return element.toLowerCase();
-                                                       } )
-                                               ) === -1
+                                               mw.config.get( 'wgFileExtensions' ).map( function ( element ) {
+                                                       return element.toLowerCase();
+                                               } ).indexOf( fname.slice( fname.lastIndexOf( '.' ) + 1 ).toLowerCase() ) === -1
                                        ) {
                                                // Not a valid extension
                                                // Clear the upload and set mw-upload-permitted to error
                function fileIsPreviewable( file ) {
                        var known = [ 'image/png', 'image/gif', 'image/jpeg', 'image/svg+xml' ],
                                tooHuge = 10 * 1024 * 1024;
-                       return ( $.inArray( file.type, known ) !== -1 ) && file.size > 0 && file.size < tooHuge;
+                       return ( known.indexOf( file.type ) !== -1 ) && file.size > 0 && file.size < tooHuge;
                }
 
                /**
index 2fcb4be..2e5a92e 100644 (file)
@@ -42,7 +42,7 @@
                        'import',
                        'options'
                ];
-               if ( $.inArray( action, csrfActions ) !== -1 ) {
+               if ( csrfActions.indexOf( action ) !== -1 ) {
                        mw.track( 'mw.deprecate', 'apitoken_' + action );
                        mw.log.warn( 'Use of the "' + action + '" token is deprecated. Use "csrf" instead.' );
                        return 'csrf';
index e1681fa..8b25a0b 100644 (file)
 
                                startTagName = startTagName.toLowerCase();
                                endTagName = endTagName.toLowerCase();
-                               if ( startTagName !== endTagName || $.inArray( startTagName, settings.allowedHtmlElements ) === -1 ) {
+                               if ( startTagName !== endTagName || settings.allowedHtmlElements.indexOf( startTagName ) === -1 ) {
                                        return false;
                                }
 
                                for ( i = 0, len = attributes.length; i < len; i += 2 ) {
                                        attributeName = attributes[ i ];
-                                       if ( $.inArray( attributeName, settings.allowedHtmlCommonAttributes ) === -1 &&
-                                               $.inArray( attributeName, settings.allowedHtmlAttributesByElement[ startTagName ] || [] ) === -1 ) {
+                                       if ( settings.allowedHtmlCommonAttributes.indexOf( attributeName ) === -1 &&
+                                               ( settings.allowedHtmlAttributesByElement[ startTagName ] || [] ).indexOf( attributeName ) === -1 ) {
                                                return false;
                                        }
                                }
index 6a7d0b9..06c34a5 100644 (file)
@@ -23,7 +23,7 @@
                // Try the cache
                if ( cache[ url ] ) {
                        // Update access freshness
-                       cacheOrder.splice( $.inArray( url, cacheOrder ), 1 );
+                       cacheOrder.splice( cacheOrder.indexOf( url ), 1 );
                        cacheOrder.push( url );
                        return $.Deferred().resolve( cache[ url ] ).promise();
                }
index 5b3c2ed..32cda7e 100644 (file)
 
                        function among( actual, expected, message ) {
                                if ( Array.isArray( expected ) ) {
-                                       assert.ok( $.inArray( actual, expected ) !== -1, message + ' (got ' + actual + '; expected one of ' + expected.join( ', ' ) + ')' );
+                                       assert.ok( expected.indexOf( actual ) !== -1, message + ' (got ' + actual + '; expected one of ' + expected.join( ', ' ) + ')' );
                                } else {
                                        assert.equal( actual, expected, message );
                                }
index 4ee8038..997a42c 100644 (file)
@@ -30,7 +30,7 @@
 
                // Requests are POST, match requestBody instead of url
                this.server.respond( function ( request ) {
-                       if ( $.inArray( request.requestBody, [
+                       if ( [
                                // simple
                                'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C',
                                // two options
@@ -43,7 +43,7 @@
                                'action=options&format=json&formatversion=2&change=foo&token=%2B%5C',
                                // reset an option, not bundleable
                                'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C'
-                       ] ) !== -1 ) {
+                       ].indexOf( request.requestBody ) !== -1 ) {
                                assert.ok( true, 'Repond to ' + request.requestBody );
                                request.respond( 200, { 'Content-Type': 'application/json' },
                                        '{ "options": "success" }' );
@@ -88,7 +88,7 @@
 
                // Requests are POST, match requestBody instead of url
                this.server.respond( function ( request ) {
-                       if ( $.inArray( request.requestBody, [
+                       if ( [
                                // simple
                                'action=options&format=json&formatversion=2&change=foo%3Dbar&token=%2B%5C',
                                // two options
                                'action=options&format=json&formatversion=2&change=foo&token=%2B%5C',
                                // reset an option, not bundleable
                                'action=options&format=json&formatversion=2&optionname=foo%7Cbar%3Dquux&token=%2B%5C'
-                       ] ) !== -1 ) {
+                       ].indexOf( request.requestBody ) !== -1 ) {
                                assert.ok( true, 'Repond to ' + request.requestBody );
                                request.respond(
                                        200,
index 5a92da4..417ad3d 100644 (file)
@@ -18,7 +18,7 @@
        }
 
        function sequenceBodies( status, headers, bodies ) {
-               jQuery.each( bodies, function ( i, body ) {
+               bodies.forEach( function ( body, i ) {
                        bodies[ i ] = [ status, headers, body ];
                } );
                return sequence( bodies );
index 67ee3b8..2a563c8 100644 (file)
                        ]
                ];
 
-               $.each( testCases, function () {
+               testCases.forEach( function ( testCase ) {
                        var
-                               key = this[ 0 ],
-                               input = this[ 1 ],
-                               output = this[ 2 ];
+                               key = testCase[ 0 ],
+                               input = testCase[ 1 ],
+                               output = testCase[ 2 ];
                        mw.messages.set( key, input );
                        assert.htmlEqual(
                                formatParse( key ),
                        ]
                ];
 
-               $.each( testCases, function () {
+               testCases.forEach( function ( testCase ) {
                        var
-                               key = this[ 0 ],
-                               input = this[ 1 ],
-                               output = this[ 2 ],
+                               key = testCase[ 0 ],
+                               input = testCase[ 1 ],
+                               output = testCase[ 2 ],
                                paramHref = key.slice( 0, 8 ) === 'wikilink' ? 'Example' : 'http://example.com',
                                paramText = 'Text';
                        mw.messages.set( key, input );