From 3408db91b6beecb10bd1f2dd8abbdbbf352521fe Mon Sep 17 00:00:00 2001 From: Agus Pina Date: Tue, 10 Nov 2015 16:21:01 -0300 Subject: [PATCH] Add `_.mean`. --- lib/fp/mapping.js | 12 ++++++------ lodash.js | 49 +++++++++++++++++++++++++++++++---------------- test/test.js | 15 ++++++++++++++- 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/lib/fp/mapping.js b/lib/fp/mapping.js index 6286eacfc..a30bf9349 100644 --- a/lib/fp/mapping.js +++ b/lib/fp/mapping.js @@ -84,12 +84,12 @@ module.exports = { 'dropWhile,endsWith,every,extend,filter,find,find,findIndex,findKey,findLast,' + 'findLastIndex,findLastKey,forEach,forEachRight,forIn,forInRight,forOwn,' + 'forOwnRight,get,groupBy,includes,indexBy,indexOf,intersection,invoke,isMatch,' + - 'lastIndexOf,map,mapKeys,mapValues,matchesProperty,maxBy,minBy,merge,modArgs,' + - 'modArgsSet,omit,pad,padLeft,padRight,parseInt,partition,pick,pull,pullAll,' + - 'pullAt,random,range,rearg,reject,remove,repeat,result,sampleSize,set,some,' + - 'sortBy,sortByOrder,sortedIndexBy,sortedLastIndexBy,sortedUniqBy,startsWith,' + - 'subtract,sumBy,take,takeRight,takeRightWhile,takeWhile,throttle,times,' + - 'truncate,union,uniqBy,without,wrap,xor,zip,zipObject').split(','), + 'lastIndexOf,map,mapKeys,mapValues,matchesProperty,maxBy,mean,minBy,merge,' + + 'modArgs,modArgsSet,omit,pad,padLeft,padRight,parseInt,partition,pick,pull,' + + 'pullAll,pullAt,random,range,rearg,reject,remove,repeat,result,sampleSize,' + + 'set,some,sortBy,sortByOrder,sortedIndexBy,sortedLastIndexBy,sortedUniqBy,' + + 'startsWith,subtract,sumBy,take,takeRight,takeRightWhile,takeWhile,throttle,' + + 'times,truncate,union,uniqBy,without,wrap,xor,zip,zipObject').split(','), 3: ( 'assignWith,clamp,differenceBy,extendWith,getOr,inRange,intersectionBy,' + 'isEqualWith,isMatchWith,mergeWith,omitBy,pickBy,pullAllBy,reduce,' + diff --git a/lodash.js b/lodash.js index 034e6d353..58cf52f6a 100644 --- a/lodash.js +++ b/lodash.js @@ -1521,15 +1521,15 @@ * `isNaN`, `isNative`, `isNil`, `isNull`, `isNumber`, `isObject`, `isObjectLike`, * `isPlainObject`, `isRegExp`, `isSafeInteger`, `isString`, `isUndefined`, * `isTypedArray`, `join`, `kebabCase`, `last`, `lastIndexOf`, `lowerCase`, - * `lowerFirst`, `lt`, `lte`, `max`, `maxBy`, `min`, `minBy`, `noConflict`, - * `noop`, `now`, `pad`, `padLeft`, `padRight`, `parseInt`, `pop`, `random`, - * `reduce`, `reduceRight`, `repeat`, `result`, `round`, `runInContext`, - * `sample`, `shift`, `size`, `snakeCase`, `some`, `sortedIndex`, `sortedIndexBy`, - * `sortedLastIndex`, `sortedLastIndexBy`, `startCase`, `startsWith`, `subtract`, - * `sum`, sumBy`, `template`, `toLower`, `toInteger`, `toLength`, `toNumber`, - * `toSafeInteger`, toString`, `toUpper`, `trim`, `trimLeft`, `trimRight`, - * `truncate`, `unescape`, `uniqueId`, `upperCase`, `upperFirst`, `value`, - * and `words` + * `lowerFirst`, `lt`, `lte`, `max`, `maxBy`, `mean`, `min`, `minBy`, + * `noConflict`, `noop`, `now`, `pad`, `padLeft`, `padRight`, `parseInt`, + * `pop`, `random`, `reduce`, `reduceRight`, `repeat`, `result`, `round`, + * `runInContext`, `sample`, `shift`, `size`, `snakeCase`, `some`, `sortedIndex`, + * `sortedIndexBy`, `sortedLastIndex`, `sortedLastIndexBy`, `startCase`, + * `startsWith`, `subtract`, `sum`, sumBy`, `template`, `toLower`, `toInteger`, + * `toLength`, `toNumber`, `toSafeInteger`, toString`, `toUpper`, `trim`, + * `trimLeft`, `trimRight`, `truncate`, `unescape`, `uniqueId`, `upperCase`, + * `upperFirst`, `value`, and `words` * * @name _ * @constructor @@ -13335,6 +13335,23 @@ : undefined; } + /** + * Computes the mean value of an `array`. + * + * @static + * @memberOf _ + * @category Math + * @param {Array} array The array to iterate over. + * @returns {number} Returns the mean. + * @example + * + * _.mean([4, 2, 8, 6]); + * // => 5 + */ + function mean(array) { + return sum(array) / (array ? array.length : 0); + } + /** * Gets the minimum value of `array`. If `array` is empty or falsey * `undefined` is returned. @@ -13446,8 +13463,8 @@ * @returns {number} Returns the sum. * @example * - * _.sum([4, 6]); - * // => 10 + * _.sum([4, 2, 8, 6]); + * // => 20 */ function sum(array) { return (array && array.length) @@ -13468,17 +13485,14 @@ * @returns {number} Returns the sum. * @example * - * var objects = [ - * { 'n': 4 }, - * { 'n': 6 } - * ]; + * var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }]; * * _.sumBy(objects, function(o) { return o.n; }); - * // => 10 + * // => 20 * * // using the `_.property` callback shorthand * _.sumBy(objects, 'n'); - * // => 10 + * // => 20 */ function sumBy(array, iteratee) { return (array && array.length) @@ -13747,6 +13761,7 @@ lodash.lte = lte; lodash.max = max; lodash.maxBy = maxBy; + lodash.mean = mean; lodash.min = min; lodash.minBy = minBy; lodash.noConflict = noConflict; diff --git a/test/test.js b/test/test.js index ecf040102..17280376b 100644 --- a/test/test.js +++ b/test/test.js @@ -12207,6 +12207,19 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.mean'); + + (function() { + QUnit.test('should return the mean of an array of numbers', function(assert) { + assert.expect(1); + + var array = [4, 2, 8, 6]; + assert.strictEqual(_.mean(array), 5); + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.memoize'); (function() { @@ -22426,7 +22439,7 @@ var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); QUnit.test('should accept falsey arguments', function(assert) { - assert.expect(277); + assert.expect(278); var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));