ffca3c1f1dfcae9314a252955ef86dd04d91fcf2680c8cb927aaa9166337a7f4a30ce9b4c5ebae6e65ad9306265823162b1766eda1fb7653c13cb9559e72c8 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { OperatorFunction } from '../types';
  2. /**
  3. * Counts the number of emissions on the source and emits that number when the
  4. * source completes.
  5. *
  6. * <span class="informal">Tells how many values were emitted, when the source
  7. * completes.</span>
  8. *
  9. * ![](count.png)
  10. *
  11. * `count` transforms an Observable that emits values into an Observable that
  12. * emits a single value that represents the number of values emitted by the
  13. * source Observable. If the source Observable terminates with an error, `count`
  14. * will pass this error notification along without emitting a value first. If
  15. * the source Observable does not terminate at all, `count` will neither emit
  16. * a value nor terminate. This operator takes an optional `predicate` function
  17. * as argument, in which case the output emission will represent the number of
  18. * source values that matched `true` with the `predicate`.
  19. *
  20. * ## Examples
  21. *
  22. * Counts how many seconds have passed before the first click happened
  23. *
  24. * ```ts
  25. * import { interval, fromEvent, takeUntil, count } from 'rxjs';
  26. *
  27. * const seconds = interval(1000);
  28. * const clicks = fromEvent(document, 'click');
  29. * const secondsBeforeClick = seconds.pipe(takeUntil(clicks));
  30. * const result = secondsBeforeClick.pipe(count());
  31. * result.subscribe(x => console.log(x));
  32. * ```
  33. *
  34. * Counts how many odd numbers are there between 1 and 7
  35. *
  36. * ```ts
  37. * import { range, count } from 'rxjs';
  38. *
  39. * const numbers = range(1, 7);
  40. * const result = numbers.pipe(count(i => i % 2 === 1));
  41. * result.subscribe(x => console.log(x));
  42. * // Results in:
  43. * // 4
  44. * ```
  45. *
  46. * @see {@link max}
  47. * @see {@link min}
  48. * @see {@link reduce}
  49. *
  50. * @param predicate A function that is used to analyze the value and the index and
  51. * determine whether or not to increment the count. Return `true` to increment the count,
  52. * and return `false` to keep the count the same.
  53. * If the predicate is not provided, every value will be counted.
  54. * @return A function that returns an Observable that emits one number that
  55. * represents the count of emissions.
  56. */
  57. export declare function count<T>(predicate?: (value: T, index: number) => boolean): OperatorFunction<T, number>;
  58. //# sourceMappingURL=count.d.ts.map