fix: simplify null and undefined checks in get function - #426
fix: simplify null and undefined checks in get function#426OgabekYuldoshev wants to merge 4 commits into
Conversation
aleclarson
left a comment
There was a problem hiding this comment.
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. |
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. |
Summary
This PR fixes the
getfunction to properly handlenullvalues by returning the default value when the final resolved value isnull. Previously, the function only checked forundefinedat the end, allowingnullvalues to be returned even when a default value was provided.Changes:
undefinedandnullvaluesnullandundefinedtrigger 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
nullvalues 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.tsFootnotes
Function size includes the
importdependencies of the function. ↩