-
Notifications
You must be signed in to change notification settings - Fork 16
/
memoize-state.d.ts
120 lines (111 loc) · 3.63 KB
/
memoize-state.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
declare module 'memoize-state' {
interface MemoizeStateOptions {
/**
* the size of the cache
* @default 1
*/
cacheSize?: number,
/**
* allow shallow arguments check
* @default true
*/
shallowCheck?: boolean,
/**
* allow equal argiments check
* @default true
*/
equalCheck?: boolean,
/**
* respond only to the "used" amount of arguments (not working with spread)
* @default false
*/
strictArity?: boolean,
/**
* maintain the equal object equality
* @default true
*/
nestedEquality?: boolean,
/**
* perform additional safe checks (this could produce cache-wiping side effect)
* @default false
*/
safe?: boolean
}
interface PureOptions {
/**
* message displayed
*/
message?: string,
/**
* controls uber paranoidal "safe" check mode
*/
checkAffectedKeys?: boolean,
/**
* controls how to check `purity`. true will perform two calls with shallow non equal arguments, bypassing some sorts of memoization
* false will call twice with the same arguments, detecting only internal memoizations
*/
isolatedCheck?: boolean,
/**
* will call on failure
*/
onTrigger?: (...args: any[]) => void,
}
export type NotRequired<T extends { [key: string]: any }> = {
[K in keyof T]?: T[K];
};
export type Mapper<T, K = NotRequired<T> > = (input: T) => K;
type ResultFunction<T> = (input: T) => T;
/**
* Memoizes the function basing on paramiters actually used by a function
* @param {T} functor - function to be wrapped
* @param {MemoizeStateOptions} [memoizationOptions], options
* @return {T}
* @example
* const memoizedFn = memoize(fn)
*/
export default function memoize<T>(functor: T, memoizationOptions?: MemoizeStateOptions): T;
/**
* Flow(pipe) memoization composition.
* @param {Mapper[]} flow - array of functions to be applied from left to right
* @return {Function}
*/
export function memoizedFlow<T>(flow: Mapper<T>): ResultFunction<T>;
/**
* Flow(pipe) memoization composition.
* @param {Mapper[]} flow - array of functions to be applied from left to right
* @return {Function}
*/
export function memoizedPipe<T>(flow: Mapper<T>): ResultFunction<T>;
/**
* FlowRight(compose) memoization composition.
* @param {Mapper[]} flow - array of functions to be applied from right to left
* @return {Function}
*/
export function memoizedFlowRight<T>(flow: Mapper<T>): ResultFunction<T>;
/**
* FlowRight(compose) memoization composition.
* @param {Mapper[]} flow - array of functions to be applied from right to left
* @return {Function}
*/
export function memoizedCompose<T>(flow: Mapper<T>): ResultFunction<T>;
/**
* double checks that function inside the `executor` is a pure function
* @param {() => any} executor
* @return {boolean}
* @example
* isThisPure(() => someFn())
*/
export function isThisPure(executor: () => any): boolean
/**
* perform consistent checks, that fn is a pure function.
* @param {T} fn
* @return {T}
*/
export function shallBePure<T>(fn: T, options?: PureOptions): T
/**
* perform consistent checks, that fn is a pure function in DEV env only
* @param {T} fn
* @return {T}
*/
export function shouldBePure<T>(fn: T, options?: PureOptions): T
}