source: trip-planner-front/node_modules/rxjs/src/internal/operators/partition.ts@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1import { not } from '../util/not';
2import { filter } from './filter';
3import { Observable } from '../Observable';
4import { 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 * ![](partition.png)
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 * Partition click events into those on DIV elements and those elsewhere
25 * ```ts
26 * import { fromEvent } from 'rxjs';
27 * import { partition } from 'rxjs/operators';
28 *
29 * const clicks = fromEvent(document, 'click');
30 * const parts = clicks.pipe(partition(ev => ev.target.tagName === 'DIV'));
31 * const clicksOnDivs = parts[0];
32 * const clicksElsewhere = parts[1];
33 * clicksOnDivs.subscribe(x => console.log('DIV clicked: ', x));
34 * clicksElsewhere.subscribe(x => console.log('Other clicked: ', x));
35 * ```
36 *
37 * @see {@link filter}
38 *
39 * @param {function(value: T, index: number): boolean} predicate A function that
40 * evaluates each value emitted by the source Observable. If it returns `true`,
41 * the value is emitted on the first Observable in the returned array, if
42 * `false` the value is emitted on the second Observable in the array. The
43 * `index` parameter is the number `i` for the i-th source emission that has
44 * happened since the subscription, starting from the number `0`.
45 * @param {any} [thisArg] An optional argument to determine the value of `this`
46 * in the `predicate` function.
47 * @return {[Observable<T>, Observable<T>]} An array with two Observables: one
48 * with values that passed the predicate, and another with values that did not
49 * pass the predicate.
50 * @method partition
51 * @owner Observable
52 * @deprecated use `partition` static creation function instead
53 */
54export function partition<T>(predicate: (value: T, index: number) => boolean,
55 thisArg?: any): UnaryFunction<Observable<T>, [Observable<T>, Observable<T>]> {
56 return (source: Observable<T>) => [
57 filter(predicate, thisArg)(source),
58 filter(not(predicate, thisArg) as any)(source)
59 ] as [Observable<T>, Observable<T>];
60}
Note: See TracBrowser for help on using the repository browser.