ad0f23b5d5896a69544c631be9a0b155e5f0b6625e4ef10589205dd53be066a37f2eeec225ee125bb80fee5cf37ca09af6265279f08a793a2bea501d3a333b 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { Observable } from '../Observable';
  2. import { Falsy, OperatorFunction } from '../types';
  3. import { operate } from '../util/lift';
  4. import { createFind } from './find';
  5. export function findIndex<T>(predicate: BooleanConstructor): OperatorFunction<T, T extends Falsy ? -1 : number>;
  6. /** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
  7. export function findIndex<T>(predicate: BooleanConstructor, thisArg: any): OperatorFunction<T, T extends Falsy ? -1 : number>;
  8. /** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
  9. export function findIndex<T, A>(
  10. predicate: (this: A, value: T, index: number, source: Observable<T>) => boolean,
  11. thisArg: A
  12. ): OperatorFunction<T, number>;
  13. export function findIndex<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean): OperatorFunction<T, number>;
  14. /**
  15. * Emits only the index of the first value emitted by the source Observable that
  16. * meets some condition.
  17. *
  18. * <span class="informal">It's like {@link find}, but emits the index of the
  19. * found value, not the value itself.</span>
  20. *
  21. * ![](findIndex.png)
  22. *
  23. * `findIndex` searches for the first item in the source Observable that matches
  24. * the specified condition embodied by the `predicate`, and returns the
  25. * (zero-based) index of the first occurrence in the source. Unlike
  26. * {@link first}, the `predicate` is required in `findIndex`, and does not emit
  27. * an error if a valid value is not found.
  28. *
  29. * ## Example
  30. *
  31. * Emit the index of first click that happens on a DIV element
  32. *
  33. * ```ts
  34. * import { fromEvent, findIndex } from 'rxjs';
  35. *
  36. * const div = document.createElement('div');
  37. * div.style.cssText = 'width: 200px; height: 200px; background: #09c;';
  38. * document.body.appendChild(div);
  39. *
  40. * const clicks = fromEvent(document, 'click');
  41. * const result = clicks.pipe(findIndex(ev => (<HTMLElement>ev.target).tagName === 'DIV'));
  42. * result.subscribe(x => console.log(x));
  43. * ```
  44. *
  45. * @see {@link filter}
  46. * @see {@link find}
  47. * @see {@link first}
  48. * @see {@link take}
  49. *
  50. * @param predicate A function called with each item to test for condition matching.
  51. * @param thisArg An optional argument to determine the value of `this` in the
  52. * `predicate` function.
  53. * @return A function that returns an Observable that emits the index of the
  54. * first item that matches the condition.
  55. */
  56. export function findIndex<T>(
  57. predicate: (value: T, index: number, source: Observable<T>) => boolean,
  58. thisArg?: any
  59. ): OperatorFunction<T, number> {
  60. return operate(createFind(predicate, thisArg, 'index'));
  61. }