Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

min.ts 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { reduce } from './reduce';
  2. import { MonoTypeOperatorFunction } from '../types';
  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. * ![](min.png)
  8. *
  9. * ## Examples
  10. * Get the minimal value of a series of numbers
  11. * ```ts
  12. * import { of } from 'rxjs';
  13. * import { min } from 'rxjs/operators';
  14. *
  15. * of(5, 4, 7, 2, 8).pipe(
  16. * min(),
  17. * )
  18. * .subscribe(x => console.log(x)); // -> 2
  19. * ```
  20. *
  21. * Use a comparer function to get the minimal item
  22. * ```typescript
  23. * import { of } from 'rxjs';
  24. * import { min } from 'rxjs/operators';
  25. *
  26. * interface Person {
  27. * age: number,
  28. * name: string
  29. * }
  30. * of<Person>(
  31. * {age: 7, name: 'Foo'},
  32. * {age: 5, name: 'Bar'},
  33. * {age: 9, name: 'Beer'},
  34. * ).pipe(
  35. * min<Person>( (a: Person, b: Person) => a.age < b.age ? -1 : 1),
  36. * )
  37. * .subscribe((x: Person) => console.log(x.name)); // -> 'Bar'
  38. * ```
  39. * @see {@link max}
  40. *
  41. * @param {Function} [comparer] - Optional comparer function that it will use instead of its default to compare the
  42. * value of two items.
  43. * @return {Observable<R>} An Observable that emits item with the smallest value.
  44. * @method min
  45. * @owner Observable
  46. */
  47. export function min<T>(comparer?: (x: T, y: T) => number): MonoTypeOperatorFunction<T> {
  48. const min: (x: T, y: T) => T = (typeof comparer === 'function')
  49. ? (x, y) => comparer(x, y) < 0 ? x : y
  50. : (x, y) => x < y ? x : y;
  51. return reduce(min);
  52. }