87b5fd6c7afec406972ffbf5a12afabe7a4c42ef642203530297b60693c87a5b5440a8f66b53649a33b476f32944a7e5c4f5cb179e31764e053b2abdf0e933 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { Observable } from '../Observable';
  2. import { Falsy, OperatorFunction } from '../types';
  3. import { operate } from '../util/lift';
  4. import { createOperatorSubscriber } from './OperatorSubscriber';
  5. export function every<T>(predicate: BooleanConstructor): OperatorFunction<T, Exclude<T, Falsy> extends never ? false : boolean>;
  6. /** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
  7. export function every<T>(
  8. predicate: BooleanConstructor,
  9. thisArg: any
  10. ): OperatorFunction<T, Exclude<T, Falsy> extends never ? false : boolean>;
  11. /** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
  12. export function every<T, A>(
  13. predicate: (this: A, value: T, index: number, source: Observable<T>) => boolean,
  14. thisArg: A
  15. ): OperatorFunction<T, boolean>;
  16. export function every<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean): OperatorFunction<T, boolean>;
  17. /**
  18. * Returns an Observable that emits whether or not every item of the source satisfies the condition specified.
  19. *
  20. * <span class="informal">If all values pass predicate before the source completes, emits true before completion,
  21. * otherwise emit false, then complete.</span>
  22. *
  23. * ![](every.png)
  24. *
  25. * ## Example
  26. *
  27. * A simple example emitting true if all elements are less than 5, false otherwise
  28. *
  29. * ```ts
  30. * import { of, every } from 'rxjs';
  31. *
  32. * of(1, 2, 3, 4, 5, 6)
  33. * .pipe(every(x => x < 5))
  34. * .subscribe(x => console.log(x)); // -> false
  35. * ```
  36. *
  37. * @param predicate A function for determining if an item meets a specified condition.
  38. * @param thisArg Optional object to use for `this` in the callback.
  39. * @return A function that returns an Observable of booleans that determines if
  40. * all items of the source Observable meet the condition specified.
  41. */
  42. export function every<T>(
  43. predicate: (value: T, index: number, source: Observable<T>) => boolean,
  44. thisArg?: any
  45. ): OperatorFunction<T, boolean> {
  46. return operate((source, subscriber) => {
  47. let index = 0;
  48. source.subscribe(
  49. createOperatorSubscriber(
  50. subscriber,
  51. (value) => {
  52. if (!predicate.call(thisArg, value, index++, source)) {
  53. subscriber.next(false);
  54. subscriber.complete();
  55. }
  56. },
  57. () => {
  58. subscriber.next(true);
  59. subscriber.complete();
  60. }
  61. )
  62. );
  63. });
  64. }