distinctUntilKeyChanged.js 2.2 KB

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