From af80fa34293d95ef3a1ec18535235fafc6307dd5 Mon Sep 17 00:00:00 2001 From: David Driscoll Date: Sat, 18 Jan 2014 23:13:39 -0500 Subject: [PATCH 1/2] (feat): Updated function methods to be generic This allows those method signatures to be passed through, but gain the benefits of things like throttle, memoize. --- lodash/lodash.d.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 5de99d755..68ee4f8df 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -2580,10 +2580,10 @@ declare module _ { * @param options.trailing Specify execution on the trailing edge of the timeout. * @return The new debounced function. **/ - debounce( - func: Function, + debounce( + func: T, wait: number, - options?: DebounceSettings): Function; + options?: DebounceSettings): T; } interface LoDashObjectWrapper { @@ -2670,9 +2670,9 @@ declare module _ { * @param resolver Hash function for storing the result of `fn`. * @return Returns the new memoizing function. **/ - memoize( - func: Function, - resolver?: (n: any) => string): Function; + memoize( + func: T, + resolver?: (n: any) => string): T; } //_.once @@ -2684,7 +2684,7 @@ declare module _ { * @param func Function to only execute once. * @return The new restricted function. **/ - once(func: Function): Function; + once(func: T): T; } //_.partial @@ -2733,10 +2733,10 @@ declare module _ { * @param options.trailing Specify execution on the trailing edge of the timeout. * @return The new throttled function. **/ - throttle( - func: any, + throttle( + func: T, wait: number, - options?: ThrottleSettings): Function; + options?: ThrottleSettings): T; } interface ThrottleSettings { @@ -3764,7 +3764,7 @@ declare module _ { **/ times( n: number, - callback: Function, + callback: (num: number) => TResult, context?: any): TResult[]; } From 53a5613482ae9c7d384dc7cc1e691bc4778ae66c Mon Sep 17 00:00:00 2001 From: David Driscoll Date: Sat, 18 Jan 2014 23:13:39 -0500 Subject: [PATCH 2/2] (feat): Updated function methods to be generic This allows those method signatures to be passed through, but gain the benefits of things like throttle, memoize. --- lodash/lodash-tests.disabled.ts | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/lodash/lodash-tests.disabled.ts b/lodash/lodash-tests.disabled.ts index 18c3cadd6..aca0bbdf0 100644 --- a/lodash/lodash-tests.disabled.ts +++ b/lodash/lodash-tests.disabled.ts @@ -592,6 +592,9 @@ source.addEventListener('message', <_.LoDashObjectWrapper>_(function() 'maxWait': 1000 }), false); +var returnedDebounce = _.throttle(function (a) { return a * 5; }, 5); +returnedThrottled(4); + result = _.defer(function() { console.log('deferred'); }); result = <_.LoDashWrapper>_(function() { console.log('deferred'); }).defer(); @@ -608,15 +611,20 @@ var data = { 'curly': { 'name': 'curly', 'age': 60 } }; -var stooge = _.memoize(function(name: string) { return data[name]; }, _.identity); +var stooge = _.memoize(function(name: string) { return data[name]; }, _.identity); stooge('curly'); stooge['cache']['curly'].name = 'jerome'; stooge('curly'); -var initialize = _.once(function(){ }); -initialize(); +var returnedMemoize = _.throttle(function (a) { return a * 5; }, 5); +returnedMemoize(4); + +var initialize = _.once(function(){ }); initialize(); +initialize();'' +var returnedOnce = _.throttle(function (a) { return a * 5; }, 5); +returnedOnce(4); var greetPartial = function(greeting: string, name: string) { return greeting + ' ' + name; }; var hi = _.partial(greetPartial, 'hi'); @@ -638,6 +646,9 @@ jQuery('.interactive').on('click', _.throttle(function() { }, 300000, { 'trailing': false })); +var returnedThrottled = _.throttle(function (a) { return a*5; }, 5); +returnedThrottled(4); + var helloWrap = function(name: string) { return 'hello ' + name; }; var helloWrap2 = _.wrap(helloWrap, function(func) { return 'before, ' + func('moe') + ', after'; @@ -950,9 +961,9 @@ class Mage { } var mage = new Mage(); -result = _.times(3, _.partial(_.random, 1, 6)); -result = _.times(3, function(n: number) { mage.castSpell(n); }); -result = _.times(3, function(n: number) { this.cast(n); }, mage); +result = _.times(3, <() => number>_.partial(_.random, 1, 6)); +result = _.times(3, function(n: number) { mage.castSpell(n); }); +result = _.times(3, function(n: number) { this.cast(n); }, mage); result = _.unescape('Moe, Larry & Curly');