count.d.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { Observable } from '../Observable';
  2. import { OperatorFunction } from '../interfaces';
  3. /**
  4. * Counts the number of emissions on the source and emits that number when the
  5. * source completes.
  6. *
  7. * <span class="informal">Tells how many values were emitted, when the source
  8. * completes.</span>
  9. *
  10. * <img src="./img/count.png" width="100%">
  11. *
  12. * `count` transforms an Observable that emits values into an Observable that
  13. * emits a single value that represents the number of values emitted by the
  14. * source Observable. If the source Observable terminates with an error, `count`
  15. * will pass this error notification along without emitting a value first. If
  16. * the source Observable does not terminate at all, `count` will neither emit
  17. * a value nor terminate. This operator takes an optional `predicate` function
  18. * as argument, in which case the output emission will represent the number of
  19. * source values that matched `true` with the `predicate`.
  20. *
  21. * @example <caption>Counts how many seconds have passed before the first click happened</caption>
  22. * var seconds = Rx.Observable.interval(1000);
  23. * var clicks = Rx.Observable.fromEvent(document, 'click');
  24. * var secondsBeforeClick = seconds.takeUntil(clicks);
  25. * var result = secondsBeforeClick.count();
  26. * result.subscribe(x => console.log(x));
  27. *
  28. * @example <caption>Counts how many odd numbers are there between 1 and 7</caption>
  29. * var numbers = Rx.Observable.range(1, 7);
  30. * var result = numbers.count(i => i % 2 === 1);
  31. * result.subscribe(x => console.log(x));
  32. *
  33. * // Results in:
  34. * // 4
  35. *
  36. * @see {@link max}
  37. * @see {@link min}
  38. * @see {@link reduce}
  39. *
  40. * @param {function(value: T, i: number, source: Observable<T>): boolean} [predicate] A
  41. * boolean function to select what values are to be counted. It is provided with
  42. * arguments of:
  43. * - `value`: the value from the source Observable.
  44. * - `index`: the (zero-based) "index" of the value from the source Observable.
  45. * - `source`: the source Observable instance itself.
  46. * @return {Observable} An Observable of one number that represents the count as
  47. * described above.
  48. * @method count
  49. * @owner Observable
  50. */
  51. export declare function count<T>(predicate?: (value: T, index: number, source: Observable<T>) => boolean): OperatorFunction<T, number>;