diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index aa70ea4d9..a7afd3ee5 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -1,7 +1,82 @@ -/// +/// declare var $; +interface IFoodOrganic { + name: string; + organic: boolean; +} + +interface IFoodType { + name: string; + type: string; +} + + +var foodsOrganic: IFoodOrganic[] = [ + { name: 'banana', organic: true }, + { name: 'beet', organic: false }, +]; +var foodsType: IFoodType[] = [ + { name: 'apple', type: 'fruit' }, + { name: 'banana', type: 'fruit' }, + { name: 'beet', type: 'vegetable' } +]; +var result; + +result = _.compact([0, 1, false, 2, '', 3]); +result = _.difference([1, 2, 3, 4, 5], [5, 2, 10]); + +result = _.rest([1, 2, 3]); +result = _.rest([1, 2, 3], 2); +result = _.rest([1, 2, 3], function(num) { + return num < 3; +}); +result = _.rest(foodsOrganic, 'test'); +result = _.rest(foodsType, { 'test': 'value' }); + + _.drop([1, 2, 3]); + _.drop([1, 2, 3], 2); + _.drop([1, 2, 3], function(num) { + return num < 3; + }); + _.drop(foodsOrganic, 'test'); + _.drop(foodsType, { 'test': 'value' }); + + _.tail([1, 2, 3]); + _.tail([1, 2, 3], 2); + _.tail([1, 2, 3], function(num) { + return num < 3; + }); + _.tail(foodsOrganic, 'test'); + _.tail(foodsType, { 'test': 'value' }); + +result = _.findIndex(['apple', 'banana', 'beet'], function(f) { + return /^b/.test(f); +}); +result = _.findIndex(['apple', 'banana', 'beet'], 'apple'); +result = _.findIndex([{ food: 'apple' }, { food: 'banana' }, { food: 'beet' }], { food: 'apple'}); + +result = _.findLastIndex(['apple', 'banana', 'beet'], function(f) { + return /^b/.test(f); +}); +result = _.findLastIndex(['apple', 'banana', 'beet'], 'apple'); +result = _.findLastIndex([{ food: 'apple' }, { food: 'banana' }, { food: 'beet' }], { food: 'apple'}); + + +result = _.first([1, 2, 3]); +result = _.first([1, 2, 3], 2); +result = _.first([1, 2, 3], function(num) { + return num < 3; +}); +result = _.first(foodsOrganic, 'organic'); +result = _.first(foodsType, { 'type': 'fruit' }); + + +//////////////////////////////////////////////////////////////////////////////////////// +//WHAT'S LEFT +//////////////////////////////////////////////////////////////////////////////////////// + _.each([1, 2, 3], (num) => alert(num.toString())); _.each({ one: 1, two: 2, three: 3 }, (value) => alert(value.toString())); @@ -66,11 +141,13 @@ _.size({ one: 1, two: 2, three: 3 }); /////////////////////////////////////////////////////////////////////////////////////// -_.first([5, 4, 3, 2, 1]); + _.initial([5, 4, 3, 2, 1]); _.last([5, 4, 3, 2, 1]); -_.rest([5, 4, 3, 2, 1]); -_.compact([0, 1, false, 2, '', 3]); + + + + _.flatten([1, 2, 3, 4]); _.flatten([1, [2]]); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 60991da49..4d85483c5 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -237,6 +237,49 @@ declare module _ { iterator: ListIterator, context?: any): T; + /** + * This method is like _.find except that it returns the index of the first element that passes + * the callback check, instead of the element itself. + * @param array The array to search. + * @param {(Function|Object|string)} callback The function called per iteration. If a property name or object is provided it will be + * used to create a ".pluck" or ".where" style callback, respectively. + * @param thisArg The this binding of callback. + * @return Returns the index of the found element, else -1. + **/ + export function findIndex( + array: List, + callback: ListIterator, + thisArg?: any): number; + export function findIndex( + array: List, + pluckValue: string, + thisArg?: any): number; + export function findIndex( + array: List, + whereDictionary: Dictionary, + thisArg?: any): number; + + /** + * This method is like _.findIndex except that it iterates over elements of a collection from right to left. + * @param array The array to search. + * @param {(Function|Object|string)} callback The function called per iteration. If a property name or object is provided it will be + * used to create a ".pluck" or ".where" style callback, respectively. + * @param thisArg The this binding of callback. + * @return Returns the index of the found element, else -1. + **/ + export function findLastIndex( + array: List, + callback: ListIterator, + thisArg?: any): number; + export function findLastIndex( + array: List, + pluckValue: string, + thisArg?: any): number; + export function findLastIndex( + array: List, + whereDictionary: Dictionary, + thisArg?: any): number; + /** * @see _.find **/ @@ -549,7 +592,16 @@ declare module _ { **********/ /** - * Returns the first element of an array. Passing n will return the first n elements of the array. + * 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`. **/ @@ -557,12 +609,38 @@ declare module _ { /** * @see _.first - * @param n Return more than one element from `array`. + * @param n The number of elements to return. **/ export function first( array: List, n: number): T[]; + /** + * @see _.first + * @param callback The function called per element. + * @param [thisArg] The this binding of callback. + **/ + export function first( + array: List, + callback: ListIterator, + thisArg?: any): T[]; + + /** + * @see _.first + * @param pluckValue "_.pluck" style callback value + **/ + export function first( + array: List, + pluckValue: string): T[]; + + /** + * @see _.first + * @param whereValue "_.where" style callback value + **/ + export function first( + array: List, + whereValue: Dictionary): T[]; + /** * @see _.first **/ @@ -614,35 +692,85 @@ declare module _ { n: number): T[]; /** - * Returns the rest of the elements in an array. Pass an index to return the values of the array - * from that index onward. - * @param array The array to retrieve all but the first `index` elements. - * @param n The index to start retrieving elements forward from, optional, default = 1. - * @return Returns the elements of `array` from `index` to the end of `array`. + * The opposite of _.initial this method gets all but the first element or first n elements of + * an array. If a callback function is provided elements at the beginning of the array are excluded + * from the result 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 The array to query. + * @param {(Function|Object|number|string)} [callback=1] The function called per element or the number + * of elements to exclude. If a property name or object is provided it will be used to create a + * ".pluck" or ".where" style callback, respectively. + * @param {*} [thisArg] The this binding of callback. + * @return Returns a slice of array. **/ export function rest( array: List, - n?: number): T[]; - - /** - * @see _.rest - **/ - export function tail( + callback: (num: number) => boolean, + thisArg?: any): T[]; + export function rest( array: List, - n?: number): T[]; + n?: number, + thisArg?: any): T[]; + export function rest( + array: List, + pluckValue: string, + thisArg?: any): T[]; + export function rest( + array: List, + whereValue: Dictionary, + thisArg?: any): T[]; /** * @see _.rest **/ export function drop( array: List, - n?: number): T[]; + callback: (num: number) => boolean, + thisArg?: any): T[]; + export function drop( + array: List, + n?: number, + thisArg?: any): T[]; + export function drop( + array: List, + pluckValue: string, + thisArg?: any): T[]; + export function drop( + array: List, + whereValue: Dictionary, + thisArg?: any): T[]; + + /** + * @see _.rest + **/ + export function tail( + array: List, + callback: (num: number) => boolean, + thisArg?: any): T[]; + export function tail( + array: List, + n?: number, + thisArg?: any): T[]; + export function tail( + array: List, + pluckValue: string, + thisArg?: any): T[]; + export function tail( + array: List, + whereValue: Dictionary, + thisArg?: any): T[]; /** * Returns a copy of the array with all falsy values removed. In JavaScript, false, null, 0, "", * undefined and NaN are all falsy. * @param array Array to compact. - * @return Copy of `array` without false values. + * @return (Array) Returns a new array of filtered values. **/ export function compact(array: List): T[]; @@ -684,10 +812,11 @@ declare module _ { export function intersection(...arrays: List[]): T[]; /** - * Similar to without, but returns the values from array that are not present in the other arrays. - * @param array Keeps values that are within `others`. - * @param others The values to keep within `array`. - * @return Copy of `array` with only `others` values. + * Creates an array excluding all values of the provided arrays using strict equality for comparisons + * , i.e. ===. + * @param array The array to process + * @param others The arrays of values to exclude. + * @return Returns a new array of filtered values. **/ export function difference( array: List,