Add _.mean.

This commit is contained in:
Agus Pina
2015-11-10 16:21:01 -03:00
committed by John-David Dalton
parent f0e8371997
commit 3408db91b6
3 changed files with 52 additions and 24 deletions

View File

@@ -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,' +

View File

@@ -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;

View File

@@ -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([]));