every.js 2.8KB

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