Code coverage report for src/operator/pluck.ts

Statements: 100% (18 / 18)      Branches: 100% (4 / 4)      Functions: 100% (3 / 3)      Lines: 100% (16 / 16)      Ignored: none     

All files » src/operator/ » pluck.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36  1                   28 12 12 1   11     1 11 32 32 54 54 51   3     29     11    
import {Observable} from '../Observable';
import {map} from './map';
 
/**
 * Retrieves the value of a specified nested property from all elements in
 * the Observable sequence. If a property can't be resolved, it will return
 * `undefined` for that value.
 *
 * @param {...args} properties the nested properties to pluck
 * @returns {Observable} Returns a new Observable sequence of property values
 */
export function pluck(...properties: string[]): Observable<any> {
  const length = properties.length;
  if (length === 0) {
    throw new Error('List of properties cannot be empty.');
  }
  return map.call(this, plucker(properties, length));
}
 
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;
}