1 import { reduce } from './reduce';
2 import { MonoTypeOperatorFunction } from '../types';
5 * The Max operator operates on an Observable that emits numbers (or items that can be compared with a provided function),
6 * and when source Observable completes it emits a single item: the item with the largest value.
11 * Get the maximal value of a series of numbers
13 * import { of } from 'rxjs';
14 * import { max } from 'rxjs/operators';
16 * of(5, 4, 7, 2, 8).pipe(
19 * .subscribe(x => console.log(x)); // -> 8
22 * Use a comparer function to get the maximal item
24 * import { of } from 'rxjs';
25 * import { max } from 'rxjs/operators';
32 * {age: 7, name: 'Foo'},
33 * {age: 5, name: 'Bar'},
34 * {age: 9, name: 'Beer'},
36 * max<Person>((a: Person, b: Person) => a.age < b.age ? -1 : 1),
38 * .subscribe((x: Person) => console.log(x.name)); // -> 'Beer'
43 * @param {Function} [comparer] - Optional comparer function that it will use instead of its default to compare the
45 * @return {Observable} An Observable that emits item with the largest value.
49 export function max<T>(comparer?: (x: T, y: T) => number): MonoTypeOperatorFunction<T> {
50 const max: (x: T, y: T) => T = (typeof comparer === 'function')
51 ? (x, y) => comparer(x, y) > 0 ? x : y
52 : (x, y) => x > y ? x : y;