-
-
Notifications
You must be signed in to change notification settings - Fork 697
/
addToRange.ts
44 lines (42 loc) · 1 KB
/
addToRange.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { isAfter, isBefore, isSameDay } from "date-fns";
import { DateRange } from "../../../types/Matchers";
/**
* Add a day to an existing range.
*
* The returned range takes in account the `undefined` values and if the added
* day is already present in the range.
*/
export function addToRange(
day: Date,
range?: DateRange
): DateRange | undefined {
const { from, to } = range || {};
if (from && to) {
if (isSameDay(to, day) && isSameDay(from, day)) {
return undefined;
}
if (isSameDay(to, day)) {
return { from: to, to: undefined };
}
if (isSameDay(from, day)) {
return undefined;
}
if (isAfter(from, day)) {
return { from: day, to };
}
return { from, to: day };
}
if (to) {
if (isAfter(day, to)) {
return { from: to, to: day };
}
return { from: day, to };
}
if (from) {
if (isBefore(day, from)) {
return { from: day, to: from };
}
return { from, to: day };
}
return { from: day, to: undefined };
}