'use strict'; var $ = require('../internals/export'); var iterate = require('../internals/iterate'); var aCallable = require('../internals/a-callable'); var anObject = require('../internals/an-object'); var getIteratorDirect = require('../internals/get-iterator-direct'); var $TypeError = TypeError; // `Iterator.prototype.reduce` method // https://github.com/tc39/proposal-iterator-helpers $({ target: 'Iterator', proto: true, real: true }, { reduce: function reduce(reducer /* , initialValue */) { anObject(this); aCallable(reducer); var record = getIteratorDirect(this); var noInitial = arguments.length < 2; var accumulator = noInitial ? undefined : arguments[1]; var counter = 0; iterate(record, function (value) { if (noInitial) { noInitial = false; accumulator = value; } else { accumulator = reducer(accumulator, value, counter); } counter++; }, { IS_RECORD: true }); if (noInitial) throw new $TypeError('Reduce of empty iterator with no initial value'); return accumulator; } });