New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(identity): add identity docs #6750
Merged
benlesh
merged 2 commits into
ReactiveX:master
from
jakovljevic-mladen:add_identity_docs
Jan 12, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,45 @@ | ||
/** | ||
* This function takes one parameter and just returns it. Simply put, | ||
* this is like `<T>(x: T): T => x`. | ||
* | ||
* ## Examples | ||
* | ||
* This is useful in some cases when using things like `mergeMap` | ||
* | ||
* ```ts | ||
* import { interval, take, map, range, mergeMap, identity } from 'rxjs'; | ||
* | ||
* const source$ = interval(1000).pipe(take(5)); | ||
* | ||
* const result$ = source$.pipe( | ||
* map(i => range(i)), | ||
* mergeMap(identity) // same as mergeMap(x => x) | ||
* ); | ||
* | ||
* result$.subscribe({ | ||
* next: console.log | ||
* }); | ||
* ``` | ||
* | ||
* Or when you want to selectively apply an operator | ||
* | ||
* ```ts | ||
* import { interval, take, identity } from 'rxjs'; | ||
* | ||
* const shouldLimit = () => Math.random() < 0.5; | ||
* | ||
* const source$ = interval(1000); | ||
* | ||
* const result$ = source$.pipe(shouldLimit() ? take(5) : identity); | ||
* | ||
* result$.subscribe({ | ||
* next: console.log | ||
* }); | ||
* ``` | ||
* | ||
* @param x Any value that is returned by this function | ||
* @returns The value passed as the first parameter to this function | ||
*/ | ||
export function identity<T>(x: T): T { | ||
return x; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One of the main use cases I personally have for this is with a ternary to selectively apply an operator:
It would be nice to have an example of that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added (though, as the second example). If you'd like this example to be the first one, I can swap them.