min.js 1.4 KB

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