diff --git a/src/operator/partition.ts b/src/operator/partition.ts index c494190d6e..c5026eddb2 100644 --- a/src/operator/partition.ts +++ b/src/operator/partition.ts @@ -1,7 +1,5 @@ -import { not } from '../util/not'; -import { filter } from './filter'; import { Observable } from '../Observable'; - +import { partition as higherOrder } from '../operators/partition'; /** * Splits the source Observable into two, one with values that satisfy a * predicate, and another with values that don't satisfy the predicate. @@ -44,8 +42,5 @@ import { Observable } from '../Observable'; * @owner Observable */ export function partition(this: Observable, predicate: (value: T) => boolean, thisArg?: any): [Observable, Observable] { - return [ - filter.call(this, predicate, thisArg), - filter.call(this, not(predicate, thisArg)) - ]; + return higherOrder(predicate, thisArg)(this); } diff --git a/src/operators/index.ts b/src/operators/index.ts index 01a74a9f01..baa5444a42 100644 --- a/src/operators/index.ts +++ b/src/operators/index.ts @@ -47,6 +47,7 @@ export { multicast } from './multicast'; export { observeOn } from './observeOn'; export { onErrorResumeNext } from './onErrorResumeNext'; export { pairwise } from './pairwise'; +export { partition } from './partition'; export { publish } from './publish'; export { race } from './race'; export { reduce } from './reduce'; diff --git a/src/operators/partition.ts b/src/operators/partition.ts new file mode 100644 index 0000000000..0a6e846b4e --- /dev/null +++ b/src/operators/partition.ts @@ -0,0 +1,52 @@ +import { not } from '../util/not'; +import { filter } from './filter'; +import { Observable } from '../Observable'; +import { UnaryFunction } from '../interfaces'; + +/** + * Splits the source Observable into two, one with values that satisfy a + * predicate, and another with values that don't satisfy the predicate. + * + * It's like {@link filter}, but returns two Observables: + * one like the output of {@link filter}, and the other with values that did not + * pass the condition. + * + * + * + * `partition` outputs an array with two Observables that partition the values + * from the source Observable through the given `predicate` function. The first + * Observable in that array emits source values for which the predicate argument + * returns true. The second Observable emits source values for which the + * predicate returns false. The first behaves like {@link filter} and the second + * behaves like {@link filter} with the predicate negated. + * + * @example Partition click events into those on DIV elements and those elsewhere + * var clicks = Rx.Observable.fromEvent(document, 'click'); + * var parts = clicks.partition(ev => ev.target.tagName === 'DIV'); + * var clicksOnDivs = parts[0]; + * var clicksElsewhere = parts[1]; + * clicksOnDivs.subscribe(x => console.log('DIV clicked: ', x)); + * clicksElsewhere.subscribe(x => console.log('Other clicked: ', x)); + * + * @see {@link filter} + * + * @param {function(value: T, index: number): boolean} predicate A function that + * evaluates each value emitted by the source Observable. If it returns `true`, + * the value is emitted on the first Observable in the returned array, if + * `false` the value is emitted on the second Observable in the array. The + * `index` parameter is the number `i` for the i-th source emission that has + * happened since the subscription, starting from the number `0`. + * @param {any} [thisArg] An optional argument to determine the value of `this` + * in the `predicate` function. + * @return {[Observable, Observable]} An array with two Observables: one + * with values that passed the predicate, and another with values that did not + * pass the predicate. + * @method partition + * @owner Observable + */ +export function partition(predicate: (value: T) => boolean, thisArg?: any): UnaryFunction, [Observable, Observable]> { + return (source: Observable) => [ + filter(predicate, thisArg)(source), + filter(not(predicate, thisArg) as any)(source) + ]; +}