iteragain
Version:
Javascript Iterable/Iterator/Generator-function utilities.
27 lines • 978 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.reduce = reduce;
const emptyIteratorError_1 = require("./internal/emptyIteratorError");
const toIterator_1 = require("./toIterator");
function reduce(...args) {
if (typeof args[0] === 'function')
return it => reduce(it, args[0], args[1]);
const it = (0, toIterator_1.default)(args[0]);
let next;
const reducer = args[1];
if (args[2] === undefined) {
const first = it.next();
if (first.done)
throw new TypeError(emptyIteratorError_1.REDUCE_EMPTY_ERROR);
let accumulator = first.value;
while (!(next = it.next()).done)
accumulator = reducer(accumulator, next.value);
return accumulator;
}
let accumulator = args[2];
while (!(next = it.next()).done)
accumulator = reducer(accumulator, next.value);
return accumulator;
}
exports.default = reduce;
//# sourceMappingURL=reduce.js.map