Skip to content

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

Merged
merged 4 commits into from
Dec 7, 2016
Merged

Fix number named frames #1236

merged 4 commits into from
Dec 7, 2016

Conversation

rreusser
Copy link
Contributor

@rreusser rreusser commented Dec 7, 2016

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.

@rreusser rreusser added status: in progress bug something broken labels Dec 7, 2016
@rreusser
Copy link
Contributor Author

rreusser commented Dec 7, 2016

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 addFrames would overwrite with a number-named frame, but other than that it should just work as expected.

See:

example: https://rreusser.github.io/demos/plotly-unsupported/numbered-frames-bugfix.html
and source: https://github.com/rreusser/demos/blob/master/plotly-unsupported/src/numbered-frames-bugfix.js

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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nice touch.

Copy link
Contributor Author

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'}]);
Copy link
Contributor

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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

sweet 🎉

@etpinard etpinard added this to the v1.21.0 milestone Dec 7, 2016
// 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;
Copy link
Contributor

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!

Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

fun

image

Copy link
Contributor

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.

Copy link
Contributor Author

@rreusser rreusser Dec 7, 2016

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():

screen shot 2016-12-07 at 16 45 07

Which is why the extra gymnastics to ensure nothing is falsey.

@etpinard
Copy link
Contributor

etpinard commented Dec 7, 2016

💃 looks good to me.

@rreusser rreusser merged commit 8265df5 into master Dec 7, 2016
@rreusser rreusser deleted the fix-number-named-frames branch December 7, 2016 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants