81521652d1d533434abdf7df02348ccdc0a145c4664bfb5c5181db63d6fb88f534663453d7db612b9c62518815ae2e48799f7cfc2d7872c0fb8e052afab225 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * This function takes one parameter and just returns it. Simply put,
  3. * this is like `<T>(x: T): T => x`.
  4. *
  5. * ## Examples
  6. *
  7. * This is useful in some cases when using things like `mergeMap`
  8. *
  9. * ```ts
  10. * import { interval, take, map, range, mergeMap, identity } from 'rxjs';
  11. *
  12. * const source$ = interval(1000).pipe(take(5));
  13. *
  14. * const result$ = source$.pipe(
  15. * map(i => range(i)),
  16. * mergeMap(identity) // same as mergeMap(x => x)
  17. * );
  18. *
  19. * result$.subscribe({
  20. * next: console.log
  21. * });
  22. * ```
  23. *
  24. * Or when you want to selectively apply an operator
  25. *
  26. * ```ts
  27. * import { interval, take, identity } from 'rxjs';
  28. *
  29. * const shouldLimit = () => Math.random() < 0.5;
  30. *
  31. * const source$ = interval(1000);
  32. *
  33. * const result$ = source$.pipe(shouldLimit() ? take(5) : identity);
  34. *
  35. * result$.subscribe({
  36. * next: console.log
  37. * });
  38. * ```
  39. *
  40. * @param x Any value that is returned by this function
  41. * @returns The value passed as the first parameter to this function
  42. */
  43. export function identity<T>(x: T): T {
  44. return x;
  45. }