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

partition.ts 2.7KB

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