UNPKG

iteragain

Version:

Javascript Iterable/Iterator/Generator-function utilities.

28 lines 960 B
import { REDUCE_EMPTY_ERROR } from './internal/emptyIteratorError'; import toIterator from './toIterator'; export function reduce() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } if (typeof args[0] === 'function') return function (it) { return reduce(it, args[0], args[1]); }; var it = toIterator(args[0]); var next; var reducer = args[1]; if (args[2] === undefined) { var first = it.next(); if (first.done) throw new TypeError(REDUCE_EMPTY_ERROR); var accumulator_1 = first.value; while (!(next = it.next()).done) accumulator_1 = reducer(accumulator_1, next.value); return accumulator_1; } var accumulator = args[2]; while (!(next = it.next()).done) accumulator = reducer(accumulator, next.value); return accumulator; } export default reduce; //# sourceMappingURL=reduce.js.map