|
| 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