1 | import { Observable } from '../Observable';
|
---|
2 | import { OperatorFunction } from '../types';
|
---|
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 | * ![](count.png)
|
---|
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 | * ## Examples
|
---|
22 | *
|
---|
23 | * Counts how many seconds have passed before the first click happened
|
---|
24 | * ```ts
|
---|
25 | * import { fromEvent, interval } from 'rxjs';
|
---|
26 | * import { count, takeUntil } from 'rxjs/operators';
|
---|
27 | *
|
---|
28 | * const seconds = interval(1000);
|
---|
29 | * const clicks = fromEvent(document, 'click');
|
---|
30 | * const secondsBeforeClick = seconds.pipe(takeUntil(clicks));
|
---|
31 | * const result = secondsBeforeClick.pipe(count());
|
---|
32 | * result.subscribe(x => console.log(x));
|
---|
33 | * ```
|
---|
34 | *
|
---|
35 | * Counts how many odd numbers are there between 1 and 7
|
---|
36 | * ```ts
|
---|
37 | * import { range } from 'rxjs';
|
---|
38 | * import { count } from 'rxjs/operators';
|
---|
39 | *
|
---|
40 | * const numbers = range(1, 7);
|
---|
41 | * const result = numbers.pipe(count(i => i % 2 === 1));
|
---|
42 | * result.subscribe(x => console.log(x));
|
---|
43 | * // Results in:
|
---|
44 | * // 4
|
---|
45 | * ```
|
---|
46 | *
|
---|
47 | * @see {@link max}
|
---|
48 | * @see {@link min}
|
---|
49 | * @see {@link reduce}
|
---|
50 | *
|
---|
51 | * @param {function(value: T, i: number, source: Observable<T>): boolean} [predicate] A
|
---|
52 | * boolean function to select what values are to be counted. It is provided with
|
---|
53 | * arguments of:
|
---|
54 | * - `value`: the value from the source Observable.
|
---|
55 | * - `index`: the (zero-based) "index" of the value from the source Observable.
|
---|
56 | * - `source`: the source Observable instance itself.
|
---|
57 | * @return {Observable} An Observable of one number that represents the count as
|
---|
58 | * described above.
|
---|
59 | * @method count
|
---|
60 | * @owner Observable
|
---|
61 | */
|
---|
62 | export declare function count<T>(predicate?: (value: T, index: number, source: Observable<T>) => boolean): OperatorFunction<T, number>;
|
---|