Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reduceRight in Protractor

There are two ways to reduce regular javascript arrays:

  • reduce() (from-left-to-right)
  • reduceRight() (from-right-to-left)

In Protractor, there is reduce() which would work from the left element to the very right:

Apply a reduce function against an accumulator and every element found using the locator (from left-to-right).

Is there reduceRight() method in Protractor and, if not, would it be possible to implement it based on reduce()?

like image 644
alecxe Avatar asked Apr 16 '26 06:04

alecxe


1 Answers

Is there reduceRight() method in Protractor?

No, protractor doesn't support this method.

Would it be possible to implement it based on reduce()?

Sure, just replace the arr.reduce by arr.reduceRight in the reduce method:

protractor.ElementArrayFinder.prototype.reduceRight = function (reduceFn, initialValue) {
  var valuePromise = protractor.promise.fulfilled(initialValue);
  return this.asElementFinders_().then(function(arr) {
    return arr.reduceRight(function(valuePromise, elementFinder, index) {
      return valuePromise.then(function(value) {
        return reduceFn(value, elementFinder, index, arr);
      });
    }, valuePromise);
  });
}
like image 142
Florent B. Avatar answered Apr 17 '26 19:04

Florent B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!