|
| 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 | +} |
0 commit comments