min.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. var reduce_1 = require('./reduce');
  3. /**
  4. * The Min 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 smallest value.
  6. *
  7. * <img src="./img/min.png" width="100%">
  8. *
  9. * @example <caption>Get the minimal value of a series of numbers</caption>
  10. * Rx.Observable.of(5, 4, 7, 2, 8)
  11. * .min()
  12. * .subscribe(x => console.log(x)); // -> 2
  13. *
  14. * @example <caption>Use a comparer function to get the minimal 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. * .min<Person>( (a: Person, b: Person) => a.age < b.age ? -1 : 1)
  23. * .subscribe((x: Person) => console.log(x.name)); // -> 'Bar'
  24. * }
  25. *
  26. * @see {@link max}
  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<R>} An Observable that emits item with the smallest value.
  31. * @method min
  32. * @owner Observable
  33. */
  34. function min(comparer) {
  35. var min = (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(min);
  39. }
  40. exports.min = min;
  41. //# sourceMappingURL=min.js.map