a zip code crypto-currency system good for red ONLY

count.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * Counts the number of emissions on the source and emits that number when the
  10. * source completes.
  11. *
  12. * <span class="informal">Tells how many values were emitted, when the source
  13. * completes.</span>
  14. *
  15. * <img src="./img/count.png" width="100%">
  16. *
  17. * `count` transforms an Observable that emits values into an Observable that
  18. * emits a single value that represents the number of values emitted by the
  19. * source Observable. If the source Observable terminates with an error, `count`
  20. * will pass this error notification along without emitting a value first. If
  21. * the source Observable does not terminate at all, `count` will neither emit
  22. * a value nor terminate. This operator takes an optional `predicate` function
  23. * as argument, in which case the output emission will represent the number of
  24. * source values that matched `true` with the `predicate`.
  25. *
  26. * @example <caption>Counts how many seconds have passed before the first click happened</caption>
  27. * var seconds = Rx.Observable.interval(1000);
  28. * var clicks = Rx.Observable.fromEvent(document, 'click');
  29. * var secondsBeforeClick = seconds.takeUntil(clicks);
  30. * var result = secondsBeforeClick.count();
  31. * result.subscribe(x => console.log(x));
  32. *
  33. * @example <caption>Counts how many odd numbers are there between 1 and 7</caption>
  34. * var numbers = Rx.Observable.range(1, 7);
  35. * var result = numbers.count(i => i % 2 === 1);
  36. * result.subscribe(x => console.log(x));
  37. *
  38. * // Results in:
  39. * // 4
  40. *
  41. * @see {@link max}
  42. * @see {@link min}
  43. * @see {@link reduce}
  44. *
  45. * @param {function(value: T, i: number, source: Observable<T>): boolean} [predicate] A
  46. * boolean function to select what values are to be counted. It is provided with
  47. * arguments of:
  48. * - `value`: the value from the source Observable.
  49. * - `index`: the (zero-based) "index" of the value from the source Observable.
  50. * - `source`: the source Observable instance itself.
  51. * @return {Observable} An Observable of one number that represents the count as
  52. * described above.
  53. * @method count
  54. * @owner Observable
  55. */
  56. function count(predicate) {
  57. return function (source) { return source.lift(new CountOperator(predicate, source)); };
  58. }
  59. exports.count = count;
  60. var CountOperator = (function () {
  61. function CountOperator(predicate, source) {
  62. this.predicate = predicate;
  63. this.source = source;
  64. }
  65. CountOperator.prototype.call = function (subscriber, source) {
  66. return source.subscribe(new CountSubscriber(subscriber, this.predicate, this.source));
  67. };
  68. return CountOperator;
  69. }());
  70. /**
  71. * We need this JSDoc comment for affecting ESDoc.
  72. * @ignore
  73. * @extends {Ignored}
  74. */
  75. var CountSubscriber = (function (_super) {
  76. __extends(CountSubscriber, _super);
  77. function CountSubscriber(destination, predicate, source) {
  78. _super.call(this, destination);
  79. this.predicate = predicate;
  80. this.source = source;
  81. this.count = 0;
  82. this.index = 0;
  83. }
  84. CountSubscriber.prototype._next = function (value) {
  85. if (this.predicate) {
  86. this._tryPredicate(value);
  87. }
  88. else {
  89. this.count++;
  90. }
  91. };
  92. CountSubscriber.prototype._tryPredicate = function (value) {
  93. var result;
  94. try {
  95. result = this.predicate(value, this.index++, this.source);
  96. }
  97. catch (err) {
  98. this.destination.error(err);
  99. return;
  100. }
  101. if (result) {
  102. this.count++;
  103. }
  104. };
  105. CountSubscriber.prototype._complete = function () {
  106. this.destination.next(this.count);
  107. this.destination.complete();
  108. };
  109. return CountSubscriber;
  110. }(Subscriber_1.Subscriber));
  111. //# sourceMappingURL=count.js.map