Skip to content

Commit

Permalink
refactor(count): Base off of reduce.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: No longer passes `source` observable as a third argument to the predicate. That feature was rarely used, and of limited value. The workaround is to simply close over the source inside of the function if you need to access it in there.
  • Loading branch information
benlesh committed Sep 27, 2020
1 parent 9ba2b15 commit 98a6d09
Showing 1 changed file with 7 additions and 29 deletions.
36 changes: 7 additions & 29 deletions src/internal/operators/count.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/** @prettier */
import { Observable } from '../Observable';
import { OperatorFunction } from '../types';
import { operate } from '../util/lift';
import { OperatorSubscriber } from './OperatorSubscriber';
import { reduce } from './reduce';
/**
* Counts the number of emissions on the source and emits that number when the
* source completes.
Expand Down Expand Up @@ -51,32 +49,12 @@ import { OperatorSubscriber } from './OperatorSubscriber';
* @see {@link min}
* @see {@link reduce}
*
* @param {function(value: T, i: number, source: Observable<T>): boolean} [predicate] A
* boolean function to select what values are to be counted. It is provided with
* arguments of:
* - `value`: the value from the source Observable.
* - `index`: the (zero-based) "index" of the value from the source Observable.
* - `source`: the source Observable instance itself.
* @return {Observable} An Observable of one number that represents the count as
* described above.
* @name count
* @param predicate A function that is used to analyze the value and the index and
* determine whether or not to increment the count. Return `true` to increment the count,
* and return `false` to keep the count the same.
* If the predicate is not provided, every value will be counted.
*/

export function count<T>(predicate?: (value: T, index: number, source: Observable<T>) => boolean): OperatorFunction<T, number> {
return operate((source, subscriber) => {
let index = 0;
let count = 0;

source.subscribe(
new OperatorSubscriber(
subscriber,
(value) => (!predicate || predicate(value, index++, source)) && count++,
undefined,
() => {
subscriber.next(count);
subscriber.complete();
}
)
);
});
export function count<T>(predicate?: (value: T, index: number) => boolean): OperatorFunction<T, number> {
return reduce((count, value, i) => (!predicate || predicate(value, i) ? count + 1 : count), 0);
}

0 comments on commit 98a6d09

Please sign in to comment.