-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathselectors.ts
51 lines (47 loc) · 1.48 KB
/
selectors.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
import { getIn } from '../utils/get-in';
import { Observable } from 'rxjs';
/**
* Custom equality checker that can be used with `.select` and `@select`.
* ```ts
* const customCompare: Comparator = (x: any, y: any) => {
* return x.id === y.id
* }
*
* @select(selector, customCompare)
* ```
*/
export type Comparator = (x: any, y: any) => boolean;
export type Transformer<RootState, V> = (
store$: Observable<RootState>,
scope: any
) => Observable<V>;
export type PropertySelector = string | number | symbol;
export type PathSelector = (string | number)[];
export type FunctionSelector<RootState, S> = ((s: RootState) => S);
export type Selector<RootState, S> =
| PropertySelector
| PathSelector
| FunctionSelector<RootState, S>;
/** @hidden */
export const sniffSelectorType = <RootState, S>(
selector?: Selector<RootState, S>
) =>
!selector
? 'nil'
: Array.isArray(selector)
? 'path'
: 'function' === typeof selector
? 'function'
: 'property';
/** @hidden */
export const resolver = <RootState, S>(selector?: Selector<RootState, S>) => ({
property: (state: any) =>
state ? state[selector as PropertySelector] : undefined,
path: (state: RootState) => getIn(state, selector as PathSelector),
function: selector as FunctionSelector<RootState, S>,
nil: (state: RootState) => state,
});
/** @hidden */
export const resolveToFunctionSelector = <RootState, S>(
selector?: Selector<RootState, S>
) => resolver(selector)[sniffSelectorType(selector)];