lodash: explicit chaining wrapper added; signatures of methods _.chain and _.tap changed

This commit is contained in:
Ilya Mochalov
2015-10-17 10:26:04 +05:00
parent 25b0d00e06
commit b7a578fbd8
2 changed files with 300 additions and 97 deletions

View File

@@ -104,28 +104,33 @@ result = <(key: string) => any>testMapCache.get;
result = <(key: string) => boolean>testMapCache.has;
result = <(key: string, value: any) => _.Dictionary<any>>testMapCache.set;
/*************
* Chaining *
*************/
result = <_.LoDashImplicitWrapper<string>>_('test');
result = <_.LoDashImplicitWrapper<number>>_(1);
result = <_.LoDashImplicitWrapper<boolean>>_(true);
result = <_.LoDashImplicitArrayWrapper<string>>_(['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<string>>>_(<{ [index: string]: string; }>{ 'key1': 'test1', 'key2': 'test2' });
// _
module TestWrapper {
{
let result: _.LoDashImplicitWrapper<string>;
result = _('');
}
result = <_.LoDashImplicitWrapper<string>>_.chain('test');
result = <_.LoDashImplicitWrapper<string>>_('test').chain();
result = <_.LoDashImplicitWrapper<number>>_.chain(1);
result = <_.LoDashImplicitWrapper<number>>_(1).chain();
result = <_.LoDashImplicitWrapper<boolean>>_.chain(true);
result = <_.LoDashImplicitWrapper<boolean>>_(true).chain();
result = <_.LoDashImplicitArrayWrapper<string>>_.chain(['test1', 'test2']);
result = <_.LoDashImplicitArrayWrapper<string>>_(['test1', 'test2']).chain();
result = <_.LoDashImplicitObjectWrapper<_.Dictionary<string>>>_.chain(<{ [index: string]: string; }>{ 'key1': 'test1', 'key2': 'test2' });
result = <_.LoDashImplicitObjectWrapper<_.Dictionary<string>>>_(<{ [index: string]: string; }>{ 'key1': 'test1', 'key2': 'test2' }).chain();
{
let result: _.LoDashImplicitWrapper<number>;
result = _(42);
}
{
let result: _.LoDashImplicitWrapper<boolean>;
result = _(true);
}
{
let result: _.LoDashImplicitArrayWrapper<string>;
result = _<string>(['']);
}
{
let result: _.LoDashImplicitObjectWrapper<{a: string}>;
result = _<{a: string}>({a: ''});
}
}
//Wrapped array shortcut methods
result = <_.LoDashImplicitArrayWrapper<number>>_([1, 2, 3, 4]).concat(5, 6);
@@ -140,11 +145,6 @@ result = <_.LoDashImplicitArrayWrapper<number>>_([1, 2, 3, 4]).splice(1);
result = <_.LoDashImplicitArrayWrapper<number>>_([1, 2, 3, 4]).splice(1, 2, 5, 6);
result = <_.LoDashImplicitArrayWrapper<number>>_([1, 2, 3, 4]).unshift(5, 6);
result = <number[]>_.tap([1, 2, 3, 4], function (array) { console.log(array); });
result = <_.LoDashImplicitWrapper<string>>_('test').tap(function (value) { console.log(value); });
result = <_.LoDashImplicitArrayWrapper<number>>_([1, 2, 3, 4]).tap(function (array) { console.log(array); });
result = <_.LoDashImplicitObjectWrapper<_.Dictionary<string>>>_(<{ [index: string]: string; }>{ 'key1': 'test1', 'key2': 'test2' }).tap(function (array) { console.log(array); });
result = <string>_('test').toString();
result = <string>_([1, 2, 3]).toString();
result = <string>_({ 'key1': 'test1', 'key2': 'test2' }).toString();
@@ -1053,6 +1053,140 @@ result = <number[]>_([1, 2]).zipWith<number>([1, 2], [1, 2], [1, 2], [1, 2], [1,
* Chain *
*********/
// _.chain
module TestChain {
{
let result: _.LoDashExplicitWrapper<string>;
result = _.chain('');
result = _('').chain();
result = _.chain('').chain();
result = _('').chain().chain();
}
{
let result: _.LoDashExplicitWrapper<number>;
result = _.chain(42);
result = _(42).chain();
}
{
let result: _.LoDashExplicitWrapper<boolean>;
result = _.chain(true);
result = _(true).chain();
}
{
let result: _.LoDashExplicitArrayWrapper<string>;
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<string>;
_.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<string>;
_.chain('').tap(interceptor, any);
_.chain('').tap(interceptor, any);
_('').tap(interceptor);
_('').tap(interceptor, any);
}
{
let interceptor: (value: string[]) => void;
let result: _.LoDashImplicitArrayWrapper<string>;
_.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<string>;
_.chain('').tap(interceptor, any);
_.chain('').tap(interceptor, any);
_('').chain().tap(interceptor);
_('').chain().tap(interceptor, any);
}
{
let interceptor: (value: string[]) => void;
let result: _.LoDashExplicitArrayWrapper<string>;
_.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;

211
lodash/lodash.d.ts vendored
View File

@@ -211,41 +211,24 @@ declare module _ {
unindexedChars: boolean;
}
interface LoDashImplicitWrapperBase<T, TWrapper> {
/**
* Produces the toString result of the wrapped value.
* @return Returns the string result.
**/
toString(): string;
interface LoDashWrapperBase<T, TWrapper> { }
/**
* Executes the chained sequence to extract the unwrapped value.
* @return Returns the resolved unwrapped value.
**/
value(): T;
interface LoDashImplicitWrapperBase<T, TWrapper> extends LoDashWrapperBase<T, TWrapper> { }
/**
* @see _.value
**/
run(): T;
/**
* @see _.value
**/
toJSON(): T;
/**
* @see _.value
**/
valueOf(): T;
}
interface LoDashExplicitWrapperBase<T, TWrapper> extends LoDashWrapperBase<T, TWrapper> { }
interface LoDashImplicitWrapper<T> extends LoDashImplicitWrapperBase<T, LoDashImplicitWrapper<T>> { }
interface LoDashExplicitWrapper<T> extends LoDashExplicitWrapperBase<T, LoDashExplicitWrapper<T>> { }
interface LoDashImplicitStringWrapper extends LoDashImplicitWrapper<string> { }
interface LoDashExplicitStringWrapper extends LoDashExplicitWrapper<string> { }
interface LoDashImplicitObjectWrapper<T> extends LoDashImplicitWrapperBase<T, LoDashImplicitObjectWrapper<T>> { }
interface LoDashExplicitObjectWrapper<T> extends LoDashExplicitWrapperBase<T, LoDashExplicitObjectWrapper<T>> { }
interface LoDashImplicitArrayWrapper<T> extends LoDashImplicitWrapperBase<T[], LoDashImplicitArrayWrapper<T>> {
concat(...items: Array<T|Array<T>>): LoDashImplicitArrayWrapper<T>;
join(seperator?: string): string;
@@ -259,53 +242,11 @@ declare module _ {
unshift(...items: T[]): LoDashImplicitArrayWrapper<T>;
}
interface LoDashExplicitArrayWrapper<T> extends LoDashExplicitWrapperBase<T[], LoDashExplicitArrayWrapper<T>> { }
interface LoDashImplicitNumberArrayWrapper extends LoDashImplicitArrayWrapper<number> { }
//_.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<number>;
chain(value: string): LoDashImplicitWrapper<string>;
chain(value: boolean): LoDashImplicitWrapper<boolean>;
chain<T>(value: Array<T>): LoDashImplicitArrayWrapper<T>;
chain<T extends {}>(value: T): LoDashImplicitObjectWrapper<T>;
chain(value: any): LoDashImplicitWrapper<any>;
}
interface LoDashImplicitWrapperBase<T, TWrapper> {
/**
* 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<T>(
value: T,
interceptor: (value: T) => void): T;
}
interface LoDashImplicitWrapperBase<T, TWrapper> {
/**
* @see _.tap
**/
tap(interceptor: (value: T) => void): TWrapper;
}
interface LoDashExplicitNumberArrayWrapper extends LoDashExplicitArrayWrapper<number> { }
/*********
* 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<number>;
chain(value: string): LoDashExplicitWrapper<string>;
chain(value: boolean): LoDashExplicitWrapper<boolean>;
chain<T>(value: T[]): LoDashExplicitArrayWrapper<T>;
chain<T extends {}>(value: T): LoDashExplicitObjectWrapper<T>;
chain(value: any): LoDashExplicitWrapper<any>;
}
interface LoDashImplicitWrapper<T> {
/**
* @see _.chain
*/
chain(): LoDashExplicitWrapper<T>;
}
interface LoDashImplicitArrayWrapper<T> {
/**
* @see _.chain
*/
chain(): LoDashExplicitArrayWrapper<T>;
}
interface LoDashImplicitObjectWrapper<T> {
/**
* @see _.chain
*/
chain(): LoDashExplicitObjectWrapper<T>;
}
interface LoDashExplicitWrapperBase<T, TWrapper> {
/**
* @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<T>(
value: T,
interceptor: (value: T) => void,
thisArg?: any
): T;
}
interface LoDashImplicitWrapperBase<T, TWrapper> {
/**
* @see _.tap
*/
tap(
interceptor: (value: T) => void,
thisArg?: any
): TWrapper;
}
interface LoDashExplicitWrapperBase<T, TWrapper> {
/**
* @see _.tap
*/
tap(
interceptor: (value: T) => void,
thisArg?: any
): TWrapper;
}
//_.thru
interface LoDashStatic {
/**
@@ -2451,6 +2475,51 @@ declare module _ {
plant(value: any): LoDashImplicitWrapper<any>;
}
// _.run
interface LoDashWrapperBase<T, TWrapper> {
/**
* @see _.value
*/
run(): T;
}
// _.toJSON
interface LoDashWrapperBase<T, TWrapper> {
/**
* @see _.value
*/
toJSON(): T;
}
interface LoDashWrapperBase<T, TWrapper> {
/**
* Produces the result of coercing the unwrapped value to a string.
*
* @return Returns the coerced string value.
*/
toString(): string;
}
// _.value
interface LoDashWrapperBase<T, TWrapper> {
/**
* Executes the chained sequence to extract the unwrapped value.
*
* @alias _.run, _.toJSON, _.valueOf
*
* @return Returns the resolved unwrapped value.
*/
value(): T;
}
// _.valueOf
interface LoDashWrapperBase<T, TWrapper> {
/**
* @see _.value
*/
valueOf(): T;
}
/**************
* Collection *
**************/