fix: prevent falsy values from being coerced to null#6984
Conversation
WalkthroughFixed a bug in user preferences where the logical OR operator ( Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 6✅ Passed checks (6 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. No actionable comments were generated in the recent review. 🎉 📜 Recent review detailsConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
🔇 Additional comments (3)
✏️ Tip: You can disable this entire section by setting Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Proposed changes
The
getString(),getBool(), andgetNumber()methods inUserPreferencescurrently use the||operator to returnnullwhen a key is missing.This unintentionally coerces valid falsy values into
null:false || null → null0 || null → null"" || null → nullAs a result, legitimate stored preferences (e.g., disabled toggles, zero counters, or empty strings) are read back as
nullinstead of their actual values.This PR replaces the
|| nullpattern with an explicitundefinedcheck, ensuring:nullfalse,0,"") are preserved correctlyThe change is minimal, safe, and non-breaking.
Issue(s)
Closes #6983
How to test or reproduce
console.loginsidegetBool()to inspect the returned valueBefore fix:
getBool()returnsnullinstead offalse.After fix:
getBool()correctly returnsfalse.The same issue can be reproduced for:
getNumber()when a stored value is0getString()when a stored value is""Screenshots
N/A — logic-only change with no UI impact.
Types of changes
Checklist
Further comments
All three getter methods shared the same
|| nullpattern, so the fix applies consistently across them.While some current callers use
??fallbacks that mask this issue, the underlying behavior is still incorrect and could affect future usages where no fallback is applied.This update ensures correct handling of falsy preference values and aligns the implementation with the expected behavior of MMKV getters (
undefinedmeaning “key not found”).Summary by CodeRabbit