Skip to content

Commit

Permalink
fix(max): do not return comparer values
Browse files Browse the repository at this point in the history
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 ReactiveX#1892
  • Loading branch information
gluck committed Sep 7, 2016
1 parent af4e6a9 commit fb3a327
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
23 changes: 5 additions & 18 deletions spec/operators/max-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,42 +184,29 @@ 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((<any>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((<any>e1).max(predicate)).toBe(expected, { w: -1 });
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).max(predicate)).toBe(expected, { w: '4' });
expectObservable((<any>e1).max(predicate)).toBe(expected, { w: 'b' });
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

Expand Down
6 changes: 3 additions & 3 deletions src/operator/max.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { ReduceOperator } from './reduce';
* @method max
* @owner Observable
*/
export function max<T>(comparer?: (x: T, y: T) => T): Observable<T> {
const max: typeof comparer = (typeof comparer === 'function')
? comparer
export function max<T>(comparer?: (x: T, y: T) => number): Observable<T> {
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));
}
Expand Down

0 comments on commit fb3a327

Please sign in to comment.