-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathselect.ts
More file actions
26 lines (22 loc) · 838 Bytes
/
Copy pathselect.ts
File metadata and controls
26 lines (22 loc) · 838 Bytes
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
import { pluck } from 'rxjs/operator/pluck';
import { map } from 'rxjs/operator/map';
import { distinctUntilChanged } from 'rxjs/operator/distinctUntilChanged';
import { Observable } from 'rxjs/Observable';
export interface SelectSignature<T> {
<R>(...paths: string[]): Observable<R>;
<R>(mapFn: (state: T) => R): Observable<R>;
}
export function select<T, R>(pathOrMapFn: any, ...paths: string[]): Observable<R> {
let mapped$: Observable<R>;
if (typeof pathOrMapFn === 'string') {
mapped$ = pluck.call(this, pathOrMapFn, ...paths);
}
else if (typeof pathOrMapFn === 'function') {
mapped$ = map.call(this, pathOrMapFn);
}
else {
throw new TypeError(`Unexpected type ${ typeof pathOrMapFn } in select operator,`
+ ` expected 'string' or 'function'`);
}
return distinctUntilChanged.call(mapped$);
}