Skip to content

Commit 595e588

Browse files
committed
feat(partition): add higher-order lettable version of partition
1 parent bb21a44 commit 595e588

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

src/operator/partition.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { not } from '../util/not';
2-
import { filter } from './filter';
31
import { Observable } from '../Observable';
4-
2+
import { partition as higherOrder } from '../operators/partition';
53
/**
64
* Splits the source Observable into two, one with values that satisfy a
75
* predicate, and another with values that don't satisfy the predicate.
@@ -44,8 +42,5 @@ import { Observable } from '../Observable';
4442
* @owner Observable
4543
*/
4644
export function partition<T>(this: Observable<T>, predicate: (value: T) => boolean, thisArg?: any): [Observable<T>, Observable<T>] {
47-
return [
48-
filter.call(this, predicate, thisArg),
49-
filter.call(this, not(predicate, thisArg))
50-
];
45+
return higherOrder(predicate, thisArg)(this);
5146
}

src/operators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export { multicast } from './multicast';
4747
export { observeOn } from './observeOn';
4848
export { onErrorResumeNext } from './onErrorResumeNext';
4949
export { pairwise } from './pairwise';
50+
export { partition } from './partition';
5051
export { publish } from './publish';
5152
export { race } from './race';
5253
export { reduce } from './reduce';

src/operators/partition.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { not } from '../util/not';
2+
import { filter } from './filter';
3+
import { Observable } from '../Observable';
4+
import { UnaryFunction } from '../interfaces';
5+
6+
/**
7+
* Splits the source Observable into two, one with values that satisfy a
8+
* predicate, and another with values that don't satisfy the predicate.
9+
*
10+
* <span class="informal">It's like {@link filter}, but returns two Observables:
11+
* one like the output of {@link filter}, and the other with values that did not
12+
* pass the condition.</span>
13+
*
14+
* <img src="./img/partition.png" width="100%">
15+
*
16+
* `partition` outputs an array with two Observables that partition the values
17+
* from the source Observable through the given `predicate` function. The first
18+
* Observable in that array emits source values for which the predicate argument
19+
* returns true. The second Observable emits source values for which the
20+
* predicate returns false. The first behaves like {@link filter} and the second
21+
* behaves like {@link filter} with the predicate negated.
22+
*
23+
* @example <caption>Partition click events into those on DIV elements and those elsewhere</caption>
24+
* var clicks = Rx.Observable.fromEvent(document, 'click');
25+
* var parts = clicks.partition(ev => ev.target.tagName === 'DIV');
26+
* var clicksOnDivs = parts[0];
27+
* var clicksElsewhere = parts[1];
28+
* clicksOnDivs.subscribe(x => console.log('DIV clicked: ', x));
29+
* clicksElsewhere.subscribe(x => console.log('Other clicked: ', x));
30+
*
31+
* @see {@link filter}
32+
*
33+
* @param {function(value: T, index: number): boolean} predicate A function that
34+
* evaluates each value emitted by the source Observable. If it returns `true`,
35+
* the value is emitted on the first Observable in the returned array, if
36+
* `false` the value is emitted on the second Observable in the array. The
37+
* `index` parameter is the number `i` for the i-th source emission that has
38+
* happened since the subscription, starting from the number `0`.
39+
* @param {any} [thisArg] An optional argument to determine the value of `this`
40+
* in the `predicate` function.
41+
* @return {[Observable<T>, Observable<T>]} An array with two Observables: one
42+
* with values that passed the predicate, and another with values that did not
43+
* pass the predicate.
44+
* @method partition
45+
* @owner Observable
46+
*/
47+
export function partition<T>(predicate: (value: T) => boolean, thisArg?: any): UnaryFunction<Observable<T>, [Observable<T>, Observable<T>]> {
48+
return (source: Observable<T>) => [
49+
filter(predicate, thisArg)(source),
50+
filter(not(predicate, thisArg) as any)(source)
51+
];
52+
}

0 commit comments

Comments
 (0)