@@ -56,6 +56,7 @@ type MemoDebugHandler<TReturn = unknown> = (info: { type: string; value: unknown
5656 * @property [cacheLimit] Number of entries to cache before overwriting previous entries (default: 1)
5757 * @property {MemoDebugHandler<TReturn> } [debug] Debug callback function
5858 * @property [expire] Expandable milliseconds until cache expires
59+ * @property [keyHash] Function to generate a predictable hash key from the provided arguments. Defaults to internal `generateHash`.
5960 * @property {OnMemoCacheHandler<TReturn> } [onCacheExpire] Callback when cache expires. Only fires when the `expire` option is set.
6061 * @property {OnMemoCacheHandler<TReturn> } [onCacheRollout] Callback when cache entries are rolled off due to cache limit.
6162 */
@@ -64,6 +65,7 @@ interface MemoOptions<TReturn = unknown> {
6465 cacheLimit ?: number ;
6566 debug ?: MemoDebugHandler < TReturn > ;
6667 expire ?: number ;
68+ keyHash ?: ( args : unknown [ ] ) => unknown ;
6769 onCacheExpire ?: OnMemoCacheHandler < TReturn > ;
6870 onCacheRollout ?: OnMemoCacheHandler < TReturn > ;
6971}
@@ -93,6 +95,7 @@ const memo = <TArgs extends unknown[], TReturn = unknown>(
9395 cacheLimit = 1 ,
9496 debug = ( ) => { } ,
9597 expire,
98+ keyHash = generateHash ,
9699 onCacheExpire,
97100 onCacheRollout
98101 } : MemoOptions < TReturn > = { }
@@ -104,6 +107,9 @@ const memo = <TArgs extends unknown[], TReturn = unknown>(
104107 const isOnCacheRolloutPromise = isPromise ( onCacheRollout ) ;
105108 const isOnCacheRollout = typeof onCacheRollout === 'function' || isOnCacheRolloutPromise ;
106109 const updatedExpire = Number . parseInt ( String ( expire ) , 10 ) || undefined ;
110+ const setKey = function ( value : unknown [ ] ) : unknown {
111+ return keyHash . call ( null , value ) ;
112+ } ;
107113
108114 const ized = function ( ) {
109115 const cache : MemoCache < TReturn > = [ ] ;
@@ -159,7 +165,7 @@ const memo = <TArgs extends unknown[], TReturn = unknown>(
159165 return bypassValue ;
160166 }
161167
162- const key = generateHash ( args ) ;
168+ const key = setKey ( args ) ;
163169
164170 // Parse, memoize and return the original value
165171 if ( cache . indexOf ( key ) < 0 ) {
0 commit comments