| 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 |
} |