1 | import { distinctUntilChanged } from './distinctUntilChanged';
|
---|
2 | import { MonoTypeOperatorFunction } from '../types';
|
---|
3 |
|
---|
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 */
|
---|
8 |
|
---|
9 | /**
|
---|
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.
|
---|
12 | *
|
---|
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.
|
---|
14 | *
|
---|
15 | * If a comparator function is not provided, an equality check is used by default.
|
---|
16 | *
|
---|
17 | * ## Examples
|
---|
18 | * An example comparing the name of persons
|
---|
19 | * ```typescript
|
---|
20 | * import { of } from 'rxjs';
|
---|
21 | * import { distinctUntilKeyChanged } from 'rxjs/operators';
|
---|
22 | *
|
---|
23 | * interface Person {
|
---|
24 | * age: number,
|
---|
25 | * name: string
|
---|
26 | * }
|
---|
27 | *
|
---|
28 | * of<Person>(
|
---|
29 | * { age: 4, name: 'Foo'},
|
---|
30 | * { age: 7, name: 'Bar'},
|
---|
31 | * { age: 5, name: 'Foo'},
|
---|
32 | * { age: 6, name: 'Foo'},
|
---|
33 | * ).pipe(
|
---|
34 | * distinctUntilKeyChanged('name'),
|
---|
35 | * )
|
---|
36 | * .subscribe(x => console.log(x));
|
---|
37 | *
|
---|
38 | * // displays:
|
---|
39 | * // { age: 4, name: 'Foo' }
|
---|
40 | * // { age: 7, name: 'Bar' }
|
---|
41 | * // { age: 5, name: 'Foo' }
|
---|
42 | * ```
|
---|
43 | *
|
---|
44 | * An example comparing the first letters of the name
|
---|
45 | * ```typescript
|
---|
46 | * import { of } from 'rxjs';
|
---|
47 | * import { distinctUntilKeyChanged } from 'rxjs/operators';
|
---|
48 | *
|
---|
49 | * interface Person {
|
---|
50 | * age: number,
|
---|
51 | * name: string
|
---|
52 | * }
|
---|
53 | *
|
---|
54 | * of<Person>(
|
---|
55 | * { age: 4, name: 'Foo1'},
|
---|
56 | * { age: 7, name: 'Bar'},
|
---|
57 | * { age: 5, name: 'Foo2'},
|
---|
58 | * { age: 6, name: 'Foo3'},
|
---|
59 | * ).pipe(
|
---|
60 | * distinctUntilKeyChanged('name', (x: string, y: string) => x.substring(0, 3) === y.substring(0, 3)),
|
---|
61 | * )
|
---|
62 | * .subscribe(x => console.log(x));
|
---|
63 | *
|
---|
64 | * // displays:
|
---|
65 | * // { age: 4, name: 'Foo1' }
|
---|
66 | * // { age: 7, name: 'Bar' }
|
---|
67 | * // { age: 5, name: 'Foo2' }
|
---|
68 | * ```
|
---|
69 | *
|
---|
70 | * @see {@link distinct}
|
---|
71 | * @see {@link distinctUntilChanged}
|
---|
72 | *
|
---|
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
|
---|
77 | * @owner Observable
|
---|
78 | */
|
---|
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]);
|
---|
81 | }
|
---|