1 import { distinctUntilChanged } from './distinctUntilChanged';
2 import { MonoTypeOperatorFunction } from '../types';
4 /* tslint:disable:max-line-length */
5 export function distinctUntilKeyChanged<T>(key: keyof T): MonoTypeOperatorFunction<T>;
6 export function distinctUntilKeyChanged<T, K extends keyof T>(key: K, compare: (x: T[K], y: T[K]) => boolean): MonoTypeOperatorFunction<T>;
7 /* tslint:enable:max-line-length */
10 * Returns an Observable that emits all items emitted by the source Observable that are distinct by comparison from the previous item,
11 * using a property accessed by using the key provided to check if the two items are distinct.
13 * 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.
15 * If a comparator function is not provided, an equality check is used by default.
18 * An example comparing the name of persons
20 * import { of } from 'rxjs';
21 * import { distinctUntilKeyChanged } from 'rxjs/operators';
29 * { age: 4, name: 'Foo'},
30 * { age: 7, name: 'Bar'},
31 * { age: 5, name: 'Foo'},
32 * { age: 6, name: 'Foo'},
34 * distinctUntilKeyChanged('name'),
36 * .subscribe(x => console.log(x));
39 * // { age: 4, name: 'Foo' }
40 * // { age: 7, name: 'Bar' }
41 * // { age: 5, name: 'Foo' }
44 * An example comparing the first letters of the name
46 * import { of } from 'rxjs';
47 * import { distinctUntilKeyChanged } from 'rxjs/operators';
55 * { age: 4, name: 'Foo1'},
56 * { age: 7, name: 'Bar'},
57 * { age: 5, name: 'Foo2'},
58 * { age: 6, name: 'Foo3'},
60 * distinctUntilKeyChanged('name', (x: string, y: string) => x.substring(0, 3) === y.substring(0, 3)),
62 * .subscribe(x => console.log(x));
65 * // { age: 4, name: 'Foo1' }
66 * // { age: 7, name: 'Bar' }
67 * // { age: 5, name: 'Foo2' }
70 * @see {@link distinct}
71 * @see {@link distinctUntilChanged}
73 * @param {string} key String key for object property lookup on each item.
74 * @param {function} [compare] Optional comparison function called to test if an item is distinct from the previous item in the source.
75 * @return {Observable} An Observable that emits items from the source Observable with distinct values based on the key specified.
76 * @method distinctUntilKeyChanged
79 export function distinctUntilKeyChanged<T, K extends keyof T>(key: K, compare?: (x: T[K], y: T[K]) => boolean): MonoTypeOperatorFunction<T> {
80 return distinctUntilChanged((x: T, y: T) => compare ? compare(x[key], y[key]) : x[key] === y[key]);