-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Handle weekday strings in rangebreaks #4661
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
Changes from 3 commits
a03d64d
d52a803
05ab23c
db2c3a3
c48f551
550a375
e481a13
84c2a6f
9a0416a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ var handleCategoryOrderDefaults = require('./category_order_defaults'); | |
var handleLineGridDefaults = require('./line_grid_defaults'); | ||
var setConvert = require('./set_convert'); | ||
|
||
var DAY_OF_WEEK = require('./constants').WEEKDAY_PATTERN; | ||
|
||
/** | ||
* options: object containing: | ||
* | ||
|
@@ -161,6 +163,27 @@ function rangebreaksDefaults(itemIn, itemOut, containerOut) { | |
itemOut.bounds = itemOut.bounds.slice(0, 2); | ||
} | ||
|
||
var dfltPattern = ''; | ||
var i, q; | ||
for(i = 0; i < bnds.length; i++) { | ||
q = indexOfDay(bnds[i]); | ||
if(q !== -1) { | ||
dfltPattern = DAY_OF_WEEK; | ||
break; | ||
} | ||
} | ||
var pattern = coerce('pattern', dfltPattern); | ||
|
||
if(pattern === DAY_OF_WEEK) { | ||
for(i = 0; i < bnds.length; i++) { | ||
q = indexOfDay(bnds[i]); | ||
if(q !== -1) { | ||
// convert to integers i.e 'Monday' --> 1 | ||
itemOut.bounds[i] = bnds[i] = q; | ||
} | ||
alexcjohnson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
if(containerOut.autorange === false) { | ||
var rng = containerOut.range; | ||
|
||
|
@@ -175,8 +198,6 @@ function rangebreaksDefaults(itemIn, itemOut, containerOut) { | |
return; | ||
} | ||
} | ||
|
||
coerce('pattern'); | ||
} else { | ||
var values = coerce('values'); | ||
|
||
|
@@ -189,3 +210,18 @@ function rangebreaksDefaults(itemIn, itemOut, containerOut) { | |
} | ||
} | ||
} | ||
|
||
var weekSTR = [ | ||
'sun', | ||
'mon', | ||
'tue', | ||
'wed', | ||
'thu', | ||
'fri', | ||
'sat' | ||
]; | ||
|
||
function indexOfDay(v) { | ||
var str = String(v).substr(0, 3).toLowerCase(); | ||
return weekSTR.indexOf(str); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be sped up a bit with an object lookup rather than var dayStrToNum = {
sun: 0,
mon: 1,
...
}
return dayStrToNum[str]; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and possibly bail out early if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good calls! Done in db2c3a3. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 Also of course object lookups only support strings... looking forward to when we stop supporting IE and can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alexcjohnson thank you!! |
||
} |
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 think you have to do this
slice(0, 2)
-coerce
forinfo_array
already does this.But if somehow I'm mistaken about that, you'd need to also shorten
bnds
so you don't lengthenitemOut.bounds
again in the loops below.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 see any place where
itemOut.bounds.length
is changed.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.
itemOut.bounds[i] = bnds[i] = q;
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.
Good call. I revised the new logic in 550a375.
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.
Again, you don't need to worry about the length of
bnds
at all -coerce
handles that sobnds
(anditemOut.bounds
) is eitherundefined
(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 arraycoerce
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 individualsupplyDefaults
functions could be simple and readable.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.
Good call. Addressed in 84c2a6f.