Skip to content

Commit 9db141c

Browse files
committed
feat(distinctUntilKeyChanged): add higher-order lettable version of distinctUntilKeyChanged
1 parent b2725e7 commit 9db141c

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

src/operator/distinctUntilKeyChanged.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { distinctUntilChanged } from './distinctUntilChanged';
1+
22
import { Observable } from '../Observable';
3+
import { distinctUntilKeyChanged as higherOrder } from '../operators';
34

45
/* tslint:disable:max-line-length */
56
export function distinctUntilKeyChanged<T>(this: Observable<T>, key: string): Observable<T>;
@@ -64,10 +65,5 @@ export function distinctUntilKeyChanged<T, K>(this: Observable<T>, key: string,
6465
* @owner Observable
6566
*/
6667
export function distinctUntilKeyChanged<T>(this: Observable<T>, key: string, compare?: (x: T, y: T) => boolean): Observable<T> {
67-
return distinctUntilChanged.call(this, function(x: T, y: T) {
68-
if (compare) {
69-
return compare(x[key], y[key]);
70-
}
71-
return x[key] === y[key];
72-
});
68+
return higherOrder<T, T>(key, compare)(this);
7369
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { distinctUntilChanged } from './distinctUntilChanged';
2+
import { MonoTypeOperatorFunction } from '../interfaces';
3+
4+
/* tslint:disable:max-line-length */
5+
export function distinctUntilKeyChanged<T>(key: string): MonoTypeOperatorFunction<T>;
6+
export function distinctUntilKeyChanged<T, K>(key: string, compare: (x: K, y: 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+
* @example <caption>An example comparing the name of persons</caption>
18+
*
19+
* interface Person {
20+
* age: number,
21+
* name: string
22+
* }
23+
*
24+
* Observable.of<Person>(
25+
* { age: 4, name: 'Foo'},
26+
* { age: 7, name: 'Bar'},
27+
* { age: 5, name: 'Foo'},
28+
* { age: 6, name: 'Foo'})
29+
* .distinctUntilKeyChanged('name')
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+
* @example <caption>An example comparing the first letters of the name</caption>
38+
*
39+
* interface Person {
40+
* age: number,
41+
* name: string
42+
* }
43+
*
44+
* Observable.of<Person>(
45+
* { age: 4, name: 'Foo1'},
46+
* { age: 7, name: 'Bar'},
47+
* { age: 5, name: 'Foo2'},
48+
* { age: 6, name: 'Foo3'})
49+
* .distinctUntilKeyChanged('name', (x: string, y: string) => x.substring(0, 3) === y.substring(0, 3))
50+
* .subscribe(x => console.log(x));
51+
*
52+
* // displays:
53+
* // { age: 4, name: 'Foo1' }
54+
* // { age: 7, name: 'Bar' }
55+
* // { age: 5, name: 'Foo2' }
56+
*
57+
* @see {@link distinct}
58+
* @see {@link distinctUntilChanged}
59+
*
60+
* @param {string} key String key for object property lookup on each item.
61+
* @param {function} [compare] Optional comparison function called to test if an item is distinct from the previous item in the source.
62+
* @return {Observable} An Observable that emits items from the source Observable with distinct values based on the key specified.
63+
* @method distinctUntilKeyChanged
64+
* @owner Observable
65+
*/
66+
export function distinctUntilKeyChanged<T>(key: string, compare?: (x: T, y: T) => boolean): MonoTypeOperatorFunction<T> {
67+
return distinctUntilChanged((x: T, y: T) => compare ? compare(x[key], y[key]) : x[key] === y[key]);
68+
}

src/operators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export { delay } from './delay';
1818
export { delayWhen } from './delayWhen';
1919
export { dematerialize } from './dematerialize';
2020
export { distinctUntilChanged } from './distinctUntilChanged';
21+
export { distinctUntilKeyChanged } from './distinctUntilKeyChanged';
2122
export { filter } from './filter';
2223
export { ignoreElements } from './ignoreElements';
2324
export { map } from './map';

0 commit comments

Comments
 (0)