lodash: changed _.memoize() method

This commit is contained in:
Ilya Mochalov
2015-08-08 02:08:12 +05:00
parent a95ee80de0
commit 6df9b38f97
2 changed files with 75 additions and 23 deletions

View File

@@ -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<any>>testMapCache.set;
/*************
* Chaining *
*************/
@@ -909,17 +916,18 @@ var testFlowRightAddFn = (n: number, m: number) => n + m;
result = <number>_.flowRight<(n: number, m: number) => number>(testFlowRightSquareFn, testFlowRightAddFn)(1, 2);
result = <number>_(testFlowRightSquareFn).flowRight<(n: number, m: number) => number>(testFlowRightAddFn).value()(1, 2);
var fibonacci = <Function>_.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 = <TestMemoizedResultFn>_.memoize<TestMemoizedResultFn>(testMemoizeFn);
result = <TestMemoizedResultFn>_.memoize<TestMemoizedResultFn>(testMemoizeFn, testMemoizeResolverFn);
result = <TestMemoizedResultFn>(_(testMemoizeFn).memoize<TestMemoizedResultFn>().value());
result = <TestMemoizedResultFn>(_(testMemoizeFn).memoize<TestMemoizedResultFn>(testMemoizeResolverFn).value());
var returnedMemoize = _.throttle(function (a: any) { return a * 5; }, 5);
returnedMemoize(4);

68
lodash/lodash.d.ts vendored
View File

@@ -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<any>;
}
/**
* 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<T extends Function>(
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<TResult extends MemoizedFunction>(
func: Function,
resolver?: Function): TResult;
}
interface LoDashObjectWrapper<T> {
/**
* @see _.memoize
*/
memoize<TResult extends MemoizedFunction>(resolver?: Function): LoDashObjectWrapper<TResult>;
}
//_.modArgs