UNPKG

@reactivex/ix-esnext-esm

Version:

The Interactive Extensions for JavaScript

31 lines (29 loc) 926 B
export function reduce(source, optionsOrAccumulator, seed) { const options = // eslint-disable-next-line no-nested-ternary typeof optionsOrAccumulator === 'function' ? arguments.length > 2 ? { 'callback': optionsOrAccumulator, 'seed': seed } : { 'callback': optionsOrAccumulator } : optionsOrAccumulator; const { ['seed']: _seed, ['callback']: callback } = options; const hasSeed = options.hasOwnProperty('seed'); let i = 0; let hasValue = false; let acc = _seed; for (const item of source) { if (hasValue || (hasValue = hasSeed)) { acc = callback(acc, item, i++); } else { acc = item; hasValue = true; i++; } } if (!(hasSeed || hasValue)) { throw new Error('Sequence contains no elements'); } return acc; } //# sourceMappingURL=reduce.mjs.map