import { DateMarker } from './marker' ; import { DateEnv , DateInput } from './env' ; export interface DateRangeInput { start ? : DateInput ; end ? : DateInput ; } export interface OpenDateRange { start : DateMarker | null ; end : DateMarker | null ; } export interface DateRange { start : DateMarker ; end : DateMarker ; } export function parseRange ( input , dateEnv ) { let start = null ; let end = null ; if ( input . start ) { start = dateEnv . createMarker ( input . start ) ; } if ( input . end ) { end = dateEnv . createMarker ( input . end ) ; } if ( ! start && ! end ) { return null ; } if ( start && end && end < start ) { return null ; } return { start , end } ; } export function invertRanges ( ranges , constraintRange ) { let invertedRanges = [ ] ; let { start } = constraintRange ; let i ; let dateRange ; ranges . sort ( compareRanges ) ; for ( i = 0 ; i < ranges . length ; i += 1 ) { dateRange = ranges [ i ] ; if ( dateRange . start > start ) { invertedRanges . push ( { start , end : dateRange . start } ) ; } if ( dateRange . end > start ) { start = dateRange . end ; } } if ( start < constraintRange . end ) { invertedRanges . push ( { start , end : constraintRange . end } ) ; } return invertedRanges ; } function compareRanges ( range0 , range1 ) { return range0 . start . valueOf ( ) - range1 . start . valueOf ( ) ; } export function intersectRanges ( range0 , range1 ) { let { start , end } = range0 ; let newRange = null ; if ( range1 . start !== null ) { if ( start === null ) { start = range1 . start ; } else { start = new Date ( Math . max ( start . valueOf ( ) , range1 . start . valueOf ( ) ) ) ; } } if ( range1 . end != null ) { if ( end === null ) { end = range1 . end ; } else { end = new Date ( Math . min ( end . valueOf ( ) , range1 . end . valueOf ( ) ) ) ; } } if ( start === null || end === null || start < end ) { newRange = { start , end } ; } return newRange ; } export function rangesEqual ( range0 , range1 ) { return ( range0 . start === null ? null : range0 . start . valueOf ( ) ) === ( range1 . start === null ? null : range1 . start . valueOf ( ) ) && ( range0 . end === null ? null : range0 . end . valueOf ( ) ) === ( range1 . end === null ? null : range1 . end . valueOf ( ) ) ; } export function rangesIntersect ( range0 , range1 ) { return ( range0 . end === null || range1 . start === null || range0 . end > range1 . start ) && ( range0 . start === null || range1 . end === null || range0 . start < range1 . end ) ; } export function rangeContainsRange ( outerRange , innerRange ) { return ( outerRange . start === null || ( innerRange . start !== null && innerRange . start >= outerRange . start ) ) && ( outerRange . end === null || ( innerRange . end !== null && innerRange . end <= outerRange . end ) ) ; } export function rangeContainsMarker ( range , date ) { return ( range . start === null || date >= range . start ) && ( range . end === null || date < range . end ) ; } export function constrainMarkerToRange ( date , range ) { if ( range . start != null && date < range . start ) { return range . start ; } if ( range . end != null && date >= range . end ) { return new Date ( range . end . valueOf ( ) - 1 ) ; } return date ; }