| 1 |
import { map } from './map'; |
| 2 |
import { OperatorFunction } from '../types'; |
| 3 |
|
| 4 |
/* tslint:disable:max-line-length */ |
| 5 |
/** @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. */ |
| 6 |
export function pluck<T, K1 extends keyof T>(k1: K1): OperatorFunction<T, T[K1]>; |
| 7 |
/** @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. */ |
| 8 |
export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1]>(k1: K1, k2: K2): OperatorFunction<T, T[K1][K2]>; |
| 9 |
/** @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. */ |
| 10 |
export function pluck<T, K1 extends keyof T, K2 extends keyof T[K1], K3 extends keyof T[K1][K2]>( |
| 11 |
k1: K1, |
| 12 |
k2: K2, |
| 13 |
k3: K3 |
| 14 |
): OperatorFunction<T, T[K1][K2][K3]>; |
| 15 |
/** @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. */ |
| 16 |
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]>( |
| 17 |
k1: K1, |
| 18 |
k2: K2, |
| 19 |
k3: K3, |
| 20 |
k4: K4 |
| 21 |
): OperatorFunction<T, T[K1][K2][K3][K4]>; |
| 22 |
/** @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. */ |
| 23 |
export function pluck< |
| 24 |
T, |
| 25 |
K1 extends keyof T, |
| 26 |
K2 extends keyof T[K1], |
| 27 |
K3 extends keyof T[K1][K2], |
| 28 |
K4 extends keyof T[K1][K2][K3], |
| 29 |
K5 extends keyof T[K1][K2][K3][K4] |
| 30 |
>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5): OperatorFunction<T, T[K1][K2][K3][K4][K5]>; |
| 31 |
/** @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. */ |
| 32 |
export function pluck< |
| 33 |
T, |
| 34 |
K1 extends keyof T, |
| 35 |
K2 extends keyof T[K1], |
| 36 |
K3 extends keyof T[K1][K2], |
| 37 |
K4 extends keyof T[K1][K2][K3], |
| 38 |
K5 extends keyof T[K1][K2][K3][K4], |
| 39 |
K6 extends keyof T[K1][K2][K3][K4][K5] |
| 40 |
>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6): OperatorFunction<T, T[K1][K2][K3][K4][K5][K6]>; |
| 41 |
/** @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. */ |
| 42 |
export function pluck< |
| 43 |
T, |
| 44 |
K1 extends keyof T, |
| 45 |
K2 extends keyof T[K1], |
| 46 |
K3 extends keyof T[K1][K2], |
| 47 |
K4 extends keyof T[K1][K2][K3], |
| 48 |
K5 extends keyof T[K1][K2][K3][K4], |
| 49 |
K6 extends keyof T[K1][K2][K3][K4][K5] |
| 50 |
>(k1: K1, k2: K2, k3: K3, k4: K4, k5: K5, k6: K6, ...rest: string[]): OperatorFunction<T, unknown>; |
| 51 |
/** @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. */ |
| 52 |
export function pluck<T>(...properties: string[]): OperatorFunction<T, unknown>; |
| 53 |
/* tslint:enable:max-line-length */ |
| 54 |
|
| 55 |
/** |
| 56 |
* Maps each source value to its specified nested property. |
| 57 |
* |
| 58 |
* <span class="informal">Like {@link map}, but meant only for picking one of |
| 59 |
* the nested properties of every emitted value.</span> |
| 60 |
* |
| 61 |
*  |
| 62 |
* |
| 63 |
* Given a list of strings or numbers describing a path to a property, retrieves |
| 64 |
* the value of a specified nested property from all values in the source |
| 65 |
* Observable. If a property can't be resolved, it will return `undefined` for |
| 66 |
* that value. |
| 67 |
* |
| 68 |
* ## Example |
| 69 |
* |
| 70 |
* Map every click to the tagName of the clicked target element |
| 71 |
* |
| 72 |
* ```ts |
| 73 |
* import { fromEvent, pluck } from 'rxjs'; |
| 74 |
* |
| 75 |
* const clicks = fromEvent(document, 'click'); |
| 76 |
* const tagNames = clicks.pipe(pluck('target', 'tagName')); |
| 77 |
* |
| 78 |
* tagNames.subscribe(x => console.log(x)); |
| 79 |
* ``` |
| 80 |
* |
| 81 |
* @see {@link map} |
| 82 |
* |
| 83 |
* @param properties The nested properties to pluck from each source |
| 84 |
* value. |
| 85 |
* @return A function that returns an Observable of property values from the |
| 86 |
* source values. |
| 87 |
* @deprecated Use {@link map} and optional chaining: `pluck('foo', 'bar')` is `map(x => x?.foo?.bar)`. Will be removed in v8. |
| 88 |
*/ |
| 89 |
export function pluck<T, R>(...properties: Array<string | number | symbol>): OperatorFunction<T, R> { |
| 90 |
const length = properties.length; |
| 91 |
if (length === 0) { |
| 92 |
throw new Error('list of properties cannot be empty.'); |
| 93 |
} |
| 94 |
return map((x) => { |
| 95 |
let currentProp: any = x; |
| 96 |
for (let i = 0; i < length; i++) { |
| 97 |
const p = currentProp?.[properties[i]]; |
| 98 |
if (typeof p !== 'undefined') { |
| 99 |
currentProp = p; |
| 100 |
} else { |
| 101 |
return undefined; |
| 102 |
} |
| 103 |
} |
| 104 |
return currentProp; |
| 105 |
}); |
| 106 |
} |