UNPKG

1.66 kBJavaScriptView Raw
1import moment from 'moment';
2
3import isBeforeDay from './isBeforeDay';
4import isAfterDay from './isAfterDay';
5import toISOMonthString from './toISOMonthString';
6
7const startCacheOutsideDays = new Map();
8const endCacheOutsideDays = new Map();
9
10const startCacheInsideDays = new Map();
11const endCacheInsideDays = new Map();
12
13export default function isDayVisible(day, month, numberOfMonths, enableOutsideDays) {
14 if (!moment.isMoment(day)) return false;
15
16 // Cloning is a little expensive, so we want to do it as little as possible.
17
18 const startKey = toISOMonthString(month);
19 // eslint-disable-next-line prefer-template
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 // !enableOutsideDays
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}