| 1 |
import { not } from '../util/not'; |
| 2 |
import { filter } from '../operators/filter'; |
| 3 |
import { ObservableInput } from '../types'; |
| 4 |
import { Observable } from '../Observable'; |
| 5 |
import { innerFrom } from './innerFrom'; |
| 6 |
|
| 7 |
/** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */ |
| 8 |
export function partition<T, U extends T, A>( |
| 9 |
source: ObservableInput<T>, |
| 10 |
predicate: (this: A, value: T, index: number) => value is U, |
| 11 |
thisArg: A |
| 12 |
): [Observable<U>, Observable<Exclude<T, U>>]; |
| 13 |
export function partition<T, U extends T>( |
| 14 |
source: ObservableInput<T>, |
| 15 |
predicate: (value: T, index: number) => value is U |
| 16 |
): [Observable<U>, Observable<Exclude<T, U>>]; |
| 17 |
|
| 18 |
/** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */ |
| 19 |
export function partition<T, A>( |
| 20 |
source: ObservableInput<T>, |
| 21 |
predicate: (this: A, value: T, index: number) => boolean, |
| 22 |
thisArg: A |
| 23 |
): [Observable<T>, Observable<T>]; |
| 24 |
export function partition<T>(source: ObservableInput<T>, predicate: (value: T, index: number) => boolean): [Observable<T>, Observable<T>]; |
| 25 |
|
| 26 |
/** |
| 27 |
* Splits the source Observable into two, one with values that satisfy a |
| 28 |
* predicate, and another with values that don't satisfy the predicate. |
| 29 |
* |
| 30 |
* <span class="informal">It's like {@link filter}, but returns two Observables: |
| 31 |
* one like the output of {@link filter}, and the other with values that did not |
| 32 |
* pass the condition.</span> |
| 33 |
* |
| 34 |
*  |
| 35 |
* |
| 36 |
* `partition` outputs an array with two Observables that partition the values |
| 37 |
* from the source Observable through the given `predicate` function. The first |
| 38 |
* Observable in that array emits source values for which the predicate argument |
| 39 |
* returns true. The second Observable emits source values for which the |
| 40 |
* predicate returns false. The first behaves like {@link filter} and the second |
| 41 |
* behaves like {@link filter} with the predicate negated. |
| 42 |
* |
| 43 |
* ## Example |
| 44 |
* |
| 45 |
* Partition a set of numbers into odds and evens observables |
| 46 |
* |
| 47 |
* ```ts |
| 48 |
* import { of, partition } from 'rxjs'; |
| 49 |
* |
| 50 |
* const observableValues = of(1, 2, 3, 4, 5, 6); |
| 51 |
* const [evens$, odds$] = partition(observableValues, value => value % 2 === 0); |
| 52 |
* |
| 53 |
* odds$.subscribe(x => console.log('odds', x)); |
| 54 |
* evens$.subscribe(x => console.log('evens', x)); |
| 55 |
* |
| 56 |
* // Logs: |
| 57 |
* // odds 1 |
| 58 |
* // odds 3 |
| 59 |
* // odds 5 |
| 60 |
* // evens 2 |
| 61 |
* // evens 4 |
| 62 |
* // evens 6 |
| 63 |
* ``` |
| 64 |
* |
| 65 |
* @see {@link filter} |
| 66 |
* |
| 67 |
* @param source The source `ObservableInput` that will be split into a tuple of |
| 68 |
* two Observable elements. |
| 69 |
* @param predicate A function that evaluates each value emitted by the source |
| 70 |
* Observable. If it returns `true`, the value is emitted on the first Observable |
| 71 |
* in the returned array, if `false` the value is emitted on the second Observable |
| 72 |
* in the array. The `index` parameter is the number `i` for the i-th source |
| 73 |
* emission that has happened since the subscription, starting from the number `0`. |
| 74 |
* @param thisArg An optional argument to determine the value of `this` in the |
| 75 |
* `predicate` function. |
| 76 |
* @return An array with two Observables: one with values that passed the |
| 77 |
* predicate, and another with values that did not pass the predicate. |
| 78 |
*/ |
| 79 |
export function partition<T>( |
| 80 |
source: ObservableInput<T>, |
| 81 |
predicate: (this: any, value: T, index: number) => boolean, |
| 82 |
thisArg?: any |
| 83 |
): [Observable<T>, Observable<T>] { |
| 84 |
return [filter(predicate, thisArg)(innerFrom(source)), filter(not(predicate, thisArg))(innerFrom(source))] as [ |
| 85 |
Observable<T>, |
| 86 |
Observable<T> |
| 87 |
]; |
| 88 |
} |