File tree 2 files changed +14
-37
lines changed
2 files changed +14
-37
lines changed Original file line number Diff line number Diff line change 1
1
2
+ import _ from 'lodash' ;
2
3
import type { CachedFunctionOptions } from './index.d' ;
3
- import type { AnyFunction } from './paths.d' ;
4
+ import type { AnyFunction , ArgumentPaths } from './paths.d' ;
4
5
5
- export function cachedFunction < F extends AnyFunction > ( function_ : F , options : CachedFunctionOptions < F > ) {
6
- console . log ( options ) ;
7
- return function_ ;
6
+ export function selectorToCacheKey < F extends AnyFunction > ( arguments_ : Parameters < F > , selector : ArgumentPaths < F > ) {
7
+ const selectors = _ . castArray ( selector ) ;
8
+ const values = _ . at ( arguments_ , selectors ) ;
9
+ const result = _ . zipObject ( selectors , values ) ;
10
+ return JSON . stringify ( result ) ;
8
11
}
9
12
10
- /** TEST */
11
- type Person = {
12
- name : string ;
13
- age : number ;
14
- address : {
15
- city : string ;
16
- zip : number ;
13
+ export function cachedFunction < F extends AnyFunction > ( function_ : F , options : CachedFunctionOptions < F > ) {
14
+ return ( ...arguments_ : Parameters < F > ) : ReturnType < F > => {
15
+ const cacheKey = selectorToCacheKey ( arguments_ , options . selector ) ;
16
+ console . log ( { cacheKey} ) ;
17
+ return function_ ( ...arguments_ ) ;
17
18
} ;
18
- } ;
19
-
20
- const person : Person = {
21
- name : 'John Doe' ,
22
- age : 30 ,
23
- address : {
24
- city : 'New York' ,
25
- zip : 10_001 ,
26
- } ,
27
- } ;
28
-
29
- cachedFunction ( ( person : Person ) => person . name , {
30
- selector : person => person . name ,
31
- } ) ;
19
+ }
Original file line number Diff line number Diff line change @@ -34,15 +34,4 @@ export type Paths<T> = T extends Record<string, unknown>
34
34
* @typeparam F - The function type.
35
35
* @typeparam Paths - The paths of the arguments of the function type.
36
36
*/
37
- export type ArgumentPaths1 < F extends AnyFunction > = SingleOrArray < Paths < Parameters < F > > > ;
38
-
39
- /**
40
- * Represents a function that takes arguments of type `Parameters<F>` and returns `ArgumentPaths1<F>`.
41
- */
42
- export type ArgumentPaths2 < F extends AnyFunction > = ( ...arguments_ : Parameters < F > ) => ArgumentPaths1 < F > ;
43
-
44
- /**
45
- * Represents the argument paths for a given function type.
46
- * @template F - The function type.
47
- */
48
- export type ArgumentPaths < F extends AnyFunction > = ArgumentPaths1 < F > | ArgumentPaths2 < F > ;
37
+ export type ArgumentPaths < F extends AnyFunction > = SingleOrArray < Paths < Parameters < F > > > ;
You can’t perform that action at this time.
0 commit comments