max.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. var reduce_1 = require('./reduce');
  3. /**
  4. * The Max operator operates on an Observable that emits numbers (or items that can be compared with a provided function),
  5. * and when source Observable completes it emits a single item: the item with the largest value.
  6. *
  7. * <img src="./img/max.png" width="100%">
  8. *
  9. * @example <caption>Get the maximal value of a series of numbers</caption>
  10. * Rx.Observable.of(5, 4, 7, 2, 8)
  11. * .max()
  12. * .subscribe(x => console.log(x)); // -> 8
  13. *
  14. * @example <caption>Use a comparer function to get the maximal item</caption>
  15. * interface Person {
  16. * age: number,
  17. * name: string
  18. * }
  19. * Observable.of<Person>({age: 7, name: 'Foo'},
  20. * {age: 5, name: 'Bar'},
  21. * {age: 9, name: 'Beer'})
  22. * .max<Person>((a: Person, b: Person) => a.age < b.age ? -1 : 1)
  23. * .subscribe((x: Person) => console.log(x.name)); // -> 'Beer'
  24. * }
  25. *
  26. * @see {@link min}
  27. *
  28. * @param {Function} [comparer] - Optional comparer function that it will use instead of its default to compare the
  29. * value of two items.
  30. * @return {Observable} An Observable that emits item with the largest value.
  31. * @method max
  32. * @owner Observable
  33. */
  34. function max(comparer) {
  35. var max = (typeof comparer === 'function')
  36. ? function (x, y) { return comparer(x, y) > 0 ? x : y; }
  37. : function (x, y) { return x > y ? x : y; };
  38. return reduce_1.reduce(max);
  39. }
  40. exports.max = max;
  41. //# sourceMappingURL=max.js.map