1 import { Observable } from '../Observable';
2 import { map } from './map';
3 import { OperatorFunction } from '../types';
5 /* tslint:disable:max-line-length */
6 export function pluck<T, K1 extends keyof T>(k1: K1): OperatorFunction<T, T[K1]>;
7 export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1]>(k1: K1, k2: K2): OperatorFunction<T, T[K1][K2]>;
8 export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>(k1: K1, k2: K2, k3: K3): OperatorFunction<T, T[K1][K2][K3]>;
9 export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3]>(k1: K1, k2: K2, k3: K3, k4: K4): OperatorFunction<T, T[K1][K2][K3][K4]>;
10 export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3], K5 extends keyof T[K1][K2][K3][K4]>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5): OperatorFunction<T, T[K1][K2][K3][K4][K5]>;
11 export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2], K4 extends keyof T[K1][K2][K3], K5 extends keyof T[K1][K2][K3][K4], K6 extends keyof T[K1][K2][K3][K4][K5]>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6): OperatorFunction<T, T[K1][K2][K3][K4][K5][K6]>;
12 export function pluck<T, R>(...properties: string[]): OperatorFunction<T, R>;
13 /* tslint:enable:max-line-length */
16 * Maps each source value (an object) to its specified nested property.
18 * <span class="informal">Like {@link map}, but meant only for picking one of
19 * the nested properties of every emitted object.</span>
23 * Given a list of strings describing a path to an object property, retrieves
24 * the value of a specified nested property from all values in the source
25 * Observable. If a property can't be resolved, it will return `undefined` for
29 * Map every click to the tagName of the clicked target element
31 * import { fromEvent } from 'rxjs';
32 * import { pluck } from 'rxjs/operators';
34 * const clicks = fromEvent(document, 'click');
35 * const tagNames = clicks.pipe(pluck('target', 'tagName'));
36 * tagNames.subscribe(x => console.log(x));
41 * @param {...string} properties The nested properties to pluck from each source
43 * @return {Observable} A new Observable of property values from the source values.
47 export function pluck<T, R>(...properties: string[]): OperatorFunction<T, R> {
48 const length = properties.length;
50 throw new Error('list of properties cannot be empty.');
52 return (source: Observable<T>) => map(plucker(properties, length))(source as any);
55 function plucker(props: string[], length: number): (x: string) => any {
56 const mapper = (x: string) => {
58 for (let i = 0; i < length; i++) {
59 const p = currentProp != null ? currentProp[props[i]] : undefined;