-
Notifications
You must be signed in to change notification settings - Fork 621
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
Add mapNotNullish
to collections
#1103
Conversation
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.
@LionC LGTM.
As explained in the description, this is optimized version of .map().filter()
idiom. I think this has decent use cases, especially when the source array is large.
* | ||
* Example: | ||
* | ||
* ```typescript |
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.
We prefer ts
tag, but let's address this 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.
Won't be typechecked, but I'll address that upstream in cli.
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.
Oh sorry - I keep forgetting that. Should I post a separate PR?
This adds
mapNotNullish<T, O>(array: Array<T>, transformer: (el: T) => O): Array<NonNullable<O>>
tocollections
. This method helps in common cases like extracting an optional property from objects ormap
ping andfilter
ing an array at the same time:Example 1:
Example 2:
mapNotNullish(someArray, someFunction)
has the same result asbut is more efficient regarding runtime and memory (only one iteration, no intermediate array etc), states the intent shorter and more specifically and gets the types right (which
filter
does not without explicit type guards).Like a lot of the
collections
functions,mapNotNullish()
is heavily inspired bykotlin
s.mapNotNull()
.