Core ranges include containment, overlap checks, and intersection. Importing this plugin attaches subtract() and union() onto DateRange, DateTimeRange, and TimeRange, and exports mergeRanges() for coalescing lists of ranges.
Each range class keeps its own endpoint convention. Calendar date ranges are inclusive at both ends, while moment and clock ranges exclude their end point.
Instance methods
subtract()
range.subtract(other): Range[]
Removes the span covered by other from this range and returns zero, one, or two remaining ranges of the same kind. Returned ranges keep the receiver’s locale and time zone.
Subtracting a disjoint or empty range returns the original range in a one-element array. Calling subtract() on an empty moment or clock range returns an empty array.
Parameters
otherRange | stringrequired
Range instance or interval string of the same kind to remove.
Returns
Array<DateRange | DateTimeRange | TimeRange>
Ordered array of remaining ranges of the same kind.
Combines this range with an overlapping or directly adjacent range of the same kind, returning null when a gap separates them. Returned ranges keep the receiver’s locale and time zone.
Two DateRange values count as adjacent when the second starts the day after the first ends. Two DateTimeRange or TimeRange values count as adjacent when one ends at the exact start of the other, and empty ranges act as no-ops.
Parameters
otherRange | stringrequired
Range instance or interval string of the same kind to combine with.
Returns
DateRange | DateTimeRange | TimeRange | null
Combined range of the same kind, or null when a gap separates them.
Sorts an iterable of same-kind ranges and coalesces every overlapping or adjacent span into minimal disjoint ranges. Empty moment and clock ranges are omitted from the output.
Passing mixed range kinds throws a TypeError with code conflictingRanges, and passing non-range items throws a TypeError with code invalidRange.
Overnight TimeRange values wrap across midnight cleanly in subtract(), union(), and mergeRanges(). Adjacent pieces that touch midnight stitch back into a single overnight range.
A TimeRange with equal start and end times is empty, so no single TimeRange can represent all 24 hours. Calling union() or mergeRanges() on clock ranges that cover the entire day throws a RangeError with code unsupportedUnion.