From 37c762000842cb8152bb64a920e22552f6fc7c14 Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Wed, 1 Jul 2015 14:45:36 +0100 Subject: [PATCH 1/3] More specific _.flatten type, remove types for non-existent methods --- lodash/lodash-tests.ts | 11 ++-- lodash/lodash.d.ts | 129 ++++++----------------------------------- 2 files changed, 24 insertions(+), 116 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index e0f2b8226..cd84de417 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -239,10 +239,13 @@ result = _([1, 2, 3]).take(function (num) { result = _(foodsOrganic).take('organic').value(); result = _(foodsType).take({ 'type': 'fruit' }).value(); -result = _.flatten([1, [2], [3, [[4]]]]); -result = _.flatten([1, [2], [3, [[4]]]], true); -var result: any -result = _.flatten(stoogesQuotes, 'quotes'); +result = >_.flatten([[1, 2], [3, 4]]); +result = >_.flatten([[1, 2], [3, 4], 5, 6]); +result = >>>_.flatten([1, [2], [3, [[4]]]]); + +result = >_.flatten([1, [2], [[3]]], true); +result = >_.flatten([1, [2], [3, [[4]]]], true); +result = >_.flatten([1, [2], [3, [[false]]]], true); result = <_.LoDashArrayWrapper>_([1, [2], [3, [[4]]]]).flatten(); result = <_.LoDashArrayWrapper>_([1, [2], [3, [[4]]]]).flatten(true); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index a906b907f..8c265cb8f 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -769,123 +769,28 @@ declare module _ { take(whereValue: W): LoDashArrayWrapper; } + interface MaybeNestedList extends List> { } + interface RecursiveList extends List> { } + //_.flatten interface LoDashStatic { /** - * Flattens a nested array (the nesting can be to any depth). If isShallow is truey, the - * array will only be flattened a single level. If a callback is provided each element of - * the array is passed through the callback before flattening. 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 flatten. - * @param shallow If true then only flatten one level, optional, default = false. - * @return `array` flattened. - **/ - flatten(array: Array, isShallow?: boolean): T[]; + * Flattens a nested array. + * + * @param array The array to flatten. + * @return `array` flattened. + **/ + flatten(array: MaybeNestedList): T[]; /** - * @see _.flatten - **/ - flatten(array: List, isShallow?: boolean): T[]; - - /** - * @see _.flatten - **/ - flatten( - array: Array, - isShallow: boolean, - callback: ListIterator, - thisArg?: any): T[]; - - /** - * @see _.flatten - **/ - flatten( - array: List, - isShallow: boolean, - callback: ListIterator, - thisArg?: any): T[]; - - /** - * @see _.flatten - **/ - flatten( - array: Array, - callback: ListIterator, - thisArg?: any): T[]; - - /** - * @see _.flatten - **/ - flatten( - array: List, - callback: ListIterator, - thisArg?: any): T[]; - - /** - * @see _.flatten - **/ - flatten( - array: Array, - isShallow: boolean, - whereValue: W): T[]; - - /** - * @see _.flatten - **/ - flatten( - array: List, - isShallow: boolean, - whereValue: W): T[]; - - /** - * @see _.flatten - **/ - flatten( - array: Array, - whereValue: W): T[]; - - /** - * @see _.flatten - **/ - flatten( - array: List, - whereValue: W): T[]; - - /** - * @see _.flatten - **/ - flatten( - array: Array, - isShallow: boolean, - pluckValue: string): T[]; - - /** - * @see _.flatten - **/ - flatten( - array: List, - isShallow: boolean, - pluckValue: string): T[]; - - /** - * @see _.flatten - **/ - flatten( - array: Array, - pluckValue: string): T[]; - - /** - * @see _.flatten - **/ - flatten( - array: List, - pluckValue: string): T[]; + * Flattens a nested array. If isDeep is true the array is recursively flattened, otherwise it is only + * flattened a single level. + * + * @param array The array to flatten. + * @param deep Specify a deep flatten. + * @return `array` flattened. + **/ + flatten(array: RecursiveList, isDeep: boolean): T[]; } interface LoDashArrayWrapper { From 36f2158f0c594efabc7b2ce30ae5a34bd042d6bc Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Wed, 1 Jul 2015 14:58:39 +0100 Subject: [PATCH 2/3] Fix _().flatten() - remove non-existent methods, small type improvement Previously type was too specific, and thereby often wrong. It's now less specific, but never wrong. --- lodash/lodash-tests.ts | 5 +++-- lodash/lodash.d.ts | 44 ++++-------------------------------------- 2 files changed, 7 insertions(+), 42 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index cd84de417..709151f16 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -247,9 +247,10 @@ result = >_.flatten([1, [2], [[3]]], true); result = >_.flatten([1, [2], [3, [[4]]]], true); result = >_.flatten([1, [2], [3, [[false]]]], true); -result = <_.LoDashArrayWrapper>_([1, [2], [3, [[4]]]]).flatten(); +result = <_.LoDashArrayWrapper>_([[1, 2], [3, 4], 5, 6]).flatten(); +result = <_.LoDashArrayWrapper>>>_([1, [2], [3, [[4]]]]).flatten(); + result = <_.LoDashArrayWrapper>_([1, [2], [3, [[4]]]]).flatten(true); -result = <_.LoDashArrayWrapper>_(stoogesQuotes).flatten('quotes'); result = _.indexOf([1, 2, 3, 1, 2, 3], 2); result = _.indexOf([1, 2, 3, 1, 2, 3], 2, 3); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 8c265cb8f..c0f0bd5d5 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -795,50 +795,14 @@ declare module _ { interface LoDashArrayWrapper { /** - * @see _.flatten - **/ - flatten(isShallow?: boolean): LoDashArrayWrapper; + * @see _.flatten + **/ + flatten(): LoDashArrayWrapper; /** * @see _.flatten **/ - flatten( - isShallow: boolean, - callback: ListIterator, - thisArg?: any): LoDashArrayWrapper; - - /** - * @see _.flatten - **/ - flatten( - callback: ListIterator, - thisArg?: any): LoDashArrayWrapper; - - /** - * @see _.flatten - **/ - flatten( - isShallow: boolean, - pluckValue: string): LoDashArrayWrapper; - - /** - * @see _.flatten - **/ - flatten( - pluckValue: string): LoDashArrayWrapper; - - /** - * @see _.flatten - **/ - flatten( - isShallow: boolean, - whereValue: W): LoDashArrayWrapper; - - /** - * @see _.flatten - **/ - flatten( - whereValue: W): LoDashArrayWrapper; + flatten(isShallow: boolean): LoDashArrayWrapper; } //_.indexOf From ed15e4bd3806d00223f40803a7d13d0d3ada111e Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Wed, 1 Jul 2015 16:14:31 +0100 Subject: [PATCH 3/3] Make _.flatten(array, bool) slightly more general --- lodash/lodash.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index c0f0bd5d5..e79eb64f8 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -790,7 +790,7 @@ declare module _ { * @param deep Specify a deep flatten. * @return `array` flattened. **/ - flatten(array: RecursiveList, isDeep: boolean): T[]; + flatten(array: RecursiveList, isDeep: boolean): List | RecursiveList; } interface LoDashArrayWrapper {