Merge pull request #5975 from chrootsu/lodash-first

lodash: changed _.first() method (alias _.head())
This commit is contained in:
Masahiro Wakame
2015-09-27 12:45:27 +09:00
2 changed files with 63 additions and 246 deletions

View File

@@ -257,37 +257,16 @@ result = <number>_.findLastIndex(['apple', 'banana', 'beet'], function (f: strin
result = <number>_.findLastIndex(['apple', 'banana', 'beet'], 'apple');
result = <number>_.findLastIndex([{ food: 'apple' }, { food: 'banana' }, { food: 'beet' }], { food: 'apple' });
result = <number>_.first([1, 2, 3]);
result = <number[]>_.first([1, 2, 3], 2);
result = <number[]>_.first([1, 2, 3], function (num) {
return num < 3;
});
result = <IFoodOrganic[]>_.first(foodsOrganic, 'organic');
result = <IFoodType[]>_.first(foodsType, { 'type': 'fruit' });
result = <number>_([1, 2, 3]).first();
result = <number[]>_([1, 2, 3]).first(2).value();
result = <number[]>_([1, 2, 3]).first(function (num) {
return num < 3;
}).value();
result = <IFoodOrganic[]>_(foodsOrganic).first('organic').value();
result = <IFoodType[]>_(foodsType).first({ 'type': 'fruit' }).value();
result = <number>_.head([1, 2, 3]);
result = <number[]>_.head([1, 2, 3], 2);
result = <number[]>_.head([1, 2, 3], function (num) {
return num < 3;
});
result = <IFoodOrganic[]>_.head(foodsOrganic, 'organic');
result = <IFoodType[]>_.head(foodsType, { 'type': 'fruit' });
result = <number>_([1, 2, 3]).head();
result = <number[]>_([1, 2, 3]).head(2).value();
result = <number[]>_([1, 2, 3]).head(function (num) {
return num < 3;
}).value();
result = <IFoodOrganic[]>_(foodsOrganic).head('organic').value();
result = <IFoodType[]>_(foodsType).head({ 'type': 'fruit' }).value();
// _.first
module TestFirst {
let array: TResult[];
let list: _.List<TResult>;
let result: TResult;
result = _.first<TResult>(array);
result = _.first<TResult>(list);
result = _(array).first();
result = _(list).first<TResult>();
}
result = <number[]>_.take([1, 2, 3]);
result = <number[]>_.take([1, 2, 3], 2);
@@ -320,6 +299,17 @@ result = <_.LoDashArrayWrapper<number>>_([1, [2], [3, [[4]]]]).flatten(true);
result = <_.LoDashArrayWrapper<number>>_([1, [2], [3, [[4]]]]).flattenDeep();
// _.head
module TestHead {
let array: TResult[];
let list: _.List<TResult>;
let result: TResult;
result = _.head<TResult>(array);
result = _.head<TResult>(list);
result = _(array).head();
result = _(list).head<TResult>();
}
result = <number>_.indexOf([1, 2, 3, 1, 2, 3], 2);
result = <number>_.indexOf([1, 2, 3, 1, 2, 3], 2, 3);
result = <number>_.indexOf([1, 1, 2, 2, 3, 3], 2, true);

257
lodash/lodash.d.ts vendored
View File

@@ -556,162 +556,31 @@ declare module _ {
//_.first
interface LoDashStatic {
/**
* Gets the first element or first n elements of an array. If a callback is provided
* elements at the beginning of the array are returned as long as the callback returns
* truey. The callback is bound to thisArg and invoked with three arguments; (value,
* index, array).
*
* If a property name is provided for callback the created "_.pluck" style callback
* will return the property value of the given element.
*
* If an object is provided for callback the created "_.where" style callback will return ]
* true for elements that have the properties of the given object, else false.
* @param array Retrieves the first element of this array.
* @return Returns the first element of `array`.
**/
first<T>(array?: Array<T>): T;
* Gets the first element of array.
*
* @alias _.head
*
* @param array The array to query.
* @return Returns the first element of array.
*/
first<T>(array: List<T>): T;
}
interface LoDashArrayWrapper<T> {
/**
* @see _.first
**/
first<T>(array?: List<T>): T;
* @see _.first
*/
first(): T;
}
interface LoDashObjectWrapper<T> {
/**
* @see _.first
* @param n The number of elements to return.
**/
first<T>(
array: Array<T>,
n: number): T[];
/**
* @see _.first
* @param n The number of elements to return.
**/
first<T>(
array: List<T>,
n: number): T[];
/**
* @see _.first
* @param callback The function called per element.
* @param [thisArg] The this binding of callback.
**/
first<T>(
array: Array<T>,
callback: ListIterator<T, boolean>,
thisArg?: any): T[];
/**
* @see _.first
* @param callback The function called per element.
* @param [thisArg] The this binding of callback.
**/
first<T>(
array: List<T>,
callback: ListIterator<T, boolean>,
thisArg?: any): T[];
/**
* @see _.first
* @param pluckValue "_.pluck" style callback value
**/
first<T>(
array: Array<T>,
pluckValue: string): T[];
/**
* @see _.first
* @param pluckValue "_.pluck" style callback value
**/
first<T>(
array: List<T>,
pluckValue: string): T[];
/**
* @see _.first
* @param whereValue "_.where" style callback value
**/
first<W, T>(
array: Array<T>,
whereValue: W): T[];
/**
* @see _.first
* @param whereValue "_.where" style callback value
**/
first<W, T>(
array: List<T>,
whereValue: W): T[];
/**
* @see _.first
**/
head<T>(array: Array<T>): T;
/**
* @see _.first
**/
head<T>(array: List<T>): T;
/**
* @see _.first
**/
head<T>(
array: Array<T>,
n: number): T[];
/**
* @see _.first
**/
head<T>(
array: List<T>,
n: number): T[];
/**
* @see _.first
**/
head<T>(
array: Array<T>,
callback: ListIterator<T, boolean>,
thisArg?: any): T[];
/**
* @see _.first
**/
head<T>(
array: List<T>,
callback: ListIterator<T, boolean>,
thisArg?: any): T[];
/**
* @see _.first
**/
head<T>(
array: Array<T>,
pluckValue: string): T[];
/**
* @see _.first
**/
head<T>(
array: List<T>,
pluckValue: string): T[];
/**
* @see _.first
**/
head<W, T>(
array: Array<T>,
whereValue: W): T[];
/**
* @see _.first
**/
head<W, T>(
array: List<T>,
whereValue: W): T[];
* @see _.first
*/
first<TResult>(): TResult;
}
interface LoDashStatic {
/**
* @see _.first
**/
@@ -770,70 +639,6 @@ declare module _ {
}
interface LoDashArrayWrapper<T> {
/**
* @see _.first
**/
first(): T;
/**
* @see _.first
* @param n The number of elements to return.
**/
first(n: number): LoDashArrayWrapper<T>;
/**
* @see _.first
* @param callback The function called per element.
* @param [thisArg] The this binding of callback.
**/
first(
callback: ListIterator<T, boolean>,
thisArg?: any): LoDashArrayWrapper<T>;
/**
* @see _.first
* @param pluckValue "_.pluck" style callback value
**/
first(pluckValue: string): LoDashArrayWrapper<T>;
/**
* @see _.first
* @param whereValue "_.where" style callback value
**/
first<W>(whereValue: W): LoDashArrayWrapper<T>;
/**
* @see _.first
**/
head(): T;
/**
* @see _.first
* @param n The number of elements to return.
**/
head(n: number): LoDashArrayWrapper<T>;
/**
* @see _.first
* @param callback The function called per element.
* @param [thisArg] The this binding of callback.
**/
head(
callback: ListIterator<T, boolean>,
thisArg?: any): LoDashArrayWrapper<T>;
/**
* @see _.first
* @param pluckValue "_.pluck" style callback value
**/
head(pluckValue: string): LoDashArrayWrapper<T>;
/**
* @see _.first
* @param whereValue "_.where" style callback value
**/
head<W>(whereValue: W): LoDashArrayWrapper<T>;
/**
* @see _.first
**/
@@ -926,6 +731,28 @@ declare module _ {
flattenDeep<T>(): LoDashArrayWrapper<any>;
}
//_.head
interface LoDashStatic {
/**
* @see _.first
*/
head<T>(array: List<T>): T;
}
interface LoDashArrayWrapper<T> {
/**
* @see _.first
*/
head(): T;
}
interface LoDashObjectWrapper<T> {
/**
* @see _.first
*/
head<TResult>(): TResult;
}
//_.indexOf
interface LoDashStatic {
/**