bcc8cfab93a5461680c992aa56fff23ba6e830e0d8dcde0a95b4ccb95ec1d1edeb4824ace43c59123a95b08d9b786ef296591e69054fe83191758512bc5e06 3.5 KB

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