Skip to content
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

set command mishandles bool values #75

Closed
pabigot opened this issue Jul 2, 2020 · 3 comments · Fixed by #77
Closed

set command mishandles bool values #75

pabigot opened this issue Jul 2, 2020 · 3 comments · Fixed by #77
Labels
bug Something isn't working

Comments

@pabigot
Copy link
Contributor

pabigot commented Jul 2, 2020

When --set false reaches

if (!Number.isNaN(options.set)) {
Number.isNaN("false") evaluates to false, so the conditional passes and the option set property becomes the result of Number.parseInt("false", 10), which is NaN, which has no effect when the device receives it.

A fix is to check for the acceptable string values first, then fall back to Number.parseInt.

@pabigot
Copy link
Contributor Author

pabigot commented Jul 2, 2020

Also that code should be using isNaN, not Number.isNaN which does something different.

@codetheweb
Copy link
Member

I believe what we should be doing is Number.isNaN(Number.parseInt('false', 10)). That would evaluate correctly.

If that sounds good, feel free to make a PR. Otherwise I can push a fix at some point.

@codetheweb codetheweb added the bug Something isn't working label Jul 4, 2020
@pabigot
Copy link
Contributor Author

pabigot commented Jul 4, 2020

No, that's not right either, unless I misunderstand. Number.parseInt('12ac', 10) would return 12 so the wrong value would be passed without any warning.

Using isNaN instead of Number.isNaN would resolve that problem, but would still do the wrong thing for something like 1000 which could be a decimal number, or could be a 4-character hex string. OTOH in that case at least it's clear there's ambiguity, so one can say --raw-value should have been used to specify that the value should remain a string.

The two solutions that work are the original !isNaN(options.set) and !Number.isNaN(+options.set), but both are rejected by some style checker thing on commit.

 lib/control.js:1
 ✖  69:21  use Number(options.set) instead.              no-implicit-coercion   // this for !Number.isNaN(+options.set)
 ✖  69:8  Prefer Number.isNaN() over isNaN().  unicorn/prefer-number-properties  // this for isNaN(options.set)

#77 is the best I can do given the style checker complaints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants