From e399334e365dd6f117a50c3406331710995806b7 Mon Sep 17 00:00:00 2001 From: Francois Valdy Date: Wed, 7 Sep 2016 14:04:49 +0200 Subject: [PATCH] fix(min): do not return comparer values 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 --- spec/operators/min-spec.ts | 27 +++++++-------------------- src/operator/min.ts | 8 ++++---- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/spec/operators/min-spec.ts b/spec/operators/min-spec.ts index 8919019ae19..c3c2b48a9c2 100644 --- a/spec/operators/min-spec.ts +++ b/spec/operators/min-spec.ts @@ -169,7 +169,7 @@ describe('Observable.prototype.min', () => { return 42; }; - expectObservable((e1).min(predicate), unsub).toBe(expected, { w: 42 }); + expectObservable((e1).min(predicate), unsub).toBe(expected); expectSubscriptions(e1.subscriptions).toBe(e1subs); }); @@ -188,30 +188,17 @@ 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((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((e1).min(predicate)).toBe(expected, { w: 666 }); @@ -219,15 +206,15 @@ describe('Observable.prototype.min', () => { }); 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((e1).min(predicate)).toBe(expected, { w: '2' }); + expectObservable((e1).min(predicate)).toBe(expected, { w: 'd' }); expectSubscriptions(e1.subscriptions).toBe(e1subs); }); diff --git a/src/operator/min.ts b/src/operator/min.ts index 71ef1cdd993..601a66bcfe2 100644 --- a/src/operator/min.ts +++ b/src/operator/min.ts @@ -12,13 +12,13 @@ import { ReduceOperator } from './reduce'; * @method min * @owner Observable */ -export function min(comparer?: (x: T, y: T) => T): Observable { - const min: typeof comparer = (typeof comparer === 'function') - ? comparer +export function min(comparer?: (x: T, y: T) => number): Observable { + 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 { - (comparer?: (x: T, y: T) => T): Observable; + (comparer?: (x: T, y: T) => number): Observable; }