es-toolkit
Version:
A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.
18 lines (16 loc) • 467 B
JavaScript
function reduce(set, callback, initialValue) {
if (initialValue == null && set.size === 0) {
throw new TypeError('Reduce of empty set with no initial value');
}
let accumulator = initialValue;
for (const value of set) {
if (accumulator == null) {
accumulator = value;
}
else {
accumulator = callback(accumulator, value, value, set);
}
}
return accumulator;
}
export { reduce };