Sha256: b644143a7ff790cb90f17a20fb1e53f4f7d7c3732071547ec88c6515e19fb3d2

Contents?: true

Size: 1.76 KB

Versions: 49

Compression:

Stored size: 1.76 KB

Contents

import { Observable } from '../Observable';
import { map } from './map';
import { OperatorFunction } from '../interfaces';

/**
 * Maps each source value (an object) to its specified nested property.
 *
 * <span class="informal">Like {@link map}, but meant only for picking one of
 * the nested properties of every emitted object.</span>
 *
 * <img src="./img/pluck.png" width="100%">
 *
 * 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 <caption>Map every click to the tagName of the clicked target element</caption>
 * var clicks = Rx.Observable.fromEvent(document, 'click');
 * var tagNames = clicks.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<T, R>(...properties: string[]): OperatorFunction<T, R> {
  const length = properties.length;
  if (length === 0) {
    throw new Error('list of properties cannot be empty.');
  }
  return (source: Observable<T>) => 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[props[i]];
      if (typeof p !== 'undefined') {
        currentProp = p;
      } else {
        return undefined;
      }
    }
    return currentProp;
  };

  return mapper;
}

Version data entries

49 entries across 49 versions & 4 rubygems

Version Path
govuk_publishing_components-17.8.0 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-17.7.0 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-17.6.1 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-17.6.0 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-17.5.0 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-17.4.0 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-17.3.0 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-17.2.0 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-17.1.1 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-17.1.0 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-17.0.0 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-16.29.0 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-16.28.0 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-16.27.1 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-16.27.0 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-16.26.0 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-16.25.0 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-16.24.0 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-16.23.0 node_modules/rxjs/src/operators/pluck.ts
govuk_publishing_components-16.22.0 node_modules/rxjs/src/operators/pluck.ts