Skip to content

Commit

Permalink
chore(cleanup): Replaced Array#forEach with more suitable method: som…
Browse files Browse the repository at this point in the history
…e, map, reduce, filter, etc
  • Loading branch information
stalniy authored and sjelin committed Nov 9, 2015
1 parent 2b025f6 commit 1fa8f5b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 69 deletions.
7 changes: 2 additions & 5 deletions bin/webdriver-manager
Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions lib/cli.js
Expand Up @@ -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) {
Expand Down
69 changes: 25 additions & 44 deletions lib/element.js
Expand Up @@ -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<List<child_web_element>>
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<List<child_web_elements>> 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);
}, []);
});
});
}
};
Expand Down Expand Up @@ -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;
});
});
};
Expand Down Expand Up @@ -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;
Expand All @@ -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;
});
};

Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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);
});
};

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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'))`.
*
Expand All @@ -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();
Expand Down
26 changes: 10 additions & 16 deletions lib/launcher.js
Expand Up @@ -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');
Expand Down Expand Up @@ -267,3 +260,4 @@ var init = function(configFile, additionalConfig) {
};

exports.init = init;

0 comments on commit 1fa8f5b

Please sign in to comment.