Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

partition.ts 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. * 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 a set of numbers into odds and evens observables
  25. * ```ts
  26. * import { of, partition } from 'rxjs';
  27. *
  28. * const observableValues = of(1, 2, 3, 4, 5, 6);
  29. * const [evens$, odds$] = partition(observableValues, (value, index) => value % 2 === 0);
  30. *
  31. * odds$.subscribe(x => console.log('odds', x));
  32. * evens$.subscribe(x => console.log('evens', x));
  33. *
  34. * // Logs:
  35. * // odds 1
  36. * // odds 3
  37. * // odds 5
  38. * // evens 2
  39. * // evens 4
  40. * // evens 6
  41. * ```
  42. *
  43. * @see {@link filter}
  44. *
  45. * @param {function(value: T, index: number): boolean} predicate A function that
  46. * evaluates each value emitted by the source Observable. If it returns `true`,
  47. * the value is emitted on the first Observable in the returned array, if
  48. * `false` the value is emitted on the second Observable in the array. The
  49. * `index` parameter is the number `i` for the i-th source emission that has
  50. * happened since the subscription, starting from the number `0`.
  51. * @param {any} [thisArg] An optional argument to determine the value of `this`
  52. * in the `predicate` function.
  53. * @return {[Observable<T>, Observable<T>]} An array with two Observables: one
  54. * with values that passed the predicate, and another with values that did not
  55. * pass the predicate.
  56. */
  57. export function partition<T>(
  58. source: ObservableInput<T>,
  59. predicate: (value: T, index: number) => boolean,
  60. thisArg?: any
  61. ): [Observable<T>, Observable<T>] {
  62. return [
  63. filter(predicate, thisArg)(new Observable<T>(subscribeTo(source))),
  64. filter(not(predicate, thisArg) as any)(new Observable<T>(subscribeTo(source)))
  65. ] as [Observable<T>, Observable<T>];
  66. }