From 6207dd35d6da47cdf2c9c1bd2ce49ecf1de94375 Mon Sep 17 00:00:00 2001 From: Fomafix Date: Sun, 21 Jan 2018 10:45:43 +0100 Subject: [PATCH 1/1] mediawiki.ForeignStructuredUpload: Simplify code by using ES5 For iterating over an array replace for() by .push.apply() and .map(). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push#Merging_two_arrays https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map Change-Id: I0bb4ed7f71db1e60eb4d76a77c53d022bb2081d1 --- .../mediawiki.ForeignStructuredUpload.js | 37 ++++++------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.js b/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.js index 0c572d4991..4d569402b0 100644 --- a/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.js +++ b/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.js @@ -86,12 +86,9 @@ * @param {string[]} categories Array of categories to which this upload will be added. */ ForeignStructuredUpload.prototype.addCategories = function ( categories ) { - var i, category; - - for ( i = 0; i < categories.length; i++ ) { - category = categories[ i ]; - this.categories.push( category ); - } + // The length of the array must be less than 10000. + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push#Merging_two_arrays + Array.prototype.push.apply( this.categories, categories ); }; /** @@ -183,18 +180,11 @@ * @return {string} */ ForeignStructuredUpload.prototype.getDescriptions = function () { - var i, desc, templateCalls = []; - - for ( i = 0; i < this.descriptions.length; i++ ) { - desc = this.descriptions[ i ]; - templateCalls.push( - this.config.format.description - .replace( '$LANGUAGE', desc.language ) - .replace( '$TEXT', desc.text ) - ); - } - - return templateCalls.join( '\n' ); + return this.descriptions.map( function ( desc ) { + return this.config.format.description + .replace( '$LANGUAGE', desc.language ) + .replace( '$TEXT', desc.text ); + } ).join( '\n' ); }; /** @@ -205,18 +195,13 @@ * @return {string} */ ForeignStructuredUpload.prototype.getCategories = function () { - var i, cat, categoryLinks = []; - if ( this.categories.length === 0 ) { return this.config.format.uncategorized; } - for ( i = 0; i < this.categories.length; i++ ) { - cat = this.categories[ i ]; - categoryLinks.push( '[[Category:' + cat + ']]' ); - } - - return categoryLinks.join( '\n' ); + return this.categories.map( function ( cat ) { + return '[[Category:' + cat + ']]'; + } ).join( '\n' ); }; /** -- 2.20.1