(feature) Add _.mean and _.sumBy

This commit is contained in:
DomiR
2016-01-23 14:43:48 +01:00
parent 5919b1b39d
commit ccc411a625
2 changed files with 240 additions and 111 deletions
+61 -33
View File
@@ -6442,6 +6442,18 @@ module TestMaxBy {
result = _(dictionary).maxBy<{a: number}, number>({a: 42});
}
// _.mean
module TestMean {
let array: number[];
let result: number;
result = _.mean<number>(array);
result = _(array).mean();
}
// _.min
module TestMin {
let array: number[];
@@ -6533,58 +6545,74 @@ module TestSum {
result = _.sum(array);
result = _.sum<number>(array);
result = _.sum<number>(array, listIterator);
result = _.sum<number>(array, listIterator, any);
result = _.sum<number>(array, '');
result = _.sum(list);
result = _.sum<number>(list);
result = _.sum<number>(list, listIterator);
result = _.sum<number>(list, listIterator, any);
result = _.sum<number>(list, '');
result = _.sum(dictionary);
result = _.sum<number>(dictionary);
result = _.sum<number>(dictionary, dictionaryIterator);
result = _.sum<number>(dictionary, dictionaryIterator, any);
result = _.sum<number>(dictionary, '');
result = _(array).sum();
result = _(array).sum(listIterator);
result = _(array).sum(listIterator, any);
result = _(array).sum('');
result = _(list).sum();
result = _(list).sum<number>(listIterator);
result = _(list).sum<number>(listIterator, any);
result = _(list).sum('');
result = _(dictionary).sum();
result = _(dictionary).sum<number>(dictionaryIterator);
result = _(dictionary).sum<number>(dictionaryIterator, any);
result = _(dictionary).sum('');
}
{
let result: _.LoDashExplicitWrapper<number>;
result = _(array).chain().sum();
result = _(array).chain().sum(listIterator);
result = _(array).chain().sum(listIterator, any);
result = _(array).chain().sum('');
result = _(list).chain().sum();
result = _(list).chain().sum<number>(listIterator);
result = _(list).chain().sum<number>(listIterator, any);
result = _(list).chain().sum('');
result = _(dictionary).chain().sum();
result = _(dictionary).chain().sum<number>(dictionaryIterator);
result = _(dictionary).chain().sum<number>(dictionaryIterator, any);
result = _(dictionary).chain().sum('');
}
}
// _.sumBy
module TestSumBy {
let array: number[];
let list: _.List<number>;
let dictionary: _.Dictionary<number>;
let listIterator: (value: number, index: number, collection: _.List<number>) => number;
let dictionaryIterator: (value: number, key: string, collection: _.Dictionary<number>) => number;
{
let result: number;
result = _.sumBy<number>(array);
result = _.sumBy<number>(array, listIterator);
result = _.sumBy<number>(array, '');
result = _.sumBy<number>(list);
result = _.sumBy<number>(list, listIterator);
result = _.sumBy<number>(list, '');
result = _.sumBy<number>(dictionary);
result = _.sumBy<number>(dictionary, dictionaryIterator);
result = _.sumBy<number>(dictionary, '');
result = _(array).sumBy(listIterator);
result = _(array).sumBy('');
result = _(list).sumBy<number>(listIterator);
result = _(list).sumBy('');
result = _(dictionary).sumBy<number>(dictionaryIterator);
result = _(dictionary).sumBy('');
}
{
let result: _.LoDashExplicitWrapper<number>;
result = _(array).chain().sumBy(listIterator);
result = _(array).chain().sumBy('');
result = _(list).chain().sumBy<number>(listIterator);
result = _(list).chain().sumBy('');
result = _(dictionary).chain().sumBy<number>(dictionaryIterator);
result = _(dictionary).chain().sumBy('');
}
}
+179 -78
View File
@@ -135,9 +135,9 @@ added 8 utility methods:
added 4 math methods:
- [x] _.maxBy
- [ ] _.mean
- [x] _.mean
- [x] _.minBy
- [ ] _.sumBy
- [x] _.sumBy
added 2 function methods:
- [x] _.flip
@@ -10226,6 +10226,38 @@ declare module _ {
): T;
}
//_.mean
interface LoDashStatic {
/**
* Computes the mean of the values in `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
*/
mean<T>(
collection: List<T>
): number;
}
interface LoDashImplicitArrayWrapper<T> {
/**
* @see _.mean
*/
mean<T>(): number;
/**
* @see _.mean
*/
mean(): number;
}
//_.min
interface LoDashStatic {
/**
@@ -10391,40 +10423,19 @@ declare module _ {
//_.sum
interface LoDashStatic {
/**
* Gets the sum of the values in collection.
* Computes the sum of the values in `array`.
*
* @param collection The collection to iterate over.
* @param iteratee The function invoked per iteration.
* @param thisArg The this binding of iteratee.
* @return Returns the sum.
* @static
* @memberOf _
* @category Math
* @param {Array} array The array to iterate over.
* @returns {number} Returns the sum.
* @example
*
* _.sum([4, 2, 8, 6]);
* // => 20
*/
sum<T>(
collection: List<T>,
iteratee: ListIterator<T, number>,
thisArg?: any
): number;
/**
* @see _.sum
**/
sum<T>(
collection: Dictionary<T>,
iteratee: DictionaryIterator<T, number>,
thisArg?: any
): number;
/**
* @see _.sum
*/
sum<T>(
collection: List<number>|Dictionary<number>,
iteratee: string
): number;
/**
* @see _.sum
*/
sum<T>(collection: List<T>|Dictionary<T>): number;
sum<T>(collection: List<T>): number;
/**
* @see _.sum
@@ -10433,19 +10444,6 @@ declare module _ {
}
interface LoDashImplicitArrayWrapper<T> {
/**
* @see _.sum
*/
sum(
iteratee: ListIterator<T, number>,
thisArg?: any
): number;
/**
* @see _.sum
*/
sum(iteratee: string): number;
/**
* @see _.sum
*/
@@ -10456,15 +10454,7 @@ declare module _ {
/**
* @see _.sum
**/
sum<TValue>(
iteratee: ListIterator<TValue, number>|DictionaryIterator<TValue, number>,
thisArg?: any
): number;
/**
* @see _.sum
*/
sum(iteratee: string): number;
sum<TValue>(): number;
/**
* @see _.sum
@@ -10473,19 +10463,6 @@ declare module _ {
}
interface LoDashExplicitArrayWrapper<T> {
/**
* @see _.sum
*/
sum(
iteratee: ListIterator<T, number>,
thisArg?: any
): LoDashExplicitWrapper<number>;
/**
* @see _.sum
*/
sum(iteratee: string): LoDashExplicitWrapper<number>;
/**
* @see _.sum
*/
@@ -10496,15 +10473,7 @@ declare module _ {
/**
* @see _.sum
*/
sum<TValue>(
iteratee: ListIterator<TValue, number>|DictionaryIterator<TValue, number>,
thisArg?: any
): LoDashExplicitWrapper<number>;
/**
* @see _.sum
*/
sum(iteratee: string): LoDashExplicitWrapper<number>;
sum<TValue>(): LoDashExplicitWrapper<number>;
/**
* @see _.sum
@@ -10512,6 +10481,138 @@ declare module _ {
sum(): LoDashExplicitWrapper<number>;
}
//_.sumBy
interface LoDashStatic {
/**
* This method is like `_.sum` except that it accepts `iteratee` which is
* invoked for each element in `array` to generate the value to be summed.
* The iteratee is invoked with one argument: (value).
*
* @static
* @memberOf _
* @category Math
* @param {Array} array The array to iterate over.
* @param {Function|Object|string} [iteratee=_.identity] The iteratee invoked per element.
* @returns {number} Returns the sum.
* @example
*
* var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
*
* _.sumBy(objects, function(o) { return o.n; });
* // => 20
*
* // using the `_.property` iteratee shorthand
* _.sumBy(objects, 'n');
* // => 20
*/
sumBy<T>(
collection: List<T>,
iteratee: ListIterator<T, number>
): number;
/**
* @see _.sumBy
**/
sumBy<T>(
collection: Dictionary<T>,
iteratee: DictionaryIterator<T, number>
): number;
/**
* @see _.sumBy
*/
sumBy<T>(
collection: List<number>|Dictionary<number>,
iteratee: string
): number;
/**
* @see _.sumBy
*/
sumBy<T>(collection: List<T>|Dictionary<T>): number;
/**
* @see _.sumBy
*/
sumBy(collection: List<number>|Dictionary<number>): number;
}
interface LoDashImplicitArrayWrapper<T> {
/**
* @see _.sumBy
*/
sumBy(
iteratee: ListIterator<T, number>
): number;
/**
* @see _.sumBy
*/
sumBy(iteratee: string): number;
/**
* @see _.sumBy
*/
sumBy(): number;
}
interface LoDashImplicitObjectWrapper<T> {
/**
* @see _.sumBy
**/
sumBy<TValue>(
iteratee: ListIterator<TValue, number>|DictionaryIterator<TValue, number>
): number;
/**
* @see _.sumBy
*/
sumBy(iteratee: string): number;
/**
* @see _.sumBy
*/
sumBy(): number;
}
interface LoDashExplicitArrayWrapper<T> {
/**
* @see _.sumBy
*/
sumBy(
iteratee: ListIterator<T, number>
): LoDashExplicitWrapper<number>;
/**
* @see _.sumBy
*/
sumBy(iteratee: string): LoDashExplicitWrapper<number>;
/**
* @see _.sumBy
*/
sumBy(): LoDashExplicitWrapper<number>;
}
interface LoDashExplicitObjectWrapper<T> {
/**
* @see _.sumBy
*/
sumBy<TValue>(
iteratee: ListIterator<TValue, number>|DictionaryIterator<TValue, number>
): LoDashExplicitWrapper<number>;
/**
* @see _.sumBy
*/
sumBy(iteratee: string): LoDashExplicitWrapper<number>;
/**
* @see _.sumBy
*/
sumBy(): LoDashExplicitWrapper<number>;
}
/**********
* Number *
**********/