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

Incorrect conversion of number to boolean #5209

Merged
merged 7 commits into from Jul 29, 2018
Merged

Conversation

teroman
Copy link
Contributor

@teroman teroman commented Jan 30, 2018

Check for min/max/stepSize coerces numbers to truthy values. A min setting of 0 is therefore interpreted as falsy, and not being set.

Checking against undefined means settings are correctly detected.

Check for min/max/stepSize coerces numbers to truthy values. A min setting of 0 is therefore interpreted as not being set.

Checking against undefined means settings are correctly detected.
benmccann
benmccann previously approved these changes Jan 30, 2018
@@ -25,7 +25,7 @@ function generateTicks(generationOptions, dataRange) {
var niceMax = Math.ceil(dataRange.max / spacing) * spacing;

// If min, max and stepSize is set and they make an evenly spaced scale use it.
if (generationOptions.min && generationOptions.max && generationOptions.stepSize) {
if (generationOptions.min !== undefined && generationOptions.max !== undefined && generationOptions.stepSize !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it wise to also check for NaN, infinite values, or values of other types. Perhaps if (!isNaN(generationOptions.min) && isFinite(generationOptions.min) ... )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that if we were going to check for those values we should probably log an error or throw an exception or somehow tell the user they've passed in invalid values rather than just silently ignoring the parameters. And maybe we should have a single method to validate user options called in the constructor or something rather than placing that code here

But in any case I feel it's quite unlikely for a user to set their options to Infinity, so I'm not sure I would require it before merging this PR. It would be a nice thing to do and I wouldn't object to validating user options, but this seems like a positive change even without that further improvement, so I don't want to block this PR on it necessarily

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least we could check for undefined and null since setting null is quite common to ignore an option:

if (!helpers.isNullOrUndef(generationOptions.min) &&
    !helpers.isNullOrUndef(generationOptions.max) &&
    !helpers.isNullOrUndef(generationOptions.stepSize)  {
    // ...
}

@simonbrunel
Copy link
Member

Is there any issue associated to this PR? Don't we want a unit test to prevent regression?

@benmccann
Copy link
Contributor

@teroman will you be able to update this PR to address the suggestions?

@teroman
Copy link
Contributor Author

teroman commented May 25, 2018

I've edited the file in line with the suggestion to check for nulls.

I have written a test in scale.linear.tests.js to check 0 is used as max setting.

benmccann
benmccann previously approved these changes May 25, 2018
@benmccann
Copy link
Contributor

@etimberg @simonbrunel this is a high-quality PR with a test. Should be a good candidate for merging. Can you take a look?

@benmccann
Copy link
Contributor

@etimberg @simonbrunel just a reminder that this PR is probably ready to merge

@simonbrunel
Copy link
Member

Ideally, we should also test min: 0 and stepSize: 0 (could be in the same test though).

@benmccann
Copy link
Contributor

@teroman would you be able to update? thanks!

benmccann
benmccann previously approved these changes Jun 19, 2018
simonbrunel
simonbrunel previously approved these changes Jul 7, 2018
@simonbrunel simonbrunel dismissed their stale review July 7, 2018 15:38

stepSize === 0 is not valid

@@ -25,7 +25,7 @@ function generateTicks(generationOptions, dataRange) {
var niceMax = Math.ceil(dataRange.max / spacing) * spacing;

// If min, max and stepSize is set and they make an evenly spaced scale use it.
if (generationOptions.min && generationOptions.max && generationOptions.stepSize) {
if (!helpers.isNullOrUndef(generationOptions.min) && !helpers.isNullOrUndef(generationOptions.max) && !helpers.isNullOrUndef(generationOptions.stepSize)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@teroman I just realized that we should not apply the same logic to stepSize because stepSize === 0 is invalid (we divide by stepSize line 30).

Should be:

if (!helpers.isNullOrUndef(generationOptions.min) && !helpers.isNullOrUndef(generationOptions.max) && generationOptions.stepSize) {

Good spot, I must have been on autopilot!
@simonbrunel simonbrunel merged commit 3522686 into chartjs:master Jul 29, 2018
@simonbrunel simonbrunel added this to the Version 2.8 milestone Jul 29, 2018
exwm pushed a commit to exwm/Chart.js that referenced this pull request Apr 30, 2021
When 0, the min and max options was considered not being set, instead we should check for null or undefined.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants