Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / rxjs / src / internal / observable / partition.ts
1 import { not } from '../util/not';
2 import { subscribeTo } from '../util/subscribeTo';
3 import { filter } from '../operators/filter';
4 import { ObservableInput } from '../types';
5 import { Observable } from '../Observable';
6
7 /**
8  * Splits the source Observable into two, one with values that satisfy a
9  * predicate, and another with values that don't satisfy the predicate.
10  *
11  * <span class="informal">It's like {@link filter}, but returns two Observables:
12  * one like the output of {@link filter}, and the other with values that did not
13  * pass the condition.</span>
14  *
15  * ![](partition.png)
16  *
17  * `partition` outputs an array with two Observables that partition the values
18  * from the source Observable through the given `predicate` function. The first
19  * Observable in that array emits source values for which the predicate argument
20  * returns true. The second Observable emits source values for which the
21  * predicate returns false. The first behaves like {@link filter} and the second
22  * behaves like {@link filter} with the predicate negated.
23  *
24  * ## Example
25  * Partition a set of numbers into odds and evens observables
26  * ```ts
27  * import { of, partition } from 'rxjs';
28  *
29  * const observableValues = of(1, 2, 3, 4, 5, 6);
30  * const [evens$, odds$] = partition(observableValues, (value, index) => value % 2 === 0);
31  *
32  * odds$.subscribe(x => console.log('odds', x));
33  * evens$.subscribe(x => console.log('evens', x));
34  *
35  * // Logs:
36  * // odds 1
37  * // odds 3
38  * // odds 5
39  * // evens 2
40  * // evens 4
41  * // evens 6
42  * ```
43  *
44  * @see {@link filter}
45  *
46  * @param {function(value: T, index: number): boolean} predicate A function that
47  * evaluates each value emitted by the source Observable. If it returns `true`,
48  * the value is emitted on the first Observable in the returned array, if
49  * `false` the value is emitted on the second Observable in the array. The
50  * `index` parameter is the number `i` for the i-th source emission that has
51  * happened since the subscription, starting from the number `0`.
52  * @param {any} [thisArg] An optional argument to determine the value of `this`
53  * in the `predicate` function.
54  * @return {[Observable<T>, Observable<T>]} An array with two Observables: one
55  * with values that passed the predicate, and another with values that did not
56  * pass the predicate.
57  */
58 export function partition<T>(
59   source: ObservableInput<T>,
60   predicate: (value: T, index: number) => boolean,
61   thisArg?: any
62 ): [Observable<T>, Observable<T>] {
63   return [
64     filter(predicate, thisArg)(new Observable<T>(subscribeTo(source))),
65     filter(not(predicate, thisArg) as any)(new Observable<T>(subscribeTo(source)))
66   ] as [Observable<T>, Observable<T>];
67 }