| 1 |
import { not } from '../util/not'; |
| 2 |
import { filter } from './filter'; |
| 3 |
import { Observable } from '../Observable'; |
| 4 |
import { UnaryFunction } from '../types'; |
| 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 |
*  |
| 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 |
| 24 |
* |
| 25 |
* Partition click events into those on DIV elements and those elsewhere |
| 26 |
* |
| 27 |
* ```ts |
| 28 |
* import { fromEvent } from 'rxjs'; |
| 29 |
* import { partition } from 'rxjs/operators'; |
| 30 |
* |
| 31 |
* const div = document.createElement('div'); |
| 32 |
* div.style.cssText = 'width: 200px; height: 200px; background: #09c;'; |
| 33 |
* document.body.appendChild(div); |
| 34 |
* |
| 35 |
* const clicks = fromEvent(document, 'click'); |
| 36 |
* const [clicksOnDivs, clicksElsewhere] = clicks.pipe(partition(ev => (<HTMLElement>ev.target).tagName === 'DIV')); |
| 37 |
* |
| 38 |
* clicksOnDivs.subscribe(x => console.log('DIV clicked: ', x)); |
| 39 |
* clicksElsewhere.subscribe(x => console.log('Other clicked: ', x)); |
| 40 |
* ``` |
| 41 |
* |
| 42 |
* @see {@link filter} |
| 43 |
* |
| 44 |
* @param predicate A function that evaluates each value emitted by the source |
| 45 |
* Observable. If it returns `true`, the value is emitted on the first Observable |
| 46 |
* in the returned array, if `false` the value is emitted on the second Observable |
| 47 |
* in the array. The `index` parameter is the number `i` for the i-th source |
| 48 |
* emission that has happened since the subscription, starting from the number `0`. |
| 49 |
* @param thisArg An optional argument to determine the value of `this` in the |
| 50 |
* `predicate` function. |
| 51 |
* @return A function that returns an array with two Observables: one with |
| 52 |
* values that passed the predicate, and another with values that did not pass |
| 53 |
* the predicate. |
| 54 |
* @deprecated Replaced with the {@link partition} static creation function. Will be removed in v8. |
| 55 |
*/ |
| 56 |
export function partition<T>( |
| 57 |
predicate: (value: T, index: number) => boolean, |
| 58 |
thisArg?: any |
| 59 |
): UnaryFunction<Observable<T>, [Observable<T>, Observable<T>]> { |
| 60 |
return (source: Observable<T>) => |
| 61 |
[filter(predicate, thisArg)(source), filter(not(predicate, thisArg))(source)] as [Observable<T>, Observable<T>]; |
| 62 |
} |