Handle weekday strings in rangebreaks#4661
Conversation
src/plots/cartesian/axis_defaults.js
Outdated
| @@ -161,6 +163,27 @@ function rangebreaksDefaults(itemIn, itemOut, containerOut) { | |||
| itemOut.bounds = itemOut.bounds.slice(0, 2); | |||
There was a problem hiding this comment.
I don't think you have to do this slice(0, 2) - coerce for info_array already does this.
But if somehow I'm mistaken about that, you'd need to also shorten bnds so you don't lengthen itemOut.bounds again in the loops below.
There was a problem hiding this comment.
I don't see any place where itemOut.bounds.length is changed.
There was a problem hiding this comment.
itemOut.bounds[i] = bnds[i] = q;
There was a problem hiding this comment.
Good call. I revised the new logic in 550a375.
There was a problem hiding this comment.
Again, you don't need to worry about the length of bnds at all - coerce handles that so bnds (and itemOut.bounds) is either undefined (since it has no default value) or it's an array of length 2 - or less, I just tested this, if you pass in a shorter array coerce isn't going to pad it since the inner values don't have defaults either, but that's OK, it'll just look like undefined for missing values and get filtered out in the type checks.
Can you please take all these length checks out? We put a lot of effort into making coerce do the right thing so the individual supplyDefaults functions could be simple and readable.
src/plots/cartesian/axis_defaults.js
Outdated
|
|
||
| function indexOfDay(v) { | ||
| var str = String(v).substr(0, 3).toLowerCase(); | ||
| return weekSTR.indexOf(str); |
There was a problem hiding this comment.
This could be sped up a bit with an object lookup rather than indexOf
var dayStrToNum = {
sun: 0,
mon: 1,
...
}
return dayStrToNum[str];There was a problem hiding this comment.
and possibly bail out early if v isn't already a string, rather than converting to a string only to discard it later.
There was a problem hiding this comment.
Good calls! Done in db2c3a3.
Curious to know if there is a good article on this?
At the moment, there are quite a number of places that indexOf !== -1 is applied, so please feel free to open an issue for that.
Thanks.
There was a problem hiding this comment.
Not sure about an article... you find a lot of people saying "hash lookup is O(1) but indexOf is O(n)" but that's as far as it usually goes.
Here's a jsperf showing the general trends: https://jsperf.com/array-indexof-vs-object-key-lookup2/12 - basically, object lookups hardly scale with the number of keys but indexOf scales linearly. So indexOf can be a nice way to simplify if (a===b || a===c || a===d) -> if ([b,c,d].indexOf(a) !== -1) - but more than a few items and it's better to turn the problem into an object lookup.
Also of course object lookups only support strings... looking forward to when we stop supporting IE and can use Set!
…urn and non-strict condition checks
| } | ||
| } | ||
|
|
||
| // these numbers are one more than what bounds would be mapped to |
There was a problem hiding this comment.
clever way to avoid having to distinguish 0 from undefined :)
Co-Authored-By: alexcjohnson <johnson.alex.c@gmail.com>
|
I <3 this!! cc @chriddyp |
src/plots/cartesian/axis_defaults.js
Outdated
| case DAY_OF_WEEK : | ||
| if(isNumeric(q)) q = Math.floor(q); | ||
|
|
||
| q = Math.floor(+q); |
There was a problem hiding this comment.
redundant - and dangerous - q='' would be turned into 0 for example. We should only cast to number when the value has already passed isNumeric (but might be a numeric string).
Also I don't think we should floor, we should just discard values that aren't integers (ie enabled=false). For one thing, that way if we ever add support for fractional days it won't break any existing behavior.
Can't this numeric validation be pushed into the if(pattern === DAY_OF_WEEK) loop above, and only executed if we didn't already convert from a day name to integer?
alexcjohnson
left a comment
There was a problem hiding this comment.
Alright, I think we've got it! 💃

Follow up of #4614 and #4655
boundse.g.Monday(or 'mon`) to day of week indices between [0, 6].day of weekwhen using day strings e.g.Mondayor 'mon`.Demo.
@plotly/plotly_js