-
Notifications
You must be signed in to change notification settings - Fork 1
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
Array remove nullish #73
Conversation
src/utils/arrayUtils.ts
Outdated
/** | ||
* Return a copy of the given array without null or undefined values | ||
*/ | ||
export function removeNullish<T>(array: readonly (T | null | undefined)[]): T[] { | ||
return array.filter((e) => e !== undefined && e !== null) as T[] | ||
} | ||
|
||
/** | ||
* Return a copy of the given array without falsy values (eg: false, 0, '', null, undefined) | ||
*/ | ||
export function removeFalsy<T>(array: readonly (T | null | undefined)[]): T[] { | ||
return array.filter((e) => e) as T[] | ||
} |
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.
Finally, I added both because I can think of use cases where both can be useful, but feel free to disagree
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 think that's a great call!
Co-authored-by: Igor Savin <iselwin@gmail.com>
Co-authored-by: Igor Savin <iselwin@gmail.com>
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.
great job!
Changes
Adding
removeNullish
andremoveFalsy
to facilitate array cleaningChecklist
major
,minor
,patch
orskip-release