-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2316 from alephdata/develop
Next Release
- Loading branch information
Showing
9 changed files
with
233 additions
and
96 deletions.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,98 @@ | ||
const DEFAULT_START_INTERVAL = 1950; | ||
const ES_SUFFIX = '||/y'; | ||
import moment from 'moment'; | ||
|
||
const formatDateQParam = (datetime) => { | ||
return `${new Date(datetime).getFullYear()}||/y` | ||
const DEFAULT_START_INTERVAL = '1950'; | ||
|
||
const formatDateQParam = (datetime, granularity) => { | ||
const date = moment.utc(datetime).format("YYYY-MM-DD") | ||
if (granularity === 'month') { | ||
return `${date}||/M` | ||
} else if (granularity === 'day') { | ||
return `${date}||/d` | ||
} | ||
return `${date}||/y` | ||
}; | ||
|
||
const cleanDateQParam = (value) => { | ||
return value.replace(ES_SUFFIX, ''); | ||
if (!value) { return; } | ||
const [date, suffix] = value.split('||/'); | ||
|
||
if (suffix === 'y') { | ||
return moment.utc(date).format('YYYY') | ||
} else if (suffix === 'M') { | ||
return moment.utc(date).format('YYYY-MM') | ||
} else { | ||
return date; | ||
} | ||
}; | ||
|
||
const timestampToYear = timestamp => { | ||
return new Date(timestamp).getFullYear(); | ||
const timestampToLabel = (timestamp, granularity, locale) => { | ||
const dateObj = new Date(timestamp) | ||
let label, tooltipLabel; | ||
|
||
if (granularity === 'month') { | ||
label = new Intl.DateTimeFormat(locale, { month: 'short' }).format(dateObj) | ||
tooltipLabel = new Intl.DateTimeFormat(locale, { month: 'short', year: 'numeric' }).format(dateObj) | ||
} else if (granularity === 'day') { | ||
label = dateObj.getDate() | ||
tooltipLabel = new Intl.DateTimeFormat(locale, { month: 'short', year: 'numeric', day: 'numeric' }).format(dateObj) | ||
} else { | ||
label = tooltipLabel = dateObj.getFullYear(); | ||
} | ||
|
||
return ({ label, tooltipLabel }) | ||
} | ||
|
||
const filterDateIntervals = ({ query, intervals, useDefaultBounds }) => { | ||
const defaultEndInterval = new Date().getFullYear(); | ||
const hasGtFilter = query.hasFilter('gte:dates'); | ||
const hasLtFilter = query.hasFilter('lte:dates'); | ||
const filterDateIntervals = ({ field, query, intervals, useDefaultBounds }) => { | ||
const defaultEndInterval = moment.utc().format('YYYY-MM-DD'); | ||
const hasGtFilter = query.hasFilter(`gte:${field}`); | ||
const hasLtFilter = query.hasFilter(`lte:${field}`); | ||
|
||
const gt = hasGtFilter | ||
? cleanDateQParam(query.getFilter('gte:dates')[0]) | ||
const gtRaw = hasGtFilter | ||
? cleanDateQParam(query.getFilter(`gte:${field}`)[0]) | ||
: (useDefaultBounds && DEFAULT_START_INTERVAL); | ||
|
||
const lt = hasLtFilter | ||
? cleanDateQParam(query.getFilter('lte:dates')[0]) | ||
const ltRaw = hasLtFilter | ||
? cleanDateQParam(query.getFilter(`lte:${field}`)[0]) | ||
: (useDefaultBounds && defaultEndInterval); | ||
|
||
let gtOutOfRange, ltOutOfRange = false; | ||
const gt = gtRaw && moment(gtRaw) | ||
const lt = ltRaw && moment(ltRaw) | ||
|
||
let gtOutOfRange, ltOutOfRange = false; | ||
const filteredIntervals = intervals.filter(({ id }) => { | ||
const year = timestampToYear(id); | ||
if (gt && year < gt) { | ||
if (gt && gt.isAfter(id)) { | ||
gtOutOfRange = true; | ||
return false; | ||
} | ||
if (lt && year > lt) { | ||
if (lt && lt.isBefore(id)) { | ||
ltOutOfRange = true; | ||
return false; | ||
} | ||
return true; | ||
}) | ||
|
||
const hasOutOfRange = useDefaultBounds && ((!hasGtFilter && gtOutOfRange) || (!hasLtFilter && ltOutOfRange)); | ||
|
||
return { filteredIntervals, hasOutOfRange }; | ||
} | ||
|
||
const isDateIntervalUncertain = (timestamp, granularity) => { | ||
const dateObj = moment.utc(timestamp) | ||
|
||
if (granularity === 'month' && dateObj.month() === 0) { | ||
return true; | ||
} else if (granularity === 'day' && dateObj.date() === 1) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export { | ||
cleanDateQParam, | ||
DEFAULT_START_INTERVAL, | ||
formatDateQParam, | ||
timestampToYear, | ||
timestampToLabel, | ||
isDateIntervalUncertain, | ||
filterDateIntervals | ||
} |
Oops, something went wrong.