I am trying to a function generateIntervals
that takes a startTime
(seconds), endTime
(seconds) and a frequency being either minute
, hour
or day
and it returns an array of sub arrays of intervals divided by that frequency. For example:
generateIntervals(0, 60, 'minute') // [[0, 59], [60, 60]]
generateIntervals(0, 200, 'hour') // [[0, 200]]
Here is my attempt:
const intervalMap = {
minute: 60,
hour: 3600,
day: 86400,
}
function generateIntervals(startTime, endTime, frequency) {
const interval = intervalMap[frequency] - 1
const chunked = []
let curr = startTime
while (curr <= endTime) {
const end = Math.min(endTime, curr + interval)
chunked.push([curr, end])
curr = end + 1
}
return chunked
}
I works but I wonder if there is a cleaner way for doing that? maybe a more functional way?