diff --git a/lodash.src.js b/lodash.src.js index 3e4710e07..ad59eade1 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -1699,6 +1699,16 @@ return false; } + function arraySum(array) { + var length = array.length, + result = 0; + + while (length--) { + result += +array[length] || 0; + } + return result; + } + /** * Used by `_.defaults` to customize its `_.assign` use. * @@ -2747,6 +2757,14 @@ }); } + function baseSum(collection, iteratee) { + var result = 0; + baseEach(collection, function(value, index, collection) { + result += +iteratee(value, index, collection) || 0; + }); + return result; + } + /** * The base implementation of `_.uniq` without support for callback shorthands * and `this` binding. @@ -11333,9 +11351,6 @@ if (thisArg && isIterateeCall(collection, iteratee, thisArg)) { iteratee = null; } - if (!isArray(collection)) { - collection = toIterable(collection); - } var func = getCallback(), noIteratee = iteratee == null; @@ -11343,14 +11358,9 @@ noIteratee = false; iteratee = func(iteratee, thisArg, 3); } - var length = collection.length, - result = 0; - - while (length--) { - var value = collection[length]; - result += +(noIteratee ? value : iteratee(value)) || 0; - } - return result; + return noIteratee + ? arraySum(isArray(collection) ? collection : toIterable(collection)) + : baseSum(collection, iteratee); } /*------------------------------------------------------------------------*/ diff --git a/test/test.js b/test/test.js index c7d17e81f..bd1b45497 100644 --- a/test/test.js +++ b/test/test.js @@ -13515,6 +13515,23 @@ deepEqual(actual, 6); }); + test('should provide the correct `iteratee` arguments', 2, function() { + var args; + + _.sum(array, function() { + args || (args = slice.call(arguments)); + }); + + deepEqual(args, [6, 0, array]); + + args = null; + _.sum(object, function() { + args || (args = slice.call(arguments)); + }); + + deepEqual(args, [2, 'a', object]); + }); + test('should support the `thisArg` argument', 1, function() { var actual = _.sum([6.8, 4.5, 2.6], function(num) { return this.floor(num);