diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index 0e730b02ad..d1d53c2259 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -104,28 +104,33 @@ result = <(key: string) => any>testMapCache.get; result = <(key: string) => boolean>testMapCache.has; result = <(key: string, value: any) => _.Dictionary>testMapCache.set; -/************* - * Chaining * - *************/ -result = <_.LoDashImplicitWrapper>_('test'); -result = <_.LoDashImplicitWrapper>_(1); -result = <_.LoDashImplicitWrapper>_(true); -result = <_.LoDashImplicitArrayWrapper>_(['test1', 'test2']); -// Appears to be a change in the compiler, if the type explicity implements the object indexer. -// Looking at: https://typescript.codeplex.com/wikipage?title=Known%20breaking%20changes%20between%200.8%20and%200.9&referringTitle=Documentation -// "The ‘noimplicitany’ option now warns on the use of the hidden default indexer" -result = <_.LoDashImplicitObjectWrapper<_.Dictionary>>_(<{ [index: string]: string; }>{ 'key1': 'test1', 'key2': 'test2' }); +// _ +module TestWrapper { + { + let result: _.LoDashImplicitWrapper; + result = _(''); + } -result = <_.LoDashImplicitWrapper>_.chain('test'); -result = <_.LoDashImplicitWrapper>_('test').chain(); -result = <_.LoDashImplicitWrapper>_.chain(1); -result = <_.LoDashImplicitWrapper>_(1).chain(); -result = <_.LoDashImplicitWrapper>_.chain(true); -result = <_.LoDashImplicitWrapper>_(true).chain(); -result = <_.LoDashImplicitArrayWrapper>_.chain(['test1', 'test2']); -result = <_.LoDashImplicitArrayWrapper>_(['test1', 'test2']).chain(); -result = <_.LoDashImplicitObjectWrapper<_.Dictionary>>_.chain(<{ [index: string]: string; }>{ 'key1': 'test1', 'key2': 'test2' }); -result = <_.LoDashImplicitObjectWrapper<_.Dictionary>>_(<{ [index: string]: string; }>{ 'key1': 'test1', 'key2': 'test2' }).chain(); + { + let result: _.LoDashImplicitWrapper; + result = _(42); + } + + { + let result: _.LoDashImplicitWrapper; + result = _(true); + } + + { + let result: _.LoDashImplicitArrayWrapper; + result = _(['']); + } + + { + let result: _.LoDashImplicitObjectWrapper<{a: string}>; + result = _<{a: string}>({a: ''}); + } +} //Wrapped array shortcut methods result = <_.LoDashImplicitArrayWrapper>_([1, 2, 3, 4]).concat(5, 6); @@ -140,11 +145,6 @@ result = <_.LoDashImplicitArrayWrapper>_([1, 2, 3, 4]).splice(1); result = <_.LoDashImplicitArrayWrapper>_([1, 2, 3, 4]).splice(1, 2, 5, 6); result = <_.LoDashImplicitArrayWrapper>_([1, 2, 3, 4]).unshift(5, 6); -result = _.tap([1, 2, 3, 4], function (array) { console.log(array); }); -result = <_.LoDashImplicitWrapper>_('test').tap(function (value) { console.log(value); }); -result = <_.LoDashImplicitArrayWrapper>_([1, 2, 3, 4]).tap(function (array) { console.log(array); }); -result = <_.LoDashImplicitObjectWrapper<_.Dictionary>>_(<{ [index: string]: string; }>{ 'key1': 'test1', 'key2': 'test2' }).tap(function (array) { console.log(array); }); - result = _('test').toString(); result = _([1, 2, 3]).toString(); result = _({ 'key1': 'test1', 'key2': 'test2' }).toString(); @@ -1053,6 +1053,140 @@ result = _([1, 2]).zipWith([1, 2], [1, 2], [1, 2], [1, 2], [1, * Chain * *********/ +// _.chain +module TestChain { + { + let result: _.LoDashExplicitWrapper; + + result = _.chain(''); + result = _('').chain(); + + result = _.chain('').chain(); + result = _('').chain().chain(); + } + + { + let result: _.LoDashExplicitWrapper; + + result = _.chain(42); + result = _(42).chain(); + } + + { + let result: _.LoDashExplicitWrapper; + + result = _.chain(true); + result = _(true).chain(); + } + + { + let result: _.LoDashExplicitArrayWrapper; + + result = _.chain(['']); + result = _(['']).chain(); + } + + { + let result: _.LoDashExplicitObjectWrapper<{a: string}>; + + result = _.chain<{a: string}>({a: ''}); + result = _<{a: string}>({a: ''}).chain(); + } +} + +// _.tap +module TestTap { + { + let interceptor: (value: string) => void; + let result: string; + + _.tap('', interceptor); + _.tap('', interceptor, any); + } + + { + let interceptor: (value: string[]) => void; + let result: _.LoDashImplicitArrayWrapper; + + _.tap([''], interceptor); + _.tap([''], interceptor, any); + } + + { + let interceptor: (value: {a: string}) => void; + let result: _.LoDashImplicitObjectWrapper<{a: string}>; + + _.tap({a: ''}, interceptor); + _.tap({a: ''}, interceptor, any); + } + + { + let interceptor: (value: string) => void; + let result: _.LoDashImplicitWrapper; + + _.chain('').tap(interceptor, any); + _.chain('').tap(interceptor, any); + + _('').tap(interceptor); + _('').tap(interceptor, any); + } + + { + let interceptor: (value: string[]) => void; + let result: _.LoDashImplicitArrayWrapper; + + _.chain(['']).tap(interceptor); + _.chain(['']).tap(interceptor, any); + + _(['']).tap(interceptor); + _(['']).tap(interceptor, any); + } + + { + let interceptor: (value: {a: string}) => void; + let result: _.LoDashImplicitObjectWrapper<{a: string}>; + + _.chain({a: ''}).tap(interceptor); + _.chain({a: ''}).tap(interceptor, any); + + _({a: ''}).tap(interceptor); + _({a: ''}).tap(interceptor, any); + } + + { + let interceptor: (value: string) => void; + let result: _.LoDashExplicitWrapper; + + _.chain('').tap(interceptor, any); + _.chain('').tap(interceptor, any); + + _('').chain().tap(interceptor); + _('').chain().tap(interceptor, any); + } + + { + let interceptor: (value: string[]) => void; + let result: _.LoDashExplicitArrayWrapper; + + _.chain(['']).tap(interceptor); + _.chain(['']).tap(interceptor, any); + + _(['']).chain().tap(interceptor); + _(['']).chain().tap(interceptor, any); + } + + { + let interceptor: (value: {a: string}) => void; + let result: _.LoDashExplicitObjectWrapper<{a: string}>; + + _.chain({a: ''}).tap(interceptor); + _.chain({a: ''}).tap(interceptor, any); + + _({a: ''}).chain().tap(interceptor); + _({a: ''}).chain().tap(interceptor, any); + } +} + // _.thru { let result: number; diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 84c4510f94..68aefccc3c 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -211,41 +211,24 @@ declare module _ { unindexedChars: boolean; } - interface LoDashImplicitWrapperBase { - /** - * Produces the toString result of the wrapped value. - * @return Returns the string result. - **/ - toString(): string; + interface LoDashWrapperBase { } - /** - * Executes the chained sequence to extract the unwrapped value. - * @return Returns the resolved unwrapped value. - **/ - value(): T; + interface LoDashImplicitWrapperBase extends LoDashWrapperBase { } - /** - * @see _.value - **/ - run(): T; - - /** - * @see _.value - **/ - toJSON(): T; - - /** - * @see _.value - **/ - valueOf(): T; - } + interface LoDashExplicitWrapperBase extends LoDashWrapperBase { } interface LoDashImplicitWrapper extends LoDashImplicitWrapperBase> { } + interface LoDashExplicitWrapper extends LoDashExplicitWrapperBase> { } + interface LoDashImplicitStringWrapper extends LoDashImplicitWrapper { } + interface LoDashExplicitStringWrapper extends LoDashExplicitWrapper { } + interface LoDashImplicitObjectWrapper extends LoDashImplicitWrapperBase> { } + interface LoDashExplicitObjectWrapper extends LoDashExplicitWrapperBase> { } + interface LoDashImplicitArrayWrapper extends LoDashImplicitWrapperBase> { concat(...items: Array>): LoDashImplicitArrayWrapper; join(seperator?: string): string; @@ -259,53 +242,11 @@ declare module _ { unshift(...items: T[]): LoDashImplicitArrayWrapper; } + interface LoDashExplicitArrayWrapper extends LoDashExplicitWrapperBase> { } + interface LoDashImplicitNumberArrayWrapper extends LoDashImplicitArrayWrapper { } - //_.chain - interface LoDashStatic { - /** - * Creates a lodash object that wraps the given value with explicit method chaining enabled. - * @param value The value to wrap. - * @return The wrapper object. - **/ - chain(value: number): LoDashImplicitWrapper; - chain(value: string): LoDashImplicitWrapper; - chain(value: boolean): LoDashImplicitWrapper; - chain(value: Array): LoDashImplicitArrayWrapper; - chain(value: T): LoDashImplicitObjectWrapper; - chain(value: any): LoDashImplicitWrapper; - } - - interface LoDashImplicitWrapperBase { - /** - * Enables explicit method chaining on the wrapper object. - * @see _.chain - * @return The wrapper object. - **/ - chain(): TWrapper; - } - - //_.tap - interface LoDashStatic { - /** - * Invokes interceptor with the value as the first argument and then returns value. The - * purpose of this method is to "tap into" a method chain in order to perform operations on - * intermediate results within the chain. - * @param value The value to provide to interceptor - * @param interceptor The function to invoke. - * @return value - **/ - tap( - value: T, - interceptor: (value: T) => void): T; - } - - interface LoDashImplicitWrapperBase { - /** - * @see _.tap - **/ - tap(interceptor: (value: T) => void): TWrapper; - } + interface LoDashExplicitNumberArrayWrapper extends LoDashExplicitArrayWrapper { } /********* * Array * @@ -2349,6 +2290,89 @@ declare module _ { * Chain * *********/ + //_.chain + interface LoDashStatic { + /** + * Creates a lodash object that wraps value with explicit method chaining enabled. + * + * @param value The value to wrap. + * @return Returns the new lodash wrapper instance. + */ + chain(value: number): LoDashExplicitWrapper; + chain(value: string): LoDashExplicitWrapper; + chain(value: boolean): LoDashExplicitWrapper; + chain(value: T[]): LoDashExplicitArrayWrapper; + chain(value: T): LoDashExplicitObjectWrapper; + chain(value: any): LoDashExplicitWrapper; + } + + interface LoDashImplicitWrapper { + /** + * @see _.chain + */ + chain(): LoDashExplicitWrapper; + } + + interface LoDashImplicitArrayWrapper { + /** + * @see _.chain + */ + chain(): LoDashExplicitArrayWrapper; + } + + interface LoDashImplicitObjectWrapper { + /** + * @see _.chain + */ + chain(): LoDashExplicitObjectWrapper; + } + + interface LoDashExplicitWrapperBase { + /** + * @see _.chain + */ + chain(): TWrapper; + } + + //_.tap + interface LoDashStatic { + /** + * This method invokes interceptor and returns value. The interceptor is bound to thisArg and invoked with one + * argument; (value). The purpose of this method is to "tap into" a method chain in order to perform operations + * on intermediate results within the chain. + * + * @param value The value to provide to interceptor. + * @param interceptor The function to invoke. + * @parem thisArg The this binding of interceptor. + * @return Returns value. + **/ + tap( + value: T, + interceptor: (value: T) => void, + thisArg?: any + ): T; + } + + interface LoDashImplicitWrapperBase { + /** + * @see _.tap + */ + tap( + interceptor: (value: T) => void, + thisArg?: any + ): TWrapper; + } + + interface LoDashExplicitWrapperBase { + /** + * @see _.tap + */ + tap( + interceptor: (value: T) => void, + thisArg?: any + ): TWrapper; + } + //_.thru interface LoDashStatic { /** @@ -2451,6 +2475,51 @@ declare module _ { plant(value: any): LoDashImplicitWrapper; } + // _.run + interface LoDashWrapperBase { + /** + * @see _.value + */ + run(): T; + } + + // _.toJSON + interface LoDashWrapperBase { + /** + * @see _.value + */ + toJSON(): T; + } + + interface LoDashWrapperBase { + /** + * Produces the result of coercing the unwrapped value to a string. + * + * @return Returns the coerced string value. + */ + toString(): string; + } + + // _.value + interface LoDashWrapperBase { + /** + * Executes the chained sequence to extract the unwrapped value. + * + * @alias _.run, _.toJSON, _.valueOf + * + * @return Returns the resolved unwrapped value. + */ + value(): T; + } + + // _.valueOf + interface LoDashWrapperBase { + /** + * @see _.value + */ + valueOf(): T; + } + /************** * Collection * **************/