1 | import moment from 'moment';
|
2 |
|
3 | import isBeforeDay from './isBeforeDay';
|
4 | import isAfterDay from './isAfterDay';
|
5 | import toISOMonthString from './toISOMonthString';
|
6 |
|
7 | const startCacheOutsideDays = new Map();
|
8 | const endCacheOutsideDays = new Map();
|
9 |
|
10 | const startCacheInsideDays = new Map();
|
11 | const endCacheInsideDays = new Map();
|
12 |
|
13 | export default function isDayVisible(day, month, numberOfMonths, enableOutsideDays) {
|
14 | if (!moment.isMoment(day)) return false;
|
15 |
|
16 |
|
17 |
|
18 | const startKey = toISOMonthString(month);
|
19 |
|
20 | const endKey = startKey + '+' + numberOfMonths;
|
21 |
|
22 | if (enableOutsideDays) {
|
23 | if (!startCacheOutsideDays.has(startKey)) {
|
24 | startCacheOutsideDays.set(startKey, month.clone().startOf('month').startOf('week'));
|
25 | }
|
26 |
|
27 | if (isBeforeDay(day, startCacheOutsideDays.get(startKey))) return false;
|
28 |
|
29 | if (!endCacheOutsideDays.has(endKey)) {
|
30 | endCacheOutsideDays.set(
|
31 | endKey,
|
32 | month.clone().endOf('week').add(numberOfMonths - 1, 'months').endOf('month')
|
33 | .endOf('week'),
|
34 | );
|
35 | }
|
36 |
|
37 | return !isAfterDay(day, endCacheOutsideDays.get(endKey));
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 | if (!startCacheInsideDays.has(startKey)) {
|
43 | startCacheInsideDays.set(startKey, month.clone().startOf('month'));
|
44 | }
|
45 |
|
46 | if (isBeforeDay(day, startCacheInsideDays.get(startKey))) return false;
|
47 |
|
48 | if (!endCacheInsideDays.has(endKey)) {
|
49 | endCacheInsideDays.set(
|
50 | endKey,
|
51 | month.clone().add(numberOfMonths - 1, 'months').endOf('month'),
|
52 | );
|
53 | }
|
54 |
|
55 | return !isAfterDay(day, endCacheInsideDays.get(endKey));
|
56 | }
|