-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix number named frames #1236
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
Fix number named frames #1236
Conversation
cc: @Kully re: problems with number-named frames This PR should hopefully fix the issues. It will always be slightly sketchy since it looks up frames by name and exact numeric equality is only a good idea with integers (JS sorta doesn't distinguish number types), but it is what it is. It will issue a warning if See: example: https://rreusser.github.io/demos/plotly-unsupported/numbered-frames-bugfix.html for an example that mixes strings and numbers with components. |
'behavior since all plotly.js frame names are stored internally ' + | ||
'as strings.'); | ||
|
||
if(numericNameWarningCount > 5) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice touch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I published a module once where I said "meh, probably won't be too many warnings." Haha, WRONG.
it('treats numeric frame names as strings', function() { | ||
var result = Plots.computeAPICommandBindings(gd, 'animate', [[8]]); | ||
|
||
expect(result).toEqual([{type: 'layout', prop: '_currentFrame', value: '8'}]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nicely done
}).catch(fail).then(done); | ||
}); | ||
|
||
it('updates frames referenced by number', function(done) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sweet 🎉
// Since it's sometimes necessary to do deep digging into frame data, | ||
// we'll consider it not 100% impossible for nulls or numbers to sneak through, | ||
// so check when casting the name, just to be absolutely certain: | ||
var stringName = newFrame.name ? newFrame.name.toString() : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha. 'string'.toString()
is a thing. How useful!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
flushing all falsy names to null
is a nice addition maintenance-wise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could maybe have
var nameIn = (newFrame.name === 'number') ? newFrame.name.toPrecision(1) : newFrame.name;
var stringName = nameIn ? nameIn.toString() : null;
to make sure that we only cast integer to string.
This is probably an overkill though. @rreusser I'll let decide what's best.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have strong feelings. I feel like you kinda get what you deserve if you're going to be dealing in floating point lookups, so I think maybe it's best to let the chips fall where they may and a different fixed point cuttoff is somewhat arbitrary unless we get fancy with a sprintf library or something.
And yeah, one subtlety: String(name)
is definitely not the same as name.toString()
:
Which is why the extra gymnastics to ensure nothing is falsey.
💃 looks good to me. |
This PR adds some casting and fixes some checks to ensure that frames with number names are handled correctly. Technically it could break compatibility with current plots, but only in the sense that the current behavior is broken. This seems like an extremely common problem based on what I've observed.