forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-dates.js
More file actions
36 lines (31 loc) · 998 Bytes
/
load-dates.js
File metadata and controls
36 lines (31 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const loadDates = (moment) => ({ startDate }) => {
// Check to see if startDate is a string
if (typeof startDate !== 'string') {
console.warn(
`Error: startDate for dateRange must be of type string, instead: startDate: ${typeof startDate}`,
)
throw new Error('Start date is not a valid string. Please try again.')
}
// Check to see if startDate matches the following regex
const DATE_REGEX = /\d\d\d\d-\d\d-\d\d/
if (!DATE_REGEX.test(startDate)) {
console.warn(
`Error: startDate for dateRange must conform to format: YYYY-MM-DD, instead: startDate: ${startDate}`,
)
throw new Error(
'Start date is not a valid format, please conform to YYYY-MM-DD. Please try again.',
)
}
let start = moment(startDate)
const dates = []
for (let i = 1; i <= 13; i++) {
dates.push({
startDate: start.startOf('month').format('YYYY-MM-DD'),
})
start = start.add(1, 'month')
}
return dates
}
module.exports = {
loadDates,
}