From f3b35919efe63829c32f9b8fc31cf985ef4f3bac Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 25 Mar 2016 07:19:37 -0700 Subject: [PATCH] Add `baseMean` helper. --- lodash.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lodash.js b/lodash.js index ed655d8bb..ce3614931 100644 --- a/lodash.js +++ b/lodash.js @@ -811,6 +811,20 @@ return -1; } + /** + * The base implementation of `_.mean` and `_.meanBy` without support for + * iteratee shorthands. + * + * @private + * @param {Array} array The array to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @returns {number} Returns the mean. + */ + function baseMean(array, iteratee) { + var length = array ? array.length : 0; + return length ? (baseSum(array, iteratee) / length) : NAN; + } + /** * The base implementation of `_.reduce` and `_.reduceRight`, without support * for iteratee shorthands, which iterates over `collection` using `eachFunc`. @@ -853,7 +867,8 @@ } /** - * The base implementation of `_.sum` without support for iteratee shorthands. + * The base implementation of `_.sum` and `_.sumBy` without support for + * iteratee shorthands. * * @private * @param {Array} array The array to iterate over. @@ -14820,7 +14835,7 @@ * // => 5 */ function mean(array) { - return meanBy(array, identity); + return baseMean(array, identity); } /** @@ -14847,7 +14862,7 @@ * // => 5 */ function meanBy(array, iteratee) { - return sumBy(array, iteratee) / (array ? array.length : 0); + return baseMean(array, getIteratee(iteratee)); } /**