every.js 2.7 KB

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