-
Notifications
You must be signed in to change notification settings - Fork 175
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 isNullish
, isNonNullish
and isKeyOf
Utility Functions
#385
base: master
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
SummaryThis pull request introduces a new utility function, MotivationCurrent JavaScript operations like Implementation
For example, using enum Values {
A = 'a',
B = 'b',
}
const result = isKeyOf(Values, 'A'); // result is true, with 'A' being recognized as a valid key of `Values`. Use Cases
TestsComprehensive tests have been added to verify the functionality of Changes Made
How to Test
Potential ImpactThis addition is non-breaking and complements the existing suite of utility functions in Radash. It introduces a type-safe way to check for the presence of keys in objects, specifically designed to leverage TypeScript's capabilities. This function aligns with Radash's goal of providing practical, straightforward utilities that enhance developer productivity and code safety. I'm excited to see how |
isNullish
and isNonNullish
Utility FunctionsisNullish
, isNonNullish
and isKeyOf
Utility Functions
* Checks if the given value is null or undefined. | ||
*/ | ||
export const isNullish = (value: any): value is null | undefined => { | ||
return value === null || value === undefined |
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.
Maybe not having this is by-design? Why not value == null
or value != null
instead?
value: string | number | symbol, | ||
obj: TType | ||
): value is keyof TType => { | ||
return value in obj |
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.
The goal with Radash is to provide the powerful functions you need, not the ones the runtimes now provide
This feels like it goes against the library's philosophy. You can easily do value in obj
yourself or use Object.hasOwn
(which are different)
Also, I half-expect this to match Object.hasOwn
, that is, isKeyOf({}, "toString")
to be false
Summary
This pull request introduces two new utility functions,
isNullish
andisNonNullish
, to the Radash library. These functions are intended to provide a more intuitive and expressive way to handle nullish values in JavaScript, complementing the existing utility functions in Radash.Motivation
In JavaScript, handling nullish values (
null
andundefined
) can often be verbose and error-prone, especially when dealing with deep object structures or complex logic. TheisNullish
function provides a clear, concise way to check for nullish values, whileisNonNullish
allows for the inverse, checking for values that are not nullish. These additions aim to improve developer experience and code readability, aligning with Radash's goal of offering practical and straightforward utility functions.Details
isNullish(value: any): boolean
: Returnstrue
if the value isnull
orundefined
, otherwise returnsfalse
.isNonNullish(value: any): boolean
: Returnstrue
if the value is notnull
and notundefined
, otherwise returnsfalse
.Implementation
The implementation of these functions is straightforward, relying on strict equality checks to determine if a value is nullish or not. This approach ensures maximum performance and minimal overhead, in line with the efficiency goals of Radash.
Use Cases
These functions can be particularly useful in scenarios where distinguishing between "no value" and falsy values like
0
,''
, orfalse
is important. For example, in configuration objects, function parameters, or API response parsing, where a nullish value might signify the absence of a meaningful value.Tests
Comprehensive unit tests have been added to ensure the reliability and correctness of these functions under various scenarios.
Changes Made
isNullish
function along with its unit tests.isNonNullish
function along with its unit tests.How to Test
npm install
to install any new dependencies (if any).npm run test
to execute the unit tests forisNullish
andisNonNullish
. All tests should pass.Potential Impact
These changes are additive and fully backward compatible. They introduce no breaking changes to existing functionality in Radash.
I believe these additions will make working with nullish values in JavaScript more intuitive and expressive for Radash users. I look forward to your feedback and any suggestions for improvement!