All files / src/aggregate Iterator.aggregate.js

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23    19x 18x 1x       17x   17x 8x   9x       17x 39x   17x    
import Iterator from '../iterators/Iterator';
 
Iterator.prototype.aggregate = function aggregate(accumulator, initialValue) {
  if (typeof accumulator !== 'function') {
    throw new Error('Missing accumulator function');
  }
 
  let result;
  let iterator = this[Symbol.iterator]();
 
  if (typeof initialValue === 'undefined') {
    result = iterator.next().value;
  } else {
    result = initialValue;
  }
 
  let current;
  while ((current = iterator.next()) && current && !current.done) {
    result = accumulator(result, current.value);
  }
  return result;
};