From fb3a327ce937ecfd4d0db33480e591d8cdf5d132 Mon Sep 17 00:00:00 2001 From: Francois Valdy Date: Wed, 7 Sep 2016 14:03:13 +0200 Subject: [PATCH] fix(max): do not return comparer values max 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 max value by comparing the comparer return value to 0 - fixes #1892 --- spec/operators/max-spec.ts | 23 +++++------------------ src/operator/max.ts | 6 +++--- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/spec/operators/max-spec.ts b/spec/operators/max-spec.ts index 3e470435c7f..b41cdc0c7fe 100644 --- a/spec/operators/max-spec.ts +++ b/spec/operators/max-spec.ts @@ -184,26 +184,13 @@ describe('Observable.prototype.max', () => { 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).max(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.min(x, y); + return x > y ? -1 : 1; }; expectObservable((e1).max(predicate)).toBe(expected, { w: -1 }); @@ -211,15 +198,15 @@ describe('Observable.prototype.max', () => { }); 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).max(predicate)).toBe(expected, { w: '4' }); + expectObservable((e1).max(predicate)).toBe(expected, { w: 'b' }); expectSubscriptions(e1.subscriptions).toBe(e1subs); }); diff --git a/src/operator/max.ts b/src/operator/max.ts index 5b51f80e5cc..9083dbb71ec 100644 --- a/src/operator/max.ts +++ b/src/operator/max.ts @@ -13,9 +13,9 @@ import { ReduceOperator } from './reduce'; * @method max * @owner Observable */ -export function max(comparer?: (x: T, y: T) => T): Observable { - const max: typeof comparer = (typeof comparer === 'function') - ? comparer +export function max(comparer?: (x: T, y: T) => number): Observable { + const max: (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(max)); }