source: trip-planner-front/node_modules/rxjs/src/internal/operators/max.ts@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.5 KB
Line 
1import { reduce } from './reduce';
2import { MonoTypeOperatorFunction } from '../types';
3
4/**
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.
7 *
8 * ![](max.png)
9 *
10 * ## Examples
11 * Get the maximal value of a series of numbers
12 * ```ts
13 * import { of } from 'rxjs';
14 * import { max } from 'rxjs/operators';
15 *
16 * of(5, 4, 7, 2, 8).pipe(
17 * max(),
18 * )
19 * .subscribe(x => console.log(x)); // -> 8
20 * ```
21 *
22 * Use a comparer function to get the maximal item
23 * ```typescript
24 * import { of } from 'rxjs';
25 * import { max } from 'rxjs/operators';
26 *
27 * interface Person {
28 * age: number,
29 * name: string
30 * }
31 * of<Person>(
32 * {age: 7, name: 'Foo'},
33 * {age: 5, name: 'Bar'},
34 * {age: 9, name: 'Beer'},
35 * ).pipe(
36 * max<Person>((a: Person, b: Person) => a.age < b.age ? -1 : 1),
37 * )
38 * .subscribe((x: Person) => console.log(x.name)); // -> 'Beer'
39 * ```
40 *
41 * @see {@link min}
42 *
43 * @param {Function} [comparer] - Optional comparer function that it will use instead of its default to compare the
44 * value of two items.
45 * @return {Observable} An Observable that emits item with the largest value.
46 * @method max
47 * @owner Observable
48 */
49export 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;
53
54 return reduce(max);
55}
Note: See TracBrowser for help on using the repository browser.