Skip to content

Fix number named frames - #1236

Merged
rreusser merged 4 commits into
masterfrom
fix-number-named-frames
Dec 7, 2016
Merged

rreusser merged 4 commits into
masterfrom
fix-number-named-frames

Conversation

@rreusser

@rreusser rreusser commented Dec 7, 2016

Copy link
Copy Markdown
Contributor

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

rreusser commented Dec 7, 2016 •

Copy link
Copy Markdown
Contributor Author

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.

Comment thread src/plot_api/plot_api.js
'behavior since all plotly.js frame names are stored internally ' +
'as strings.');

if(numericNameWarningCount > 5) {

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Comment thread src/plot_api/plot_api.js
// 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@rreusser rreusser Dec 7, 2016 •

Copy link
Copy Markdown
Contributor Author

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

etpinard commented Dec 7, 2016

Copy link
Copy Markdown
Contributor

💃 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.

2 participants