0218e5b3a8406a454e5979a7200dbbf656740445796cc16533f44bfef8d6e05d9210e9931093b36f434f32e6f96cfa35432516df2c39bdf5dc705238150fd1 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { Operator } from '../Operator';
  2. import { Observable } from '../Observable';
  3. import { Subscriber } from '../Subscriber';
  4. import { Observer, OperatorFunction } from '../types';
  5. /**
  6. * Returns an Observable that emits whether or not every item of the source satisfies the condition specified.
  7. *
  8. * ## Example
  9. * A simple example emitting true if all elements are less than 5, false otherwise
  10. * ```ts
  11. * import { of } from 'rxjs';
  12. * import { every } from 'rxjs/operators';
  13. *
  14. * of(1, 2, 3, 4, 5, 6).pipe(
  15. * every(x => x < 5),
  16. * )
  17. * .subscribe(x => console.log(x)); // -> false
  18. * ```
  19. *
  20. * @param {function} predicate A function for determining if an item meets a specified condition.
  21. * @param {any} [thisArg] Optional object to use for `this` in the callback.
  22. * @return {Observable} An Observable of booleans that determines if all items of the source Observable meet the condition specified.
  23. * @method every
  24. * @owner Observable
  25. */
  26. export function every<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean,
  27. thisArg?: any): OperatorFunction<T, boolean> {
  28. return (source: Observable<T>) => source.lift(new EveryOperator(predicate, thisArg, source));
  29. }
  30. class EveryOperator<T> implements Operator<T, boolean> {
  31. constructor(private predicate: (value: T, index: number, source: Observable<T>) => boolean,
  32. private thisArg?: any,
  33. private source?: Observable<T>) {
  34. }
  35. call(observer: Subscriber<boolean>, source: any): any {
  36. return source.subscribe(new EverySubscriber(observer, this.predicate, this.thisArg, this.source));
  37. }
  38. }
  39. /**
  40. * We need this JSDoc comment for affecting ESDoc.
  41. * @ignore
  42. * @extends {Ignored}
  43. */
  44. class EverySubscriber<T> extends Subscriber<T> {
  45. private index: number = 0;
  46. constructor(destination: Observer<boolean>,
  47. private predicate: (value: T, index: number, source: Observable<T>) => boolean,
  48. private thisArg: any,
  49. private source?: Observable<T>) {
  50. super(destination);
  51. this.thisArg = thisArg || this;
  52. }
  53. private notifyComplete(everyValueMatch: boolean): void {
  54. this.destination.next(everyValueMatch);
  55. this.destination.complete();
  56. }
  57. protected _next(value: T): void {
  58. let result = false;
  59. try {
  60. result = this.predicate.call(this.thisArg, value, this.index++, this.source);
  61. } catch (err) {
  62. this.destination.error(err);
  63. return;
  64. }
  65. if (!result) {
  66. this.notifyComplete(false);
  67. }
  68. }
  69. protected _complete(): void {
  70. this.notifyComplete(true);
  71. }
  72. }