| 1 | define(['./_isArrayLike', './keys', './_optimizeCb'], function (_isArrayLike, keys, _optimizeCb) {
|
|---|
| 2 |
|
|---|
| 3 | // Internal helper to create a reducing function, iterating left or right.
|
|---|
| 4 | function createReduce(dir) {
|
|---|
| 5 | // Wrap code that reassigns argument variables in a separate function than
|
|---|
| 6 | // the one that accesses `arguments.length` to avoid a perf hit. (#1991)
|
|---|
| 7 | var reducer = function(obj, iteratee, memo, initial) {
|
|---|
| 8 | var _keys = !_isArrayLike(obj) && keys(obj),
|
|---|
| 9 | length = (_keys || obj).length,
|
|---|
| 10 | index = dir > 0 ? 0 : length - 1;
|
|---|
| 11 | if (!initial) {
|
|---|
| 12 | memo = obj[_keys ? _keys[index] : index];
|
|---|
| 13 | index += dir;
|
|---|
| 14 | }
|
|---|
| 15 | for (; index >= 0 && index < length; index += dir) {
|
|---|
| 16 | var currentKey = _keys ? _keys[index] : index;
|
|---|
| 17 | memo = iteratee(memo, obj[currentKey], currentKey, obj);
|
|---|
| 18 | }
|
|---|
| 19 | return memo;
|
|---|
| 20 | };
|
|---|
| 21 |
|
|---|
| 22 | return function(obj, iteratee, memo, context) {
|
|---|
| 23 | var initial = arguments.length >= 3;
|
|---|
| 24 | return reducer(obj, _optimizeCb(iteratee, context, 4), memo, initial);
|
|---|
| 25 | };
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | return createReduce;
|
|---|
| 29 |
|
|---|
| 30 | });
|
|---|