Skip to content

Commit

Permalink
fix(min): do not return comparer values
Browse files Browse the repository at this point in the history
min operator have now the expected behavior when used with a comparer:
- previously was behaving like _reduce_ and emitting (last) comparer return value
- now emits original observable min value by comparing the comparer return value to 0
- fixes #1892
  • Loading branch information
gluck committed Sep 8, 2016
1 parent f454e93 commit 222fd17
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
27 changes: 7 additions & 20 deletions spec/operators/min-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe('Observable.prototype.min', () => {
return 42;
};

expectObservable((<any>e1).min(predicate), unsub).toBe(expected, { w: 42 });
expectObservable((<any>e1).min(predicate), unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

Expand All @@ -188,46 +188,33 @@ describe('Observable.prototype.min', () => {
.min(predicate)
.mergeMap((x: number) => Observable.of(x));

expectObservable(result, unsub).toBe(expected, { w: 42 });
expectObservable(result, unsub).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should handle a constant predicate on observable with many values', () => {
const e1 = hot('-x-^-a-b-c-d-e-f-g-|');
const e1subs = '^ !';
const expected = '----------------(w|)';

const predicate = () => {
return 42;
};

expectObservable((<any>e1).min(predicate)).toBe(expected, { w: 42 });
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should handle a predicate on observable with many values', () => {
it('should handle a reverse predicate on observable with many values', () => {
const e1 = hot('-a-^-b--c--d-|', { a: 42, b: -1, c: 0, d: 666 });
const e1subs = '^ !';
const expected = '----------(w|)';

const predicate = function (x, y) {
return Math.max(x, y);
return x > y ? -1 : 1;
};

expectObservable((<any>e1).min(predicate)).toBe(expected, { w: 666 });
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should handle a predicate for string on observable with many values', () => {
const e1 = hot('-1-^-2--3--4-|');
const e1 = hot('-a-^-b--c--d-|');
const e1subs = '^ !';
const expected = '----------(w|)';

const predicate = function (x, y) {
return x < y ? x : y;
return x > y ? -1 : 1;
};

expectObservable((<any>e1).min(predicate)).toBe(expected, { w: '2' });
expectObservable((<any>e1).min(predicate)).toBe(expected, { w: 'd' });
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

Expand Down
8 changes: 4 additions & 4 deletions src/operator/min.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import { ReduceOperator } from './reduce';
* @method min
* @owner Observable
*/
export function min<T>(comparer?: (x: T, y: T) => T): Observable<T> {
const min: typeof comparer = (typeof comparer === 'function')
? comparer
export function min<T>(comparer?: (x: T, y: T) => number): Observable<T> {
const min: (x: T, y: T) => T = (typeof comparer === 'function')
? (x, y) => comparer(x, y) < 0 ? x : y
: (x, y) => x < y ? x : y;
return this.lift(new ReduceOperator(min));
}

export interface MinSignature<T> {
(comparer?: (x: T, y: T) => T): Observable<T>;
(comparer?: (x: T, y: T) => number): Observable<T>;
}

0 comments on commit 222fd17

Please sign in to comment.