findIndex.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { FindValueOperator } from '../operators/find';
  2. /**
  3. * Emits only the index of the first value emitted by the source Observable that
  4. * meets some condition.
  5. *
  6. * <span class="informal">It's like {@link find}, but emits the index of the
  7. * found value, not the value itself.</span>
  8. *
  9. * <img src="./img/findIndex.png" width="100%">
  10. *
  11. * `findIndex` searches for the first item in the source Observable that matches
  12. * the specified condition embodied by the `predicate`, and returns the
  13. * (zero-based) index of the first occurrence in the source. Unlike
  14. * {@link first}, the `predicate` is required in `findIndex`, and does not emit
  15. * an error if a valid value is not found.
  16. *
  17. * @example <caption>Emit the index of first click that happens on a DIV element</caption>
  18. * var clicks = Rx.Observable.fromEvent(document, 'click');
  19. * var result = clicks.findIndex(ev => ev.target.tagName === 'DIV');
  20. * result.subscribe(x => console.log(x));
  21. *
  22. * @see {@link filter}
  23. * @see {@link find}
  24. * @see {@link first}
  25. * @see {@link take}
  26. *
  27. * @param {function(value: T, index: number, source: Observable<T>): boolean} predicate
  28. * A function called with each item to test for condition matching.
  29. * @param {any} [thisArg] An optional argument to determine the value of `this`
  30. * in the `predicate` function.
  31. * @return {Observable} An Observable of the index of the first item that
  32. * matches the condition.
  33. * @method find
  34. * @owner Observable
  35. */
  36. export function findIndex(predicate, thisArg) {
  37. return (source) => source.lift(new FindValueOperator(predicate, source, true, thisArg));
  38. }
  39. //# sourceMappingURL=findIndex.js.map