1 | import { rangeIncludesDate } from "./rangeIncludesDate.js";
|
2 | import { isDateAfterType, isDateBeforeType, isDateInterval, isDateRange, isDatesArray, isDayOfWeekType } from "./typeguards.js";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | export function dateMatchModifiers(date, matchers, dateLib) {
|
23 | const matchersArr = !Array.isArray(matchers) ? [matchers] : matchers;
|
24 | const { isSameDay, differenceInCalendarDays, isAfter } = dateLib;
|
25 | return matchersArr.some((matcher) => {
|
26 | if (typeof matcher === "boolean") {
|
27 | return matcher;
|
28 | }
|
29 | if (dateLib.isDate(matcher)) {
|
30 | return isSameDay(date, matcher);
|
31 | }
|
32 | if (isDatesArray(matcher, dateLib)) {
|
33 | return matcher.includes(date);
|
34 | }
|
35 | if (isDateRange(matcher)) {
|
36 | return rangeIncludesDate(matcher, date, false, dateLib);
|
37 | }
|
38 | if (isDayOfWeekType(matcher)) {
|
39 | if (!Array.isArray(matcher.dayOfWeek)) {
|
40 | return matcher.dayOfWeek === date.getDay();
|
41 | }
|
42 | return matcher.dayOfWeek.includes(date.getDay());
|
43 | }
|
44 | if (isDateInterval(matcher)) {
|
45 | const diffBefore = differenceInCalendarDays(matcher.before, date);
|
46 | const diffAfter = differenceInCalendarDays(matcher.after, date);
|
47 | const isDayBefore = diffBefore > 0;
|
48 | const isDayAfter = diffAfter < 0;
|
49 | const isClosedInterval = isAfter(matcher.before, matcher.after);
|
50 | if (isClosedInterval) {
|
51 | return isDayAfter && isDayBefore;
|
52 | }
|
53 | else {
|
54 | return isDayBefore || isDayAfter;
|
55 | }
|
56 | }
|
57 | if (isDateAfterType(matcher)) {
|
58 | return differenceInCalendarDays(date, matcher.after) > 0;
|
59 | }
|
60 | if (isDateBeforeType(matcher)) {
|
61 | return differenceInCalendarDays(matcher.before, date) > 0;
|
62 | }
|
63 | if (typeof matcher === "function") {
|
64 | return matcher(date);
|
65 | }
|
66 | return false;
|
67 | });
|
68 | }
|
69 |
|
\ | No newline at end of file |