8464a794907eb5b209b7dd4906c83c1b724e36133bfce455c2f2780d948c2d1663507a9208c430187f1584dada7ae5d7a3b1f25d467ca34ec475e12ded8da4 2.4 KB

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