All files select.ts

89.47% Statements 17/19
84.62% Branches 11/13
85.71% Functions 6/7
94.12% Lines 16/17
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61  1x 1x   1x 1x               1x       4x 4x   4x 4x 4x                         2x                       4x       3x 1x     1x     4x    
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/map';
 
import { getIn } from './get-in';
import { NgSelect } from './ng-select';
import {
  PropertySelector,
  PathSelector,
  FunctionSelector,
  Comparator
} from './types';
 
export function select<T>(
  selector?: PropertySelector | PathSelector | FunctionSelector<any, T>,
  comparator?: Comparator): PropertyDecorator {
 
  return function decorate(target: any, key: string): void {
    let bindingKey = selector || _getDefaultSelector(key);
 
    Eif (delete target[key]) {
      Object.defineProperty(target, key, {
        get: () => _select<T>(NgSelect.state$, bindingKey, comparator),
        enumerable: true,
        configurable: true
      });
    }
  };
}
 
/**
 * The default selector is the name of the property being decorated. If it ends
 * in the common '$' convention for Observable variables, we'll ignore the '$'.
 */
function _getDefaultSelector(key) {
  return (key.lastIndexOf('$') === key.length - 1) ?
    key.substring(0, key.length - 1) :
    key;
}
 
function _select<T>(
  observable$: Observable<any>,
  selector: PropertySelector | PathSelector | FunctionSelector<any, T>,
  comparator?: Comparator): Observable<T> {
 
  let result: Observable<T>;
 
  if (typeof selector === 'string' ||
      typeof selector === 'number' ||
      typeof selector === 'symbol') {
 
      result = observable$.map(state => state[selector as PropertySelector]);
  } else Iif (Array.isArray(selector)) {
      result = observable$.map(state => getIn(state, selector as PathSelector));
  } else {
      result = observable$.map(selector as FunctionSelector<any, T>);
  }
 
  return result.distinctUntilChanged(comparator);
}