Skip to content

Commit

Permalink
Enable customization of accumulator initialization in createReduce
Browse files Browse the repository at this point in the history
  • Loading branch information
jgonggrijp committed Feb 4, 2021
1 parent c654128 commit fca3114
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
11 changes: 8 additions & 3 deletions modules/_createReduce.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import findLastIndex from './findLastIndex.js';
import isArrayLike from './_isArrayLike.js';
import keys from './keys.js';
import identity from './identity.js';
import each from './each.js';
import bindCb from './_bindCb.js';

Expand All @@ -17,8 +18,12 @@ function eachRight(obj, func) {
}

// Create a reducing function iterating left or right.
export default function createReduce(dir) {
// `customInit` can be used to control how the accumulator is
// initialized from the first element. Defaults to using the element
// directly.
export default function createReduce(dir, customInit) {
var loop = dir > 0 ? each : eachRight;
customInit || (customInit = identity);

// Wrap code that reassigns argument variables in a separate function than
// the one that accesses `arguments.length` to avoid a perf hit. (#1991)
Expand All @@ -27,9 +32,9 @@ export default function createReduce(dir) {
// Make the `iteratee` change identity temporarily so that it only sets
// the `memo` on the first iteration.
var actualIteratee = iteratee;
iteratee = function(memo, value) {
iteratee = function(memo, value, key) {
iteratee = actualIteratee;
return value;
return customInit(value, key, obj);
}
}
loop(obj, function(value, key, obj) {
Expand Down
10 changes: 7 additions & 3 deletions underscore-esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion underscore-esm.js.map

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions underscore.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion underscore.js.map

Large diffs are not rendered by default.

0 comments on commit fca3114

Please sign in to comment.