-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Handle weekday strings in rangebreaks #4661
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a03d64d
Handle day of week str - add auto dflt for pattern
archmoj d52a803
add tests to handle weekday strings
archmoj 05ab23c
mention matching logic in description
archmoj db2c3a3
attempt improve performance (a bit) by using object lookup, early ret…
archmoj c48f551
no need to cast string to string
archmoj 550a375
improve matching - only need to test first two bounds - avoid touchin…
archmoj e481a13
validate numberic bounds when using patterns
archmoj 84c2a6f
simplify default logic
archmoj 9a0416a
correct axes default logic
archmoj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
|
||
'use strict'; | ||
|
||
var isNumeric = require('fast-isnumeric'); | ||
|
||
var Registry = require('../../registry'); | ||
var Lib = require('../../lib'); | ||
|
||
|
@@ -21,6 +23,9 @@ 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; | ||
var HOUR = require('./constants').HOUR_PATTERN; | ||
|
||
/** | ||
* options: object containing: | ||
* | ||
|
@@ -155,10 +160,55 @@ function rangebreaksDefaults(itemIn, itemOut, containerOut) { | |
|
||
if(enabled) { | ||
var bnds = coerce('bounds'); | ||
|
||
if(bnds && bnds.length >= 2) { | ||
if(bnds.length > 2) { | ||
itemOut.bounds = itemOut.bounds.slice(0, 2); | ||
var dfltPattern = ''; | ||
var i, q; | ||
if(bnds.length === 2) { | ||
for(i = 0; i < 2; i++) { | ||
q = indexOfDay(bnds[i]); | ||
if(q) { | ||
dfltPattern = DAY_OF_WEEK; | ||
break; | ||
} | ||
} | ||
} | ||
var pattern = coerce('pattern', dfltPattern); | ||
if(pattern === DAY_OF_WEEK) { | ||
for(i = 0; i < 2; i++) { | ||
q = indexOfDay(bnds[i]); | ||
if(q) { | ||
// convert to integers i.e 'Sunday' --> 0 | ||
itemOut.bounds[i] = bnds[i] = q - 1; | ||
} | ||
} | ||
} | ||
if(pattern) { | ||
// ensure types and ranges | ||
for(i = 0; i < 2; i++) { | ||
q = bnds[i]; | ||
switch(pattern) { | ||
case DAY_OF_WEEK : | ||
if(isNumeric(q)) q = Math.floor(q); | ||
|
||
q = Math.floor(+q); | ||
if(!(q >= 0 && q < 7)) { | ||
itemOut.enabled = false; | ||
return; | ||
} | ||
// use int [0, 7) | ||
itemOut.bounds[i] = bnds[i] = q; | ||
break; | ||
|
||
case HOUR : | ||
if(!(q >= 0 && q <= 24)) { // accept 24 | ||
itemOut.enabled = false; | ||
return; | ||
} | ||
// use float [0, 24] | ||
itemOut.bounds[i] = bnds[i] = q; | ||
break; | ||
} | ||
alexcjohnson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
if(containerOut.autorange === false) { | ||
|
@@ -175,8 +225,6 @@ function rangebreaksDefaults(itemIn, itemOut, containerOut) { | |
return; | ||
} | ||
} | ||
|
||
coerce('pattern'); | ||
} else { | ||
var values = coerce('values'); | ||
|
||
|
@@ -189,3 +237,21 @@ function rangebreaksDefaults(itemIn, itemOut, containerOut) { | |
} | ||
} | ||
} | ||
|
||
// these numbers are one more than what bounds would be mapped to | ||
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. clever way to avoid having to distinguish 0 from undefined :) |
||
var dayStrToNum = { | ||
sun: 1, | ||
mon: 2, | ||
tue: 3, | ||
wed: 4, | ||
thu: 5, | ||
fri: 6, | ||
sat: 7 | ||
}; | ||
|
||
function indexOfDay(v) { | ||
if(typeof v !== 'string') return; | ||
return dayStrToNum[ | ||
v.substr(0, 3).toLowerCase() | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
redundant - and dangerous -
q=''
would be turned into0
for example. We should only cast to number when the value has already passedisNumeric
(but might be a numeric string).Also I don't think we should
floor
, we should just discard values that aren't integers (ieenabled=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?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. Fixed in 9a0416a.