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,' + 'dropWhile,endsWith,every,extend,filter,find,find,findIndex,findKey,findLast,' +
'findLastIndex,findLastKey,forEach,forEachRight,forIn,forInRight,forOwn,' + 'findLastIndex,findLastKey,forEach,forEachRight,forIn,forInRight,forOwn,' +
'forOwnRight,get,groupBy,includes,indexBy,indexOf,intersection,invoke,isMatch,' + 'forOwnRight,get,groupBy,includes,indexBy,indexOf,intersection,invoke,isMatch,' +
'lastIndexOf,map,mapKeys,mapValues,matchesProperty,maxBy,minBy,merge,modArgs,' + 'lastIndexOf,map,mapKeys,mapValues,matchesProperty,maxBy,mean,minBy,merge,' +
'modArgsSet,omit,pad,padLeft,padRight,parseInt,partition,pick,pull,pullAll,' + 'modArgs,modArgsSet,omit,pad,padLeft,padRight,parseInt,partition,pick,pull,' +
'pullAt,random,range,rearg,reject,remove,repeat,result,sampleSize,set,some,' + 'pullAll,pullAt,random,range,rearg,reject,remove,repeat,result,sampleSize,' +
'sortBy,sortByOrder,sortedIndexBy,sortedLastIndexBy,sortedUniqBy,startsWith,' + 'set,some,sortBy,sortByOrder,sortedIndexBy,sortedLastIndexBy,sortedUniqBy,' +
'subtract,sumBy,take,takeRight,takeRightWhile,takeWhile,throttle,times,' + 'startsWith,subtract,sumBy,take,takeRight,takeRightWhile,takeWhile,throttle,' +
'truncate,union,uniqBy,without,wrap,xor,zip,zipObject').split(','), 'times,truncate,union,uniqBy,without,wrap,xor,zip,zipObject').split(','),
3: ( 3: (
'assignWith,clamp,differenceBy,extendWith,getOr,inRange,intersectionBy,' + 'assignWith,clamp,differenceBy,extendWith,getOr,inRange,intersectionBy,' +
'isEqualWith,isMatchWith,mergeWith,omitBy,pickBy,pullAllBy,reduce,' + 'isEqualWith,isMatchWith,mergeWith,omitBy,pickBy,pullAllBy,reduce,' +

View File

@@ -1521,15 +1521,15 @@
* `isNaN`, `isNative`, `isNil`, `isNull`, `isNumber`, `isObject`, `isObjectLike`, * `isNaN`, `isNative`, `isNil`, `isNull`, `isNumber`, `isObject`, `isObjectLike`,
* `isPlainObject`, `isRegExp`, `isSafeInteger`, `isString`, `isUndefined`, * `isPlainObject`, `isRegExp`, `isSafeInteger`, `isString`, `isUndefined`,
* `isTypedArray`, `join`, `kebabCase`, `last`, `lastIndexOf`, `lowerCase`, * `isTypedArray`, `join`, `kebabCase`, `last`, `lastIndexOf`, `lowerCase`,
* `lowerFirst`, `lt`, `lte`, `max`, `maxBy`, `min`, `minBy`, `noConflict`, * `lowerFirst`, `lt`, `lte`, `max`, `maxBy`, `mean`, `min`, `minBy`,
* `noop`, `now`, `pad`, `padLeft`, `padRight`, `parseInt`, `pop`, `random`, * `noConflict`, `noop`, `now`, `pad`, `padLeft`, `padRight`, `parseInt`,
* `reduce`, `reduceRight`, `repeat`, `result`, `round`, `runInContext`, * `pop`, `random`, `reduce`, `reduceRight`, `repeat`, `result`, `round`,
* `sample`, `shift`, `size`, `snakeCase`, `some`, `sortedIndex`, `sortedIndexBy`, * `runInContext`, `sample`, `shift`, `size`, `snakeCase`, `some`, `sortedIndex`,
* `sortedLastIndex`, `sortedLastIndexBy`, `startCase`, `startsWith`, `subtract`, * `sortedIndexBy`, `sortedLastIndex`, `sortedLastIndexBy`, `startCase`,
* `sum`, sumBy`, `template`, `toLower`, `toInteger`, `toLength`, `toNumber`, * `startsWith`, `subtract`, `sum`, sumBy`, `template`, `toLower`, `toInteger`,
* `toSafeInteger`, toString`, `toUpper`, `trim`, `trimLeft`, `trimRight`, * `toLength`, `toNumber`, `toSafeInteger`, toString`, `toUpper`, `trim`,
* `truncate`, `unescape`, `uniqueId`, `upperCase`, `upperFirst`, `value`, * `trimLeft`, `trimRight`, `truncate`, `unescape`, `uniqueId`, `upperCase`,
* and `words` * `upperFirst`, `value`, and `words`
* *
* @name _ * @name _
* @constructor * @constructor
@@ -13335,6 +13335,23 @@
: undefined; : 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 * Gets the minimum value of `array`. If `array` is empty or falsey
* `undefined` is returned. * `undefined` is returned.
@@ -13446,8 +13463,8 @@
* @returns {number} Returns the sum. * @returns {number} Returns the sum.
* @example * @example
* *
* _.sum([4, 6]); * _.sum([4, 2, 8, 6]);
* // => 10 * // => 20
*/ */
function sum(array) { function sum(array) {
return (array && array.length) return (array && array.length)
@@ -13468,17 +13485,14 @@
* @returns {number} Returns the sum. * @returns {number} Returns the sum.
* @example * @example
* *
* var objects = [ * var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
* { 'n': 4 },
* { 'n': 6 }
* ];
* *
* _.sumBy(objects, function(o) { return o.n; }); * _.sumBy(objects, function(o) { return o.n; });
* // => 10 * // => 20
* *
* // using the `_.property` callback shorthand * // using the `_.property` callback shorthand
* _.sumBy(objects, 'n'); * _.sumBy(objects, 'n');
* // => 10 * // => 20
*/ */
function sumBy(array, iteratee) { function sumBy(array, iteratee) {
return (array && array.length) return (array && array.length)
@@ -13747,6 +13761,7 @@
lodash.lte = lte; lodash.lte = lte;
lodash.max = max; lodash.max = max;
lodash.maxBy = maxBy; lodash.maxBy = maxBy;
lodash.mean = mean;
lodash.min = min; lodash.min = min;
lodash.minBy = minBy; lodash.minBy = minBy;
lodash.noConflict = noConflict; 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'); QUnit.module('lodash.memoize');
(function() { (function() {
@@ -22426,7 +22439,7 @@
var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey);
QUnit.test('should accept falsey arguments', function(assert) { QUnit.test('should accept falsey arguments', function(assert) {
assert.expect(277); assert.expect(278);
var emptyArrays = lodashStable.map(falsey, lodashStable.constant([])); var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));