From 1fa8f5bf1199f59e89164b687dec2c83c770403c Mon Sep 17 00:00:00 2001 From: Sergii Stotskyi Date: Fri, 25 Sep 2015 01:50:19 +0300 Subject: [PATCH] chore(cleanup): Replaced Array#forEach with more suitable method: some, map, reduce, filter, etc --- bin/webdriver-manager | 7 ++--- lib/cli.js | 6 ++-- lib/element.js | 69 ++++++++++++++++--------------------------- lib/launcher.js | 26 +++++++--------- 4 files changed, 39 insertions(+), 69 deletions(-) diff --git a/bin/webdriver-manager b/bin/webdriver-manager index 05af9691c..1a5dea1e6 100755 --- a/bin/webdriver-manager +++ b/bin/webdriver-manager @@ -251,11 +251,8 @@ for (name in binaries) { bin = binaries[name]; bin.cdn = argv.alternate_cdn || bin.cdn; var exists = fs.existsSync(path.join(argv.out_dir, bin.filename)); - var outOfDateExists = false; - existingFiles.forEach(function(file) { - if (file.indexOf(bin.prefix) != -1 && file != bin.filename) { - outOfDateExists = true; - } + var outOfDateExists = existingFiles.some(function(file) { + return file.indexOf(bin.prefix) !== -1 && file !== bin.filename; }); bin.exists = exists; bin.outOfDateExists = outOfDateExists; diff --git a/lib/cli.js b/lib/cli.js index 5a17c8754..05ee94f75 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -108,11 +108,9 @@ if (argv.capabilities) { * @param {Array} list */ var processFilePatterns_ = function(list) { - var patterns = list.split(','); - patterns.forEach(function(spec, index, arr) { - arr[index] = path.resolve(process.cwd(), spec); + return list.split(',').map(function(spec) { + return path.resolve(process.cwd(), spec); }); - return patterns; }; if (argv.specs) { diff --git a/lib/element.js b/lib/element.js index d936e0b79..58af9b0ae 100644 --- a/lib/element.js +++ b/lib/element.js @@ -140,27 +140,21 @@ ElementArrayFinder.prototype.all = function(locator) { }); } else { return self.getWebElements().then(function(parentWebElements) { - var childrenPromiseList = []; // For each parent web element, find their children and construct a // list of Promise> - parentWebElements.forEach(function(parentWebElement) { - var childrenPromise = locator.findElementsOverride ? - locator.findElementsOverride(ptor.driver, parentWebElement, ptor.rootEl) : - parentWebElement.findElements(locator); - childrenPromiseList.push(childrenPromise); + var childrenPromiseList = parentWebElements.map(function(parentWebElement) { + return locator.findElementsOverride ? + locator.findElementsOverride(ptor.driver, parentWebElement, ptor.rootEl) : + parentWebElement.findElements(locator); }); - // Resolve the list of Promise> and merge into // a single list - return webdriver.promise.all(childrenPromiseList).then( - function(resolved) { - var childrenList = []; - resolved.forEach(function(resolvedE) { - childrenList = childrenList.concat(resolvedE); - }); - return childrenList; - }); + return webdriver.promise.all(childrenPromiseList).then(function(resolved) { + return resolved.reduce(function(childrenList, resolvedE) { + return childrenList.concat(resolvedE); + }, []); + }); }); } }; @@ -202,21 +196,16 @@ ElementArrayFinder.prototype.filter = function(filterFn) { var self = this; var getWebElements = function() { return self.getWebElements().then(function(parentWebElements) { - var list = []; - parentWebElements.forEach(function(parentWebElement, index) { + var list = parentWebElements.map(function(parentWebElement, index) { var elementFinder = ElementFinder.fromWebElement_(self.ptor_, parentWebElement, self.locator_); - list.push(filterFn(elementFinder, index)); + return filterFn(elementFinder, index); }); return webdriver.promise.all(list).then(function(resolvedList) { - var filteredElementList = []; - resolvedList.forEach(function(result, index) { - if (result) { - filteredElementList.push(parentWebElements[index]); - } + return parentWebElements.filter(function(parentWebElement, index) { + return resolvedList[index]; }); - return filteredElementList; }); }); }; @@ -391,11 +380,7 @@ ElementArrayFinder.prototype.locator = function() { ElementArrayFinder.prototype.applyAction_ = function(actionFn) { var callerError = new Error(); var actionResults = this.getWebElements().then(function(arr) { - var list = []; - arr.forEach(function(webElem) { - list.push(actionFn(webElem)); - }); - return webdriver.promise.all(list); + return webdriver.promise.all(arr.map(actionFn)); }).then(null, function(e) { e.stack = e.stack + '\n' + callerError.stack; throw e; @@ -412,11 +397,9 @@ ElementArrayFinder.prototype.applyAction_ = function(actionFn) { ElementArrayFinder.prototype.asElementFinders_ = function() { var self = this; return this.getWebElements().then(function(arr) { - var list = []; - arr.forEach(function(webElem) { - list.push(ElementFinder.fromWebElement_(self.ptor_, webElem, self.locator_)); + return arr.map(function(webElem) { + return ElementFinder.fromWebElement_(self.ptor_, webElem, self.locator_); }); - return list; }); }; @@ -513,11 +496,10 @@ ElementArrayFinder.prototype.each = function(fn) { */ ElementArrayFinder.prototype.map = function(mapFn) { return this.asElementFinders_().then(function(arr) { - var list = []; - arr.forEach(function(elementFinder, index) { + var list = arr.map(function(elementFinder, index) { var mapResult = mapFn(elementFinder, index); // All nested arrays and objects will also be fully resolved. - list.push(webdriver.promise.fullyResolved(mapResult)); + return webdriver.promise.fullyResolved(mapResult); }); return webdriver.promise.all(list); }); @@ -557,12 +539,11 @@ ElementArrayFinder.prototype.map = function(mapFn) { ElementArrayFinder.prototype.reduce = function(reduceFn, initialValue) { var valuePromise = webdriver.promise.fulfilled(initialValue); return this.asElementFinders_().then(function(arr) { - arr.forEach(function(elementFinder, index) { - valuePromise = valuePromise.then(function(value) { + return arr.reduce(function(valuePromise, elementFinder, index) { + return valuePromise.then(function(value) { return reduceFn(value, elementFinder, index, arr); }); - }); - return valuePromise; + }, valuePromise); }); }; @@ -650,7 +631,7 @@ ElementArrayFinder.prototype.allowAnimations = function(value) { * expect(input.getAttribute('value')).toBe('Foo123'); * * @constructor - * @extends {webdriver.WebElement} + * @extends {webdriver.WebElement} * @param {Protractor} ptor * @param {ElementArrayFinder} elementArrayFinder The ElementArrayFinder * that this is branched from. @@ -930,7 +911,7 @@ ElementFinder.prototype.isPresent = function() { /** * Same as ElementFinder.isPresent(), except this checks whether the element - * identified by the subLocator is present, rather than the current element + * identified by the subLocator is present, rather than the current element * finder. i.e. `element(by.css('#abc')).element(by.css('#def')).isPresent()` is * identical to `element(by.css('#abc')).isElementPresent(by.css('#def'))`. * @@ -942,8 +923,8 @@ ElementFinder.prototype.isPresent = function() { */ ElementFinder.prototype.isElementPresent = function(subLocator) { if (!subLocator) { - throw new Error('SubLocator is not supplied as a parameter to ' + - '`isElementPresent(subLocator)`. You are probably looking for the ' + + throw new Error('SubLocator is not supplied as a parameter to ' + + '`isElementPresent(subLocator)`. You are probably looking for the ' + 'function `isPresent()`.'); } return this.element(subLocator).isPresent(); diff --git a/lib/launcher.js b/lib/launcher.js index 9b5c42378..dfb4b723e 100644 --- a/lib/launcher.js +++ b/lib/launcher.js @@ -37,28 +37,21 @@ var taskResults_ = { }, totalSpecFailures: function() { - var specFailures = 0; - this.results_.forEach(function(result) { - specFailures += result.failedCount; - }); - return specFailures; + return this.results_.reduce(function(specFailures, result) { + return specFailures + result.failedCount; + }, 0); }, totalProcessFailures: function() { - var processFailures = 0; - this.results_.forEach(function(result) { - if (!result.failedCount && result.exitCode !== 0) { - processFailures += 1; - } - }); - return processFailures; + return this.results_.reduce(function(processFailures, result) { + return !result.failedCount && result.exitCode !== 0 ? processFailures + 1 : processFailures; + }, 0); }, saveResults: function(filepath) { - var jsonOutput = []; - this.results_.forEach(function(result) { - jsonOutput = jsonOutput.concat(result.specResults); - }); + var jsonOutput = this.results_.reduce(function(jsonOutput, result) { + return jsonOutput.concat(result.specResults); + }, []); var json = JSON.stringify(jsonOutput, null, ' '); var fs = require('fs'); @@ -267,3 +260,4 @@ var init = function(configFile, additionalConfig) { }; exports.init = init; +