import { Observable } from '../Observable'; import { map } from './map'; import { OperatorFunction } from '../types'; /* tslint:disable:max-line-length */ export function pluck(k1: K1): OperatorFunction; export function pluck(k1: K1, k2: K2): OperatorFunction; export function pluck(k1: K1, k2: K2, k3: K3): OperatorFunction; export function pluck(k1: K1, k2: K2, k3: K3, k4: K4): OperatorFunction; export function pluck(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5): OperatorFunction; export function pluck(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6): OperatorFunction; export function pluck(...properties: string[]): OperatorFunction; /* tslint:enable:max-line-length */ /** * Maps each source value (an object) to its specified nested property. * * Like {@link map}, but meant only for picking one of * the nested properties of every emitted object. * * ![](pluck.png) * * Given a list of strings describing a path to an object property, retrieves * the value of a specified nested property from all values in the source * Observable. If a property can't be resolved, it will return `undefined` for * that value. * * ## Example * Map every click to the tagName of the clicked target element * ```ts * import { fromEvent } from 'rxjs'; * import { pluck } from 'rxjs/operators'; * * const clicks = fromEvent(document, 'click'); * const tagNames = clicks.pipe(pluck('target', 'tagName')); * tagNames.subscribe(x => console.log(x)); * ``` * * @see {@link map} * * @param {...string} properties The nested properties to pluck from each source * value (an object). * @return {Observable} A new Observable of property values from the source values. * @method pluck * @owner Observable */ export function pluck(...properties: string[]): OperatorFunction { const length = properties.length; if (length === 0) { throw new Error('list of properties cannot be empty.'); } return (source: Observable) => map(plucker(properties, length))(source as any); } function plucker(props: string[], length: number): (x: string) => any { const mapper = (x: string) => { let currentProp = x; for (let i = 0; i < length; i++) { const p = currentProp != null ? currentProp[props[i]] : undefined; if (p !== void 0) { currentProp = p; } else { return undefined; } } return currentProp; }; return mapper; }