Create service to consolidate date ranges #110
Merged
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.
https://eaflood.atlassian.net/browse/WATER-3895
As part of our work to create transaction lines for sroc, we need to be able to take a set of dates ranges and consolidate them into a set of non-overlapping ranges. This PR implements
ConsolidateDateRangesService
to do this.The service takes an array of date ranges, where each date range is an object with a
startDate
andendDate
(each of which is a regularDate
object). For example:It merges any overlapping date ranges, leaves alone any which don't overlap, and returns an array of the resulting ranges. So the date ranges above consolidate to:
Note that if a range starts on the same day the previous range ends, that is classed as one continuous range. But a range that starts the day after the previous range ends is classed as a separate range to that previous one. For example:
Consolidates to:
One last thing to note is that, due to the way the merging has been implemented, the returned array of date ranges is sorted in order from earliest start date to latest start date.