cfee1d7f1e53a1d8e03d0fb449707fa8bab255a527fa706475e09e12e7956675152b3ea4943887f56c0d7e677ced6e1c1105c12e99665bf949c4d269d5bf51 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { not } from '../util/not';
  2. import { filter } from './filter';
  3. import { Observable } from '../Observable';
  4. import { UnaryFunction } from '../types';
  5. /**
  6. * Splits the source Observable into two, one with values that satisfy a
  7. * predicate, and another with values that don't satisfy the predicate.
  8. *
  9. * <span class="informal">It's like {@link filter}, but returns two Observables:
  10. * one like the output of {@link filter}, and the other with values that did not
  11. * pass the condition.</span>
  12. *
  13. * ![](partition.png)
  14. *
  15. * `partition` outputs an array with two Observables that partition the values
  16. * from the source Observable through the given `predicate` function. The first
  17. * Observable in that array emits source values for which the predicate argument
  18. * returns true. The second Observable emits source values for which the
  19. * predicate returns false. The first behaves like {@link filter} and the second
  20. * behaves like {@link filter} with the predicate negated.
  21. *
  22. * ## Example
  23. *
  24. * Partition click events into those on DIV elements and those elsewhere
  25. *
  26. * ```ts
  27. * import { fromEvent } from 'rxjs';
  28. * import { partition } from 'rxjs/operators';
  29. *
  30. * const div = document.createElement('div');
  31. * div.style.cssText = 'width: 200px; height: 200px; background: #09c;';
  32. * document.body.appendChild(div);
  33. *
  34. * const clicks = fromEvent(document, 'click');
  35. * const [clicksOnDivs, clicksElsewhere] = clicks.pipe(partition(ev => (<HTMLElement>ev.target).tagName === 'DIV'));
  36. *
  37. * clicksOnDivs.subscribe(x => console.log('DIV clicked: ', x));
  38. * clicksElsewhere.subscribe(x => console.log('Other clicked: ', x));
  39. * ```
  40. *
  41. * @see {@link filter}
  42. *
  43. * @param predicate A function that evaluates each value emitted by the source
  44. * Observable. If it returns `true`, the value is emitted on the first Observable
  45. * in the returned array, if `false` the value is emitted on the second Observable
  46. * in the array. The `index` parameter is the number `i` for the i-th source
  47. * emission that has happened since the subscription, starting from the number `0`.
  48. * @param thisArg An optional argument to determine the value of `this` in the
  49. * `predicate` function.
  50. * @return A function that returns an array with two Observables: one with
  51. * values that passed the predicate, and another with values that did not pass
  52. * the predicate.
  53. * @deprecated Replaced with the {@link partition} static creation function. Will be removed in v8.
  54. */
  55. export function partition<T>(
  56. predicate: (value: T, index: number) => boolean,
  57. thisArg?: any
  58. ): UnaryFunction<Observable<T>, [Observable<T>, Observable<T>]> {
  59. return (source: Observable<T>) =>
  60. [filter(predicate, thisArg)(source), filter(not(predicate, thisArg))(source)] as [Observable<T>, Observable<T>];
  61. }