Skip to content

Commit f454e93

Browse files
committed
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
1 parent af4e6a9 commit f454e93

File tree

2 files changed

+9
-22
lines changed

2 files changed

+9
-22
lines changed

spec/operators/max-spec.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -184,42 +184,29 @@ describe('Observable.prototype.max', () => {
184184
expectSubscriptions(e1.subscriptions).toBe(e1subs);
185185
});
186186

187-
it('should handle a constant predicate on observable with many values', () => {
188-
const e1 = hot('-x-^-a-b-c-d-e-f-g-|');
189-
const e1subs = '^ !';
190-
const expected = '----------------(w|)';
191-
192-
const predicate = () => {
193-
return 42;
194-
};
195-
196-
expectObservable((<any>e1).max(predicate)).toBe(expected, { w: 42 });
197-
expectSubscriptions(e1.subscriptions).toBe(e1subs);
198-
});
199-
200-
it('should handle a predicate on observable with many values', () => {
187+
it('should handle a reverse predicate on observable with many values', () => {
201188
const e1 = hot('-a-^-b--c--d-|', { a: 42, b: -1, c: 0, d: 666 });
202189
const e1subs = '^ !';
203190
const expected = '----------(w|)';
204191

205192
const predicate = function (x, y) {
206-
return Math.min(x, y);
193+
return x > y ? -1 : 1;
207194
};
208195

209196
expectObservable((<any>e1).max(predicate)).toBe(expected, { w: -1 });
210197
expectSubscriptions(e1.subscriptions).toBe(e1subs);
211198
});
212199

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

218205
const predicate = function (x, y) {
219-
return x > y ? x : y;
206+
return x > y ? -1 : 1;
220207
};
221208

222-
expectObservable((<any>e1).max(predicate)).toBe(expected, { w: '4' });
209+
expectObservable((<any>e1).max(predicate)).toBe(expected, { w: 'b' });
223210
expectSubscriptions(e1.subscriptions).toBe(e1subs);
224211
});
225212

src/operator/max.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import { ReduceOperator } from './reduce';
1313
* @method max
1414
* @owner Observable
1515
*/
16-
export function max<T>(comparer?: (x: T, y: T) => T): Observable<T> {
17-
const max: typeof comparer = (typeof comparer === 'function')
18-
? comparer
16+
export function max<T>(comparer?: (x: T, y: T) => number): Observable<T> {
17+
const max: (x: T, y: T) => T = (typeof comparer === 'function')
18+
? (x, y) => comparer(x, y) > 0 ? x : y
1919
: (x, y) => x > y ? x : y;
2020
return this.lift(new ReduceOperator(max));
2121
}
2222

2323
export interface MaxSignature<T> {
24-
(comparer?: (x: T, y: T) => T): Observable<T>;
24+
(comparer?: (x: T, y: T) => number): Observable<T>;
2525
}

0 commit comments

Comments
 (0)