DateRange.intersectionOf constructor

DateRange.intersectionOf(
  1. DateRange a,
  2. DateRange b
)

Returns the intersection of DateRanges a and b.

If a does not overlap with b, this factory returns null. Note that this is distinct from a DateRange with start == end == null, which represents a DateRange that is unbounded in both directions.

Implementation

factory DateRange.intersectionOf(DateRange a, DateRange b) {
  final start = _laterStartDateOf(a.start, b.start);
  final end = _earlierEndDateOf(a.end, b.end);

  // Note that we don't use >= here, as [DateRange]s are inclusive;
  // `start == end` is a valid condition for a date range.
  if (start != null && end != null && start > end) {
    // TODO: Need to refactor this logic
    return DateRange(null, null);
  }

  return DateRange(start, end);
}