056923527690a4f5189eb6874fefd34d49906b123498dd9eb9d433363baf5652e57d2680d02c3f9cf63198fa74b1ca3a6889c817604b02d3fa2401e7544f2a 2.8 KB

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