sequenceEqual.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var operators_1 = require("rxjs/operators");
  4. /**
  5. * Compares all values of two observables in sequence using an optional comparor function
  6. * and returns an observable of a single boolean value representing whether or not the two sequences
  7. * are equal.
  8. *
  9. * <span class="informal">Checks to see of all values emitted by both observables are equal, in order.</span>
  10. *
  11. * <img src="./img/sequenceEqual.png" width="100%">
  12. *
  13. * `sequenceEqual` subscribes to two observables and buffers incoming values from each observable. Whenever either
  14. * observable emits a value, the value is buffered and the buffers are shifted and compared from the bottom
  15. * up; If any value pair doesn't match, the returned observable will emit `false` and complete. If one of the
  16. * observables completes, the operator will wait for the other observable to complete; If the other
  17. * observable emits before completing, the returned observable will emit `false` and complete. If one observable never
  18. * completes or emits after the other complets, the returned observable will never complete.
  19. *
  20. * @example <caption>figure out if the Konami code matches</caption>
  21. * var code = Rx.Observable.from([
  22. * "ArrowUp",
  23. * "ArrowUp",
  24. * "ArrowDown",
  25. * "ArrowDown",
  26. * "ArrowLeft",
  27. * "ArrowRight",
  28. * "ArrowLeft",
  29. * "ArrowRight",
  30. * "KeyB",
  31. * "KeyA",
  32. * "Enter" // no start key, clearly.
  33. * ]);
  34. *
  35. * var keys = Rx.Observable.fromEvent(document, 'keyup')
  36. * .map(e => e.code);
  37. * var matches = keys.bufferCount(11, 1)
  38. * .mergeMap(
  39. * last11 =>
  40. * Rx.Observable.from(last11)
  41. * .sequenceEqual(code)
  42. * );
  43. * matches.subscribe(matched => console.log('Successful cheat at Contra? ', matched));
  44. *
  45. * @see {@link combineLatest}
  46. * @see {@link zip}
  47. * @see {@link withLatestFrom}
  48. *
  49. * @param {Observable} compareTo The observable sequence to compare the source sequence to.
  50. * @param {function} [comparor] An optional function to compare each value pair
  51. * @return {Observable} An Observable of a single boolean value representing whether or not
  52. * the values emitted by both observables were equal in sequence.
  53. * @method sequenceEqual
  54. * @owner Observable
  55. */
  56. function sequenceEqual(compareTo, comparor) {
  57. return operators_1.sequenceEqual(compareTo, comparor)(this);
  58. }
  59. exports.sequenceEqual = sequenceEqual;
  60. //# sourceMappingURL=sequenceEqual.js.map