Skip to content

Remove unnecessary conditionals and fix falsy property check in imperial conversion#4135

Merged
khassel merged 3 commits into
MagicMirrorOrg:developfrom
KristjanESPERANTO:conditionals
May 3, 2026
Merged

Remove unnecessary conditionals and fix falsy property check in imperial conversion#4135
khassel merged 3 commits into
MagicMirrorOrg:developfrom
KristjanESPERANTO:conditionals

Conversation

@KristjanESPERANTO
Copy link
Copy Markdown
Collaborator

@KristjanESPERANTO KristjanESPERANTO commented May 3, 2026

While removing unnecessary conditionals (for compliments reported in https://github.com/MagicMirrorOrg/MagicMirror/security/code-scanning/27 and for weather reported in https://github.com/MagicMirrorOrg/MagicMirror/security/code-scanning/28), I noticed a bug in the imperial conversion path: falsy property checks like if (imperialWeatherObject.temperature) silently skip the conversion when the value is 0. So at exactly 0 °C, the result would show 0 °F instead of the correct 32 °F.

I wrote unit tests for convertWeatherObjectToImperial first, which confirmed the bug, then fixed it by replacing the falsy checks with the in operator. Which I also find more intuitive to read.

Weather APIs deliver floating point values, so hitting exactly 0.0 might be rare in practice - more likely 0.1 or -0.1, which are truthy and convert fine. That should explain why it went unnoticed by users.

Falsy checks (e.g. 'if (obj.temperature)') incorrectly skip conversion
when the value is 0, which is a valid temperature (0°C = 32°F).
Using the 'in' operator checks for property existence instead.
@khassel khassel merged commit 3335781 into MagicMirrorOrg:develop May 3, 2026
12 checks passed
@KristjanESPERANTO KristjanESPERANTO deleted the conditionals branch May 3, 2026 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants