From f82d724389e5369f657272b5452f2a0c89f81278 Mon Sep 17 00:00:00 2001 From: Brian Zengel Date: Fri, 18 Oct 2013 19:08:04 -0400 Subject: [PATCH] _.filter/_.select definitions --- lodash/lodash-tests.ts | 25 +++++-------- lodash/lodash.d.ts | 81 +++++++++++++++++++++++++++++++----------- 2 files changed, 69 insertions(+), 37 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index ca334a3d7..c14a8b5db 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -227,6 +227,14 @@ result = _.every(stoogesAges, { 'age': 50 }); result = _.all(stoogesAges, 'age'); result = _.all(stoogesAges, { 'age': 50 }); +result = _.filter([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; }); +result = _.filter(foodsCombined, 'organic'); +result = _.filter(foodsCombined, { 'type': 'fruit' }); + + result = _.select([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; }); + result = _.select(foodsCombined, 'organic'); + result = _.select(foodsCombined, { 'type': 'fruit' }); + result = _.find([1, 2, 3, 4], function(num) { return num % 2 == 0; }); @@ -282,12 +290,6 @@ result = _.some(foodsCombined, { 'type': 'meat' }); //WHAT'S LEFT //////////////////////////////////////////////////////////////////////////////////////// -_.each([1, 2, 3], (num) => alert(num.toString())); -_.each({ one: 1, two: 2, three: 3 }, (value) => alert(value.toString())); - -_.map([1, 2, 3], (num) => num * 3); -_.map({ one: 1, two: 2, three: 3 }, (value: number, key?: string) => value * 3); - var sum = _.reduce([1, 2, 3], (memo, num) => memo + num, 0); sum = _.reduce([1, 2, 3], (memo, num) => memo + num); // memo is optional #issue 5 github @@ -296,20 +298,11 @@ var flat = _.reduceRight(list, (a, b) => a.concat(b), []); var even = _.find([1, 2, 3, 4, 5, 6], (num) => num % 2 == 0); -var evens = _.filter([1, 2, 3, 4, 5, 6], (num) => num % 2 == 0); - var listOfPlays = [{ title: "Cymbeline", author: "Shakespeare", year: 1611 }, { title: "The Tempest", author: "Shakespeare", year: 1611 }, { title: "Other", author: "Not Shakespeare", year: 2012 }]; _.where(listOfPlays, { author: "Shakespeare", year: 1611 }); var odds = _.reject([1, 2, 3, 4, 5, 6], (num) => num % 2 == 0); -_.every([true, 1, null, 'yes'], _.identity); -_.every<{}>([true, 1, null, 'yes']); - -_.any([null, 0, 'yes', false]); - -_.contains([1, 2, 3], 3); - _.invoke([[5, 1, 7], [3, 2, 1]], 'sort'); var stooges = [{ name: 'moe', age: 40 }, { name: 'larry', age: 50 }, { name: 'curly', age: 60 }]; @@ -336,8 +329,6 @@ _(stooges) .indexBy('age') .value()['40'].age; -_.countBy([1, 2, 3, 4, 5], (num) => (num % 2 == 0) ? 'even' : 'odd'); - _.shuffle([1, 2, 3, 4, 5, 6]); (function(a, b, c, d){ return _.toArray(arguments).slice(1); })(1, 2, 3, 4); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 30181a740..783b3d7ae 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -932,6 +932,66 @@ declare module _ { collection: Collection, whereValue: Dictionary): boolean; + /** + * Iterates over elements of a collection, returning an array of all elements the + * callback returns truey for. The callback is bound to thisArg and invoked with three + * arguments; (value, index|key, collection). + * + * 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 collection The collection to iterate over. + * @param callback The function called per iteration. + * @param context The this binding of callback. + * @return Returns a new array of elements that passed the callback check. + **/ + export function filter( + collection: Collection, + callback: ListIterator, + thisArg?: any): T[]; + + /** + * @see _.filter + * @param pluckValue _.pluck style callback + **/ + export function filter( + collection: Collection, + pluckValue: string): T[]; + + /** + * @see _.filter + * @param pluckValue _.pluck style callback + **/ + export function filter( + collection: Collection, + whereValue: Dictionary): T[]; + + /** + * @see _.filter + **/ + export function select( + collection: Collection, + callback: ListIterator, + thisArg?: any): T[]; + + /** + * @see _.filter + * @param pluckValue _.pluck style callback + **/ + export function select( + collection: Collection, + pluckValue: string): T[]; + + /** + * @see _.filter + * @param pluckValue _.pluck style callback + **/ + export function select( + collection: Collection, + whereValue: Dictionary): T[]; + /** * Iterates over elements of a collection, returning the first element that the callback * returns truey for. The callback is bound to thisArg and invoked with three arguments; @@ -1307,26 +1367,7 @@ declare module _ { memo?: TResult, context?: any): TResult; - /** - * Looks through each value in the list, returning an array of all the values that pass a truth - * test (iterator). Delegates to the native filter method, if it exists. - * @param list Filter elements out of this list. - * @param iterator Filter iterator function for each element in `list`. - * @param context `this` object in `iterator`, optional. - * @return The filtered list of elements. - **/ - export function filter( - list: Collection, - iterator: ListIterator, - context?: any): T[]; - - /** - * @see _.filter - **/ - export function select( - list: Collection, - iterator: ListIterator, - context?: any): T[]; + /** * Looks through each value in the list, returning an array of all the values that contain all