distinctUntilKeyChanged.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. var distinctUntilChanged_1 = require('./distinctUntilChanged');
  3. /* tslint:enable:max-line-length */
  4. /**
  5. * Returns an Observable that emits all items emitted by the source Observable that are distinct by comparison from the previous item,
  6. * using a property accessed by using the key provided to check if the two items are distinct.
  7. *
  8. * If a comparator function is provided, then it will be called for each item to test for whether or not that value should be emitted.
  9. *
  10. * If a comparator function is not provided, an equality check is used by default.
  11. *
  12. * @example <caption>An example comparing the name of persons</caption>
  13. *
  14. * interface Person {
  15. * age: number,
  16. * name: string
  17. * }
  18. *
  19. * Observable.of<Person>(
  20. * { age: 4, name: 'Foo'},
  21. * { age: 7, name: 'Bar'},
  22. * { age: 5, name: 'Foo'},
  23. * { age: 6, name: 'Foo'})
  24. * .distinctUntilKeyChanged('name')
  25. * .subscribe(x => console.log(x));
  26. *
  27. * // displays:
  28. * // { age: 4, name: 'Foo' }
  29. * // { age: 7, name: 'Bar' }
  30. * // { age: 5, name: 'Foo' }
  31. *
  32. * @example <caption>An example comparing the first letters of the name</caption>
  33. *
  34. * interface Person {
  35. * age: number,
  36. * name: string
  37. * }
  38. *
  39. * Observable.of<Person>(
  40. * { age: 4, name: 'Foo1'},
  41. * { age: 7, name: 'Bar'},
  42. * { age: 5, name: 'Foo2'},
  43. * { age: 6, name: 'Foo3'})
  44. * .distinctUntilKeyChanged('name', (x: string, y: string) => x.substring(0, 3) === y.substring(0, 3))
  45. * .subscribe(x => console.log(x));
  46. *
  47. * // displays:
  48. * // { age: 4, name: 'Foo1' }
  49. * // { age: 7, name: 'Bar' }
  50. * // { age: 5, name: 'Foo2' }
  51. *
  52. * @see {@link distinct}
  53. * @see {@link distinctUntilChanged}
  54. *
  55. * @param {string} key String key for object property lookup on each item.
  56. * @param {function} [compare] Optional comparison function called to test if an item is distinct from the previous item in the source.
  57. * @return {Observable} An Observable that emits items from the source Observable with distinct values based on the key specified.
  58. * @method distinctUntilKeyChanged
  59. * @owner Observable
  60. */
  61. function distinctUntilKeyChanged(key, compare) {
  62. return distinctUntilChanged_1.distinctUntilChanged(function (x, y) { return compare ? compare(x[key], y[key]) : x[key] === y[key]; });
  63. }
  64. exports.distinctUntilKeyChanged = distinctUntilKeyChanged;
  65. //# sourceMappingURL=distinctUntilKeyChanged.js.map