-
Notifications
You must be signed in to change notification settings - Fork 48
fix: simplify null and undefined checks in get function #426
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
base: main
Are you sure you want to change the base?
Conversation
Benchmark Results
Performance regressions of 30% or more should be investigated, unless they were anticipated. Smaller regressions may be due to normal variability, as we don't use dedicated CI infrastructure. |
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 agree with this change. It makes get
behave similarly to a ?? b
.
This is a behavior change, so a test needs to be updated or added such that it will fail when using the previous implementation. This behavior should also be documented.
- Updated get() to return defaultValue when the resolved value is null, matching behavior for undefined. - Simplified check to use `current == null` for both null and undefined. - Added unit tests to cover: * Returning defaultValue when final value is null * Preserving falsy but non-nullish values (0, '', false) - Updated documentation to note null handling and show examples.
On second thought, maybe Can't you just do this? _.get(obj, key) ?? defaultValue |
I think we should keep the defaultValue parameter for consistency with other Radashi utilities, better readability in chained calls, and to avoid extra wrapping with ??. This change focuses on improving nullish handling without removing that convenience. |
Summary
This PR fixes the
get
function to properly handlenull
values by returning the default value when the final resolved value isnull
. Previously, the function only checked forundefined
at the end, allowingnull
values to be returned even when a default value was provided.Changes:
undefined
andnull
valuesnull
andundefined
trigger default value fallbackThis improves the robustness of the utility function and provides more predictable behavior when dealing with nullable data structures.
Related issue, if any:
For any code change,
Does this PR introduce a breaking change?
No
This change maintains backward compatibility. The only behavioral change is that
null
values at the end of a path resolution will now return the default value instead ofnull
, which aligns with the expected behavior of most developers using this utility function.Bundle impact
src/object/get.ts
Footnotes
Function size includes the
import
dependencies of the function. ↩