UNPKG

2.71 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.dateMatchModifiers = dateMatchModifiers;
4const rangeIncludesDate_js_1 = require("./rangeIncludesDate.js");
5const typeguards_js_1 = require("./typeguards.js");
6/**
7 * Returns whether a day matches against at least one of the given
8 * {@link Matcher}.
9 *
10 * ```tsx
11 * const day = new Date(2022, 5, 19);
12 * const matcher1: DateRange = {
13 * from: new Date(2021, 12, 21),
14 * to: new Date(2021, 12, 30)
15 * }
16 * const matcher2: DateRange = {
17 * from: new Date(2022, 5, 1),
18 * to: new Date(2022, 5, 23)
19 * }
20 * const isMatch(day, [matcher1, matcher2]); // true, since day is in the matcher1 range.
21 * ```
22 *
23 * @group Utilities
24 */
25function dateMatchModifiers(date, matchers, dateLib) {
26 const matchersArr = !Array.isArray(matchers) ? [matchers] : matchers;
27 const { isSameDay, differenceInCalendarDays, isAfter } = dateLib;
28 return matchersArr.some((matcher) => {
29 if (typeof matcher === "boolean") {
30 return matcher;
31 }
32 if (dateLib.isDate(matcher)) {
33 return isSameDay(date, matcher);
34 }
35 if ((0, typeguards_js_1.isDatesArray)(matcher, dateLib)) {
36 return matcher.includes(date);
37 }
38 if ((0, typeguards_js_1.isDateRange)(matcher)) {
39 return (0, rangeIncludesDate_js_1.rangeIncludesDate)(matcher, date, false, dateLib);
40 }
41 if ((0, typeguards_js_1.isDayOfWeekType)(matcher)) {
42 if (!Array.isArray(matcher.dayOfWeek)) {
43 return matcher.dayOfWeek === date.getDay();
44 }
45 return matcher.dayOfWeek.includes(date.getDay());
46 }
47 if ((0, typeguards_js_1.isDateInterval)(matcher)) {
48 const diffBefore = differenceInCalendarDays(matcher.before, date);
49 const diffAfter = differenceInCalendarDays(matcher.after, date);
50 const isDayBefore = diffBefore > 0;
51 const isDayAfter = diffAfter < 0;
52 const isClosedInterval = isAfter(matcher.before, matcher.after);
53 if (isClosedInterval) {
54 return isDayAfter && isDayBefore;
55 }
56 else {
57 return isDayBefore || isDayAfter;
58 }
59 }
60 if ((0, typeguards_js_1.isDateAfterType)(matcher)) {
61 return differenceInCalendarDays(date, matcher.after) > 0;
62 }
63 if ((0, typeguards_js_1.isDateBeforeType)(matcher)) {
64 return differenceInCalendarDays(matcher.before, date) > 0;
65 }
66 if (typeof matcher === "function") {
67 return matcher(date);
68 }
69 return false;
70 });
71}
72//# sourceMappingURL=dateMatchModifiers.js.map
\No newline at end of file