From 6df9b38f97c5e5db380bdffbfbd83da18ac7cff4 Mon Sep 17 00:00:00 2001 From: Ilya Mochalov Date: Sat, 8 Aug 2015 02:08:12 +0500 Subject: [PATCH] lodash: changed _.memoize() method --- lodash/lodash-tests.ts | 30 ++++++++++++------- lodash/lodash.d.ts | 68 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 75 insertions(+), 23 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index 53a316ab50..3e59de3d08 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -89,6 +89,13 @@ class Dog { var result: any; +// _.MapCache +var testMapCache: _.MapCache; +result = <(key: string) => boolean>testMapCache.delete; +result = <(key: string) => any>testMapCache.get; +result = <(key: string) => boolean>testMapCache.has; +result = <(key: string, value: any) => _.Dictionary>testMapCache.set; + /************* * Chaining * *************/ @@ -909,17 +916,18 @@ var testFlowRightAddFn = (n: number, m: number) => n + m; result = _.flowRight<(n: number, m: number) => number>(testFlowRightSquareFn, testFlowRightAddFn)(1, 2); result = _(testFlowRightSquareFn).flowRight<(n: number, m: number) => number>(testFlowRightAddFn).value()(1, 2); -var fibonacci = _.memoize(function (n: any): number { - return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2); -}); - -var data: { [index: string]: { name: string; age: number; } } = { - 'moe': { 'name': 'moe', 'age': 40 }, - 'curly': { 'name': 'curly', 'age': 60 } -}; - -var stooge = _.memoize(function (name: string) { return data[name]; }, _.identity); -stooge('curly'); +// _.memoize +var testMemoizedFunction: _.MemoizedFunction; +result = <_.MapCache>testMemoizedFunction.cache; +interface TestMemoizedResultFn extends _.MemoizedFunction { + (...args: any[]): any; +} +var testMemoizeFn: (...args: any[]) => any; +var testMemoizeResolverFn: (...args: any[]) => any; +result = _.memoize(testMemoizeFn); +result = _.memoize(testMemoizeFn, testMemoizeResolverFn); +result = (_(testMemoizeFn).memoize().value()); +result = (_(testMemoizeFn).memoize(testMemoizeResolverFn).value()); var returnedMemoize = _.throttle(function (a: any) { return a * 5; }, 5); returnedMemoize(4); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index ea93f2e70c..2417a75495 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -94,6 +94,40 @@ declare module _ { variable?: string; } + /** + * Creates a cache object to store key/value pairs. + */ + interface MapCache { + /** + * Removes `key` and its value from the cache. + * @param key The key of the value to remove. + * @return Returns `true` if the entry was removed successfully, else `false`. + */ + delete(key: string): boolean; + + /** + * Gets the cached value for `key`. + * @param key The key of the value to get. + * @return Returns the cached value. + */ + get(key: string): any; + + /** + * Checks if a cached value for `key` exists. + * @param key The key of the entry to check. + * @return Returns `true` if an entry for `key` exists, else `false`. + */ + has(key: string): boolean; + + /** + * Sets `value` to `key` of the cache. + * @param key The key of the value to cache. + * @param value The value to cache. + * @return Returns the cache object. + */ + set(key: string, value: any): _.Dictionary; + } + /** * An object used to flag environments features. **/ @@ -5534,20 +5568,30 @@ declare module _ { } //_.memoize + interface MemoizedFunction extends Function { + cache: MapCache; + } + interface LoDashStatic { /** - * Creates a function that memoizes the result of func. If resolver is provided it will be - * used to determine the cache key for storing the result based on the arguments provided to - * the memoized function. By default, the first argument provided to the memoized function is - * used as the cache key. The func is executed with the this binding of the memoized function. - * The result cache is exposed as the cache property on the memoized function. - * @param func Computationally expensive function that will now memoized results. - * @param resolver Hash function for storing the result of `fn`. - * @return Returns the new memoizing function. - **/ - memoize( - func: T, - resolver?: Function): T; + * Creates a function that memoizes the result of func. If resolver is provided it determines the cache key for + * storing the result based on the arguments provided to the memoized function. By default, the first argument + * provided to the memoized function is coerced to a string and used as the cache key. The func is invoked with + * the this binding of the memoized function. + * @param func The function to have its output memoized. + * @param resolver The function to resolve the cache key. + * @return Returns the new memoizing function. + */ + memoize( + func: Function, + resolver?: Function): TResult; + } + + interface LoDashObjectWrapper { + /** + * @see _.memoize + */ + memoize(resolver?: Function): LoDashObjectWrapper; } //_.modArgs