source: trip-planner-front/node_modules/rxjs/src/internal/operators/pluck.ts@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.1 KB
Line 
1import { Observable } from '../Observable';
2import { map } from './map';
3import { OperatorFunction } from '../types';
4
5/* tslint:disable:max-line-length */
6export function pluck<T, K1 extends keyof T>(k1: K1): OperatorFunction<T, T[K1]>;
7export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1]>(k1: K1, k2: K2): OperatorFunction<T, T[K1][K2]>;
8export 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]>;
9export 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]>;
10export 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]>;
11export 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]>;
12export function pluck<T, R>(...properties: string[]): OperatorFunction<T, R>;
13/* tslint:enable:max-line-length */
14
15/**
16 * Maps each source value (an object) to its specified nested property.
17 *
18 * <span class="informal">Like {@link map}, but meant only for picking one of
19 * the nested properties of every emitted object.</span>
20 *
21 * ![](pluck.png)
22 *
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
26 * that value.
27 *
28 * ## Example
29 * Map every click to the tagName of the clicked target element
30 * ```ts
31 * import { fromEvent } from 'rxjs';
32 * import { pluck } from 'rxjs/operators';
33 *
34 * const clicks = fromEvent(document, 'click');
35 * const tagNames = clicks.pipe(pluck('target', 'tagName'));
36 * tagNames.subscribe(x => console.log(x));
37 * ```
38 *
39 * @see {@link map}
40 *
41 * @param {...string} properties The nested properties to pluck from each source
42 * value (an object).
43 * @return {Observable} A new Observable of property values from the source values.
44 * @method pluck
45 * @owner Observable
46 */
47export function pluck<T, R>(...properties: string[]): OperatorFunction<T, R> {
48 const length = properties.length;
49 if (length === 0) {
50 throw new Error('list of properties cannot be empty.');
51 }
52 return (source: Observable<T>) => map(plucker(properties, length))(source as any);
53}
54
55function plucker(props: string[], length: number): (x: string) => any {
56 const mapper = (x: string) => {
57 let currentProp = x;
58 for (let i = 0; i < length; i++) {
59 const p = currentProp != null ? currentProp[props[i]] : undefined;
60 if (p !== void 0) {
61 currentProp = p;
62 } else {
63 return undefined;
64 }
65 }
66 return currentProp;
67 };
68
69 return mapper;
70}
Note: See TracBrowser for help on using the repository browser.