You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1.1 KiB
33 lines
1.1 KiB
1 year ago
|
'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;
|
||
|
}
|
||
|
});
|