6d198ea36f7208b9a3cccda4d4fe553a979cf1ec34b64505a1856e671a6d8c918f423c6c15ca911f1b739e4d3d9e7e895f739dae1d405d197bbc66fe1c9385 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { OperatorFunction, MonoTypeOperatorFunction, TruthyTypesOf } from '../types';
  2. import { operate } from '../util/lift';
  3. import { createOperatorSubscriber } from './OperatorSubscriber';
  4. /** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
  5. export function filter<T, S extends T, A>(predicate: (this: A, value: T, index: number) => value is S, thisArg: A): OperatorFunction<T, S>;
  6. export function filter<T, S extends T>(predicate: (value: T, index: number) => value is S): OperatorFunction<T, S>;
  7. export function filter<T>(predicate: BooleanConstructor): OperatorFunction<T, TruthyTypesOf<T>>;
  8. /** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
  9. export function filter<T, A>(predicate: (this: A, value: T, index: number) => boolean, thisArg: A): MonoTypeOperatorFunction<T>;
  10. export function filter<T>(predicate: (value: T, index: number) => boolean): MonoTypeOperatorFunction<T>;
  11. /**
  12. * Filter items emitted by the source Observable by only emitting those that
  13. * satisfy a specified predicate.
  14. *
  15. * <span class="informal">Like
  16. * [Array.prototype.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter),
  17. * it only emits a value from the source if it passes a criterion function.</span>
  18. *
  19. * ![](filter.png)
  20. *
  21. * Similar to the well-known `Array.prototype.filter` method, this operator
  22. * takes values from the source Observable, passes them through a `predicate`
  23. * function and only emits those values that yielded `true`.
  24. *
  25. * ## Example
  26. *
  27. * Emit only click events whose target was a DIV element
  28. *
  29. * ```ts
  30. * import { fromEvent, filter } from 'rxjs';
  31. *
  32. * const div = document.createElement('div');
  33. * div.style.cssText = 'width: 200px; height: 200px; background: #09c;';
  34. * document.body.appendChild(div);
  35. *
  36. * const clicks = fromEvent(document, 'click');
  37. * const clicksOnDivs = clicks.pipe(filter(ev => (<HTMLElement>ev.target).tagName === 'DIV'));
  38. * clicksOnDivs.subscribe(x => console.log(x));
  39. * ```
  40. *
  41. * @see {@link distinct}
  42. * @see {@link distinctUntilChanged}
  43. * @see {@link distinctUntilKeyChanged}
  44. * @see {@link ignoreElements}
  45. * @see {@link partition}
  46. * @see {@link skip}
  47. *
  48. * @param predicate A function that
  49. * evaluates each value emitted by the source Observable. If it returns `true`,
  50. * the value is emitted, if `false` the value is not passed to the output
  51. * Observable. The `index` parameter is the number `i` for the i-th source
  52. * emission that has happened since the subscription, starting from the number
  53. * `0`.
  54. * @param thisArg An optional argument to determine the value of `this`
  55. * in the `predicate` function.
  56. * @return A function that returns an Observable that emits items from the
  57. * source Observable that satisfy the specified `predicate`.
  58. */
  59. export function filter<T>(predicate: (value: T, index: number) => boolean, thisArg?: any): MonoTypeOperatorFunction<T> {
  60. return operate((source, subscriber) => {
  61. // An index passed to our predicate function on each call.
  62. let index = 0;
  63. // Subscribe to the source, all errors and completions are
  64. // forwarded to the consumer.
  65. source.subscribe(
  66. // Call the predicate with the appropriate `this` context,
  67. // if the predicate returns `true`, then send the value
  68. // to the consumer.
  69. createOperatorSubscriber(subscriber, (value) => predicate.call(thisArg, value, index++) && subscriber.next(value))
  70. );
  71. });
  72. }