@chayns-components/date 5.0.0-beta.919 → 5.0.0-beta.921
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/components/calendar/Calendar.js +17 -17
- package/lib/cjs/components/calendar/Calendar.js.map +1 -1
- package/lib/cjs/components/calendar/month-wrapper/MonthWrapper.js +1 -0
- package/lib/cjs/components/calendar/month-wrapper/MonthWrapper.js.map +1 -1
- package/lib/cjs/components/calendar/month-wrapper/month/Month.js.map +1 -1
- package/lib/cjs/components/calendar/month-wrapper/month/day-wrapper/DayWrapper.js +19 -21
- package/lib/cjs/components/calendar/month-wrapper/month/day-wrapper/DayWrapper.js.map +1 -1
- package/lib/cjs/components/calendar/month-wrapper/month/day-wrapper/day/Day.js +3 -3
- package/lib/cjs/components/calendar/month-wrapper/month/day-wrapper/day/Day.js.map +1 -1
- package/lib/cjs/components/calendar/month-wrapper/month/weekday-wrapper/WeekdayWrapper.js +9 -11
- package/lib/cjs/components/calendar/month-wrapper/month/weekday-wrapper/WeekdayWrapper.js.map +1 -1
- package/lib/cjs/components/calendar/month-year-pickers/MonthYearPickers.js +2 -2
- package/lib/cjs/components/calendar/month-year-pickers/MonthYearPickers.js.map +1 -1
- package/lib/cjs/components/date-info/DateInfo.js.map +1 -1
- package/lib/cjs/hooks/useDateInfo.js +27 -72
- package/lib/cjs/hooks/useDateInfo.js.map +1 -1
- package/lib/cjs/index.js +130 -2
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/types/dateinfo.js +2 -0
- package/lib/cjs/types/dateinfo.js.map +1 -0
- package/lib/cjs/utils/calendar.js +12 -9
- package/lib/cjs/utils/calendar.js.map +1 -1
- package/lib/cjs/utils/date.js +114 -0
- package/lib/cjs/utils/date.js.map +1 -0
- package/lib/cjs/utils/dateInfo.js +99 -344
- package/lib/cjs/utils/dateInfo.js.map +1 -1
- package/lib/esm/components/calendar/Calendar.js +4 -4
- package/lib/esm/components/calendar/Calendar.js.map +1 -1
- package/lib/esm/components/calendar/month-wrapper/MonthWrapper.js +1 -0
- package/lib/esm/components/calendar/month-wrapper/MonthWrapper.js.map +1 -1
- package/lib/esm/components/calendar/month-wrapper/month/Month.js.map +1 -1
- package/lib/esm/components/calendar/month-wrapper/month/day-wrapper/DayWrapper.js +2 -4
- package/lib/esm/components/calendar/month-wrapper/month/day-wrapper/DayWrapper.js.map +1 -1
- package/lib/esm/components/calendar/month-wrapper/month/day-wrapper/day/Day.js +1 -1
- package/lib/esm/components/calendar/month-wrapper/month/day-wrapper/day/Day.js.map +1 -1
- package/lib/esm/components/calendar/month-wrapper/month/weekday-wrapper/WeekdayWrapper.js +8 -10
- package/lib/esm/components/calendar/month-wrapper/month/weekday-wrapper/WeekdayWrapper.js.map +1 -1
- package/lib/esm/components/calendar/month-year-pickers/MonthYearPickers.js +1 -1
- package/lib/esm/components/calendar/month-year-pickers/MonthYearPickers.js.map +1 -1
- package/lib/esm/components/date-info/DateInfo.js.map +1 -1
- package/lib/esm/hooks/useDateInfo.js +28 -73
- package/lib/esm/hooks/useDateInfo.js.map +1 -1
- package/lib/esm/index.js +10 -2
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/types/dateinfo.js +2 -0
- package/lib/esm/types/dateinfo.js.map +1 -0
- package/lib/esm/utils/calendar.js +4 -3
- package/lib/esm/utils/calendar.js.map +1 -1
- package/lib/esm/utils/date.js +89 -0
- package/lib/esm/utils/date.js.map +1 -0
- package/lib/esm/utils/dateInfo.js +98 -339
- package/lib/esm/utils/dateInfo.js.map +1 -1
- package/lib/types/components/calendar/Calendar.d.ts +2 -2
- package/lib/types/components/calendar/month-wrapper/MonthWrapper.d.ts +2 -2
- package/lib/types/components/calendar/month-wrapper/MonthWrapper.styles.d.ts +1 -1
- package/lib/types/components/calendar/month-wrapper/month/Month.d.ts +2 -2
- package/lib/types/components/calendar/month-wrapper/month/weekday-wrapper/WeekdayWrapper.d.ts +2 -2
- package/lib/types/components/calendar/month-year-pickers/MonthYearPickers.d.ts +2 -2
- package/lib/types/components/date-info/DateInfo.d.ts +1 -1
- package/lib/types/components/opening-times/opening-inputs/OpeningInputs.styles.d.ts +1 -1
- package/lib/types/components/opening-times/opening-inputs/opening-input/OpeningInput.styles.d.ts +1 -1
- package/lib/types/hooks/useDateInfo.d.ts +1 -34
- package/lib/types/index.d.ts +6 -4
- package/lib/types/types/dateinfo.d.ts +34 -0
- package/lib/types/utils/calendar.d.ts +2 -2
- package/lib/types/utils/date.d.ts +27 -0
- package/lib/types/utils/dateInfo.d.ts +10 -51
- package/package.json +3 -4
- package/lib/cjs/constants/dateInfo.js +0 -639
- package/lib/cjs/constants/dateInfo.js.map +0 -1
- package/lib/esm/constants/dateInfo.js +0 -633
- package/lib/esm/constants/dateInfo.js.map +0 -1
- package/lib/types/constants/dateInfo.d.ts +0 -23
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MonthYearPickers.js","names":["ComboBox","
|
|
1
|
+
{"version":3,"file":"MonthYearPickers.js","names":["ComboBox","React","useMemo","useState","formatMonth","getYearsBetween","isDateInRange","StyledMonthName","differenceInCalendarMonths","MonthYearPickerType","MonthYearPickers","_ref","month","year","locale","minDate","maxDate","setCurrentDate","displayIndex","showMonthYearPickers","currentYear","Date","getFullYear","months","map","tempMonth","text","value","hasMultipleMonths","years","tempYear","toString","hasMultipleYears","length","createElement","Fragment","onSelect","selectedItem","tempSelectedDate","currentDate","lists","list","placeholder"],"sources":["../../../../../src/components/calendar/month-year-pickers/MonthYearPickers.tsx"],"sourcesContent":["import { ComboBox } from '@chayns-components/core';\nimport React, { FC, useMemo, useState } from 'react';\nimport { EMonth } from '../../../types/calendar';\nimport { formatMonth, getYearsBetween, isDateInRange } from '../../../utils/calendar';\nimport { StyledMonthName } from '../month-wrapper/month/Month.styles';\nimport {Language} from \"chayns-api\";\nimport {differenceInCalendarMonths} from \"../../../utils/date\";\n\nexport enum MonthYearPickerType {\n Month = 'month',\n Year = 'year',\n}\n\ntype MonthYearPickerProps = {\n month: EMonth;\n year: number;\n locale: Language;\n minDate: Date;\n maxDate: Date;\n setCurrentDate: (date: Date) => void;\n displayIndex?: number;\n showMonthYearPickers: boolean;\n};\n\nconst MonthYearPickers: FC<MonthYearPickerProps> = ({\n month,\n year,\n locale,\n minDate,\n maxDate,\n setCurrentDate,\n displayIndex = 0,\n showMonthYearPickers,\n}) => {\n const [currentYear] = useState(new Date().getFullYear());\n\n const months = useMemo(\n () =>\n [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].map((tempMonth) => ({\n text: formatMonth({ month: tempMonth, locale }),\n value: tempMonth,\n })),\n [locale],\n );\n\n const hasMultipleMonths = useMemo(\n () => differenceInCalendarMonths(maxDate, minDate) > 0,\n [minDate, maxDate],\n );\n\n const years = useMemo(\n () =>\n getYearsBetween(minDate, maxDate).map((tempYear) => ({\n text: tempYear.toString(),\n value: tempYear,\n })),\n [minDate, maxDate],\n );\n\n const hasMultipleYears = useMemo(() => years.length > 1, [years]);\n\n return (\n <>\n {showMonthYearPickers && hasMultipleMonths ? (\n <ComboBox\n onSelect={(selectedItem) => {\n const tempSelectedDate = isDateInRange({\n minDate,\n maxDate,\n currentDate: new Date(\n year,\n (selectedItem.value as number) - 1 - displayIndex,\n 1,\n ),\n });\n\n setCurrentDate(tempSelectedDate);\n }}\n lists={[\n {\n list: months,\n },\n ]}\n selectedItem={{\n text: formatMonth({ month, locale }),\n value: month,\n }}\n placeholder=\"\"\n />\n ) : (\n <StyledMonthName>{formatMonth({ locale, month })}</StyledMonthName>\n )}\n {showMonthYearPickers && hasMultipleYears ? (\n <ComboBox\n onSelect={(selectedItem) => {\n const tempSelectedDate = isDateInRange({\n minDate,\n maxDate,\n currentDate: new Date(\n selectedItem.value as number,\n month - 1 - displayIndex,\n 1,\n ),\n });\n\n setCurrentDate(tempSelectedDate);\n }}\n lists={[\n {\n list: years,\n },\n ]}\n selectedItem={{\n text: year.toString(),\n value: year,\n }}\n placeholder=\"\"\n />\n ) : (\n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ACxB,MAAMC,gBAAgB,GAAGhC,aAAa,CAAC;QACnCS,OAAO;QACPC,OAAO;QACPuB,WAAW,EAAE,IAAIlB,IAAI,CACjBgB,YAAY,CAACV,KAAK,EAClBf,KAAK,GAAG,CAAC,GAAGM,YAAY,EACxB,CACJ;MACJ,CAAC,CAAC;MAEFD,cAAc,CAACqB,gBAAgB,CAAC;IACpC,CAAE;IACFE,KAAK,EAAE,CACH;MACIC,IAAI,EAAEZ;IACV,CAAC,CACH;IACFQ,YAAY,EAAE;MACVX,IAAI,EAAEb,IAAI,CAACkB,QAAQ,CAAC,CAAC;MACrBJ,KAAK,EAAEd;IACX,CAAE;IACF6B,WAAW,EAAC;EAAE,CACjB,CAAC,gBAEFzC,KAAA,CAAAiC,aAAA,CAAC3B,eAAe,QAAEa,WAAW,KAAKP,IAAI,GAAGA,IAAI,GAAG,EAAoB,CAE1E,CAAC;AAEX,CAAC;AAED,eAAeH,gBAAgB","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DateInfo.js","names":["React","useMemo","useDateInfo","DateInfo","_ref","date","preText","shouldShowYear","shouldShowTime","shouldShowRelativeDayOfWeek","shouldUseShortText","shouldShowDayOfWeek","shouldShowDateToNowDifference","formattedDate","createElement","displayName"],"sources":["../../../../src/components/date-info/DateInfo.tsx"],"sourcesContent":["import React, { FC, useMemo } from 'react';\nimport { useDateInfo
|
|
1
|
+
{"version":3,"file":"DateInfo.js","names":["React","useMemo","useDateInfo","DateInfo","_ref","date","preText","shouldShowYear","shouldShowTime","shouldShowRelativeDayOfWeek","shouldUseShortText","shouldShowDayOfWeek","shouldShowDateToNowDifference","formattedDate","createElement","displayName"],"sources":["../../../../src/components/date-info/DateInfo.tsx"],"sourcesContent":["import React, { FC, useMemo } from 'react';\nimport { useDateInfo } from '../../hooks/useDateInfo';\nimport { UseDateInfoOptions } from '../../types/dateinfo';\n\nexport type DateInfoProps = UseDateInfoOptions;\n\nconst DateInfo: FC<DateInfoProps> = ({\n date,\n preText = '',\n shouldShowYear,\n shouldShowTime,\n shouldShowRelativeDayOfWeek,\n shouldUseShortText,\n shouldShowDayOfWeek,\n shouldShowDateToNowDifference,\n}) => {\n const formattedDate = useDateInfo({\n date,\n preText,\n shouldShowYear,\n shouldShowTime,\n shouldUseShortText,\n shouldShowDayOfWeek,\n shouldShowRelativeDayOfWeek,\n shouldShowDateToNowDifference,\n });\n\n return useMemo(() => <span>{formattedDate}</span>, [formattedDate]);\n};\n\nDateInfo.displayName = 'DateInfo';\n\nexport default DateInfo;\n"],"mappings":"AAAA,OAAOA,KAAK,IAAQC,OAAO,QAAQ,OAAO;AAC1C,SAASC,WAAW,QAAQ,yBAAyB;AAKrD,MAAMC,QAA2B,GAAGC,IAAA,IAS9B;EAAA,IAT+B;IACjCC,IAAI;IACJC,OAAO,GAAG,EAAE;IACZC,cAAc;IACdC,cAAc;IACdC,2BAA2B;IAC3BC,kBAAkB;IAClBC,mBAAmB;IACnBC;EACJ,CAAC,GAAAR,IAAA;EACG,MAAMS,aAAa,GAAGX,WAAW,CAAC;IAC9BG,IAAI;IACJC,OAAO;IACPC,cAAc;IACdC,cAAc;IACdE,kBAAkB;IAClBC,mBAAmB;IACnBF,2BAA2B;IAC3BG;EACJ,CAAC,CAAC;EAEF,OAAOX,OAAO,CAAC,mBAAMD,KAAA,CAAAc,aAAA,eAAOD,aAAoB,CAAC,EAAE,CAACA,aAAa,CAAC,CAAC;AACvE,CAAC;AAEDV,QAAQ,CAACY,WAAW,GAAG,UAAU;AAEjC,eAAeZ,QAAQ","ignoreList":[]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getLanguage } from 'chayns-api';
|
|
2
2
|
import { useEffect, useMemo, useState } from 'react';
|
|
3
|
-
import {
|
|
3
|
+
import { getDateInfo, getTimeTillNow } from '../utils/dateInfo';
|
|
4
4
|
export const useDateInfo = _ref => {
|
|
5
5
|
let {
|
|
6
6
|
date,
|
|
@@ -12,91 +12,46 @@ export const useDateInfo = _ref => {
|
|
|
12
12
|
shouldShowYear,
|
|
13
13
|
preText
|
|
14
14
|
} = _ref;
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const
|
|
15
|
+
const {
|
|
16
|
+
active: language
|
|
17
|
+
} = getLanguage();
|
|
18
|
+
const [formattedDate, setFormattedDate] = useState(date.toLocaleDateString());
|
|
19
|
+
const [currentDate, setCurrentDate] = useState(new Date());
|
|
19
20
|
useEffect(() => {
|
|
20
|
-
// This useEffect is used for normal date formation
|
|
21
21
|
if (shouldShowDateToNowDifference) {
|
|
22
22
|
return;
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
shouldUseShortText,
|
|
28
|
-
date: formattedDate,
|
|
29
|
-
language
|
|
30
|
-
});
|
|
31
|
-
if (!isDateNearToday || !shouldShowRelativeDayOfWeek || !shouldShowTime) {
|
|
32
|
-
let formatString = 'dd. ';
|
|
33
|
-
formatString += `${getMonthFormat({
|
|
34
|
-
shouldUseShortText
|
|
35
|
-
})}`;
|
|
36
|
-
formatString += `${getYearFormat({
|
|
37
|
-
date: formattedDate,
|
|
38
|
-
shouldShowYear
|
|
39
|
-
})}`;
|
|
40
|
-
newFormattedDateString += format(formattedDate, formatString, {
|
|
41
|
-
locale: language
|
|
42
|
-
});
|
|
43
|
-
} else {
|
|
44
|
-
newFormattedDateString = newFormattedDateString.replace(', ', '');
|
|
45
|
-
}
|
|
46
|
-
newFormattedDateString += getFormattedTime({
|
|
47
|
-
date: formattedDate,
|
|
24
|
+
setFormattedDate(getDateInfo({
|
|
25
|
+
date,
|
|
26
|
+
shouldShowYear,
|
|
48
27
|
shouldShowTime,
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
// Calculate remaining time till next minute to update time according to time left
|
|
55
|
-
const [currentDate, setCurrentDate] = useState(new Date());
|
|
28
|
+
shouldUseShortText,
|
|
29
|
+
shouldShowDayOfWeek,
|
|
30
|
+
shouldShowRelativeDayOfWeek
|
|
31
|
+
}));
|
|
32
|
+
}, [date, shouldShowDateToNowDifference, shouldShowDayOfWeek, shouldShowRelativeDayOfWeek, shouldShowTime, shouldShowYear, shouldUseShortText]);
|
|
56
33
|
useEffect(() => {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (timeoutTime < 0) {
|
|
65
|
-
timeoutTime = 60 - new Date().getSeconds() + formattedDate.getSeconds();
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// initialized with remaining time
|
|
69
|
-
let timeDiffInMs = formattedDate.getTime() - currentDate.getTime();
|
|
70
|
-
|
|
71
|
-
// set to elapsed time
|
|
72
|
-
if (isPast(formattedDate)) {
|
|
73
|
-
timeDiffInMs = currentDate.getTime() - formattedDate.getTime();
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// time difference is less than a minute, time should be updated every second
|
|
77
|
-
if (timeDiffInMs < 60000) {
|
|
78
|
-
timeoutTime = 1;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Set timeoutTime to at least 1000ms
|
|
82
|
-
timeoutTime = Math.max(timeoutTime * 1000, 1000);
|
|
83
|
-
const timeout = setTimeout(() => {
|
|
84
|
-
setCurrentDate(new Date());
|
|
85
|
-
}, timeoutTime);
|
|
34
|
+
if (!shouldShowDateToNowDifference) return () => {};
|
|
35
|
+
const updateCurrentDate = () => setCurrentDate(new Date());
|
|
36
|
+
const now = new Date();
|
|
37
|
+
const timeDiffInMs = Math.abs(date.getTime() - now.getTime());
|
|
38
|
+
const updateInterval = timeDiffInMs < 60000 ? 1000 : 60000 - now.getSeconds() * 1000;
|
|
39
|
+
const intervalId = setInterval(updateCurrentDate, 1000);
|
|
40
|
+
const timeout = setTimeout(updateCurrentDate, updateInterval);
|
|
86
41
|
return () => {
|
|
87
42
|
clearTimeout(timeout);
|
|
43
|
+
clearInterval(intervalId);
|
|
88
44
|
};
|
|
89
|
-
}, [
|
|
45
|
+
}, [date, shouldShowDateToNowDifference]);
|
|
90
46
|
useEffect(() => {
|
|
91
|
-
// This useEffect is for showing the difference of the date to now
|
|
92
47
|
if (shouldShowDateToNowDifference) {
|
|
93
|
-
|
|
94
|
-
date
|
|
48
|
+
setFormattedDate(getTimeTillNow({
|
|
49
|
+
date,
|
|
95
50
|
currentDate,
|
|
96
51
|
language
|
|
97
52
|
}));
|
|
98
53
|
}
|
|
99
|
-
}, [
|
|
100
|
-
return useMemo(() => `${preText ? `${preText.trim()} ` : ''}${
|
|
54
|
+
}, [date, currentDate, language, shouldShowDateToNowDifference]);
|
|
55
|
+
return useMemo(() => `${preText ? `${preText.trim()} ` : ''}${formattedDate}`, [formattedDate, preText]);
|
|
101
56
|
};
|
|
102
57
|
//# sourceMappingURL=useDateInfo.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDateInfo.js","names":["
|
|
1
|
+
{"version":3,"file":"useDateInfo.js","names":["getLanguage","useEffect","useMemo","useState","getDateInfo","getTimeTillNow","useDateInfo","_ref","date","shouldShowDateToNowDifference","shouldShowRelativeDayOfWeek","shouldShowDayOfWeek","shouldShowTime","shouldUseShortText","shouldShowYear","preText","active","language","formattedDate","setFormattedDate","toLocaleDateString","currentDate","setCurrentDate","Date","updateCurrentDate","now","timeDiffInMs","Math","abs","getTime","updateInterval","getSeconds","intervalId","setInterval","timeout","setTimeout","clearTimeout","clearInterval","trim"],"sources":["../../../src/hooks/useDateInfo.ts"],"sourcesContent":["import { getLanguage } from 'chayns-api';\nimport { useEffect, useMemo, useState } from 'react';\nimport { UseDateInfoOptions } from '../types/dateinfo';\nimport { getDateInfo, getTimeTillNow } from '../utils/dateInfo';\n\nexport const useDateInfo = ({\n date,\n shouldShowDateToNowDifference,\n shouldShowRelativeDayOfWeek,\n shouldShowDayOfWeek,\n shouldShowTime,\n shouldUseShortText,\n shouldShowYear,\n preText,\n}: UseDateInfoOptions) => {\n const { active: language } = getLanguage();\n\n const [formattedDate, setFormattedDate] = useState(date.toLocaleDateString());\n const [currentDate, setCurrentDate] = useState(new Date());\n\n useEffect(() => {\n if (shouldShowDateToNowDifference) {\n return;\n }\n\n setFormattedDate(\n getDateInfo({\n date,\n shouldShowYear,\n shouldShowTime,\n shouldUseShortText,\n shouldShowDayOfWeek,\n shouldShowRelativeDayOfWeek,\n }),\n );\n }, [\n date,\n shouldShowDateToNowDifference,\n shouldShowDayOfWeek,\n shouldShowRelativeDayOfWeek,\n shouldShowTime,\n shouldShowYear,\n shouldUseShortText,\n ]);\n\n useEffect(() => {\n if (!shouldShowDateToNowDifference) return () => {};\n\n const updateCurrentDate = () => setCurrentDate(new Date());\n const now = new Date();\n const timeDiffInMs = Math.abs(date.getTime() - now.getTime());\n\n const updateInterval = timeDiffInMs < 60000 ? 1000 : 60000 - now.getSeconds() * 1000;\n\n const intervalId = setInterval(updateCurrentDate, 1000);\n const timeout = setTimeout(updateCurrentDate, updateInterval);\n\n return () => {\n clearTimeout(timeout);\n clearInterval(intervalId);\n };\n }, [date, shouldShowDateToNowDifference]);\n\n useEffect(() => {\n if (shouldShowDateToNowDifference) {\n setFormattedDate(getTimeTillNow({ date, currentDate, language }));\n }\n }, [date, currentDate, language, shouldShowDateToNowDifference]);\n\n return useMemo(\n () => `${preText ? `${preText.trim()} ` : ''}${formattedDate}`,\n [formattedDate, preText],\n );\n};\n"],"mappings":"AAAA,SAASA,WAAW,QAAQ,YAAY;AACxC,SAASC,SAAS,EAAEC,OAAO,EAAEC,QAAQ,QAAQ,OAAO;AAEpD,SAASC,WAAW,EAAEC,cAAc,QAAQ,mBAAmB;AAE/D,OAAO,MAAMC,WAAW,GAAGC,IAAA,IASD;EAAA,IATE;IACxBC,IAAI;IACJC,6BAA6B;IAC7BC,2BAA2B;IAC3BC,mBAAmB;IACnBC,cAAc;IACdC,kBAAkB;IAClBC,cAAc;IACdC;EACgB,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,OAAO,MAAM;MACTM,YAAY,CAACF,OAAO,CAAC;MACrBG,aAAa,CAACL,UAAU,CAAC;IAC7B,CAAC;EACL,CAAC,EAAE,CAACxB,IAAI,EAAEC,6BAA6B,CAAC,CAAC;EAEzCR,SAAS,CAAC,MAAM;IACZ,IAAIQ,6BAA6B,EAAE;MAC/BU,gBAAgB,CAACd,cAAc,CAAC;QAAEG,IAAI;QAAEa,WAAW;QAAEJ;MAAS,CAAC,CAAC,CAAC;IACrE;EACJ,CAAC,EAAE,CAACT,IAAI,EAAEa,WAAW,EAAEJ,QAAQ,EAAER,6BAA6B,CAAC,CAAC;EAEhE,OAAOP,OAAO,CACV,MAAM,GAAGa,OAAO,GAAG,GAAGA,OAAO,CAACuB,IAAI,CAAC,CAAC,GAAG,GAAG,EAAE,GAAGpB,aAAa,EAAE,EAC9D,CAACA,aAAa,EAAEH,OAAO,CAC3B,CAAC;AACL,CAAC","ignoreList":[]}
|
package/lib/esm/index.js
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
|
+
// Calendar
|
|
1
2
|
export { default as Calendar } from './components/calendar/Calendar';
|
|
2
|
-
export {
|
|
3
|
+
export { CalendarType } from './types/calendar';
|
|
4
|
+
|
|
5
|
+
// OpeningTimes
|
|
3
6
|
export { default as OpeningInputs } from './components/opening-times/opening-inputs/OpeningInputs';
|
|
4
7
|
export { default as OpeningTimes } from './components/opening-times/OpeningTimes';
|
|
8
|
+
// DateInfo
|
|
9
|
+
export { default as DateInfo } from './components/date-info/DateInfo';
|
|
5
10
|
export { useDateInfo } from './hooks/useDateInfo';
|
|
6
|
-
export {
|
|
11
|
+
export { getDateInfo, getTimeTillNow } from './utils/dateInfo';
|
|
12
|
+
|
|
13
|
+
// Utils
|
|
14
|
+
export { isToday, getIsDateNearToday, isTomorrow, isYesterday, isMorning, isCurrentYear, addYears, addDays, isSameDay, eachDayOfInterval, isWithinInterval, subYears, endOfWeek, startOfWeek, isSameMonth, differenceInCalendarMonths, startOfMonth, isAfter, isBefore } from './utils/date';
|
|
7
15
|
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["default","Calendar","
|
|
1
|
+
{"version":3,"file":"index.js","names":["default","Calendar","CalendarType","OpeningInputs","OpeningTimes","DateInfo","useDateInfo","getDateInfo","getTimeTillNow","isToday","getIsDateNearToday","isTomorrow","isYesterday","isMorning","isCurrentYear","addYears","addDays","isSameDay","eachDayOfInterval","isWithinInterval","subYears","endOfWeek","startOfWeek","isSameMonth","differenceInCalendarMonths","startOfMonth","isAfter","isBefore"],"sources":["../../src/index.ts"],"sourcesContent":["// Calendar\nexport {default as Calendar} from './components/calendar/Calendar';\nexport type {Categories, HighlightedDates, HighlightedDateStyles, DateInterval, CustomThumbColors} from './types/calendar';\nexport {CalendarType} from './types/calendar';\n\n// OpeningTimes\nexport {default as OpeningInputs} from './components/opening-times/opening-inputs/OpeningInputs';\nexport {default as OpeningTimes} from './components/opening-times/OpeningTimes';\nexport type {HintTextPosition, OpeningTime, Time, Weekday} from './types/openingTimes';\n\n// DateInfo\nexport {default as DateInfo} from './components/date-info/DateInfo';\nexport {useDateInfo} from './hooks/useDateInfo';\nexport {getDateInfo, getTimeTillNow} from './utils/dateInfo'\n\n// Utils\nexport {\n isToday,\n getIsDateNearToday,\n isTomorrow,\n isYesterday,\n isMorning,\n isCurrentYear,\n addYears,\n addDays,\n isSameDay,\n eachDayOfInterval,\n isWithinInterval,\n subYears,\n endOfWeek,\n startOfWeek,\n isSameMonth,\n differenceInCalendarMonths,\n startOfMonth,\n isAfter,\n isBefore\n} from './utils/date'\n"],"mappings":"AAAA;AACA,SAAQA,OAAO,IAAIC,QAAQ,QAAO,gCAAgC;AAElE,SAAQC,YAAY,QAAO,kBAAkB;;AAE7C;AACA,SAAQF,OAAO,IAAIG,aAAa,QAAO,yDAAyD;AAChG,SAAQH,OAAO,IAAII,YAAY,QAAO,yCAAyC;AAG/E;AACA,SAAQJ,OAAO,IAAIK,QAAQ,QAAO,iCAAiC;AACnE,SAAQC,WAAW,QAAO,qBAAqB;AAC/C,SAAQC,WAAW,EAAEC,cAAc,QAAO,kBAAkB;;AAE5D;AACA,SACIC,OAAO,EACPC,kBAAkB,EAClBC,UAAU,EACVC,WAAW,EACXC,SAAS,EACTC,aAAa,EACbC,QAAQ,EACRC,OAAO,EACPC,SAAS,EACTC,iBAAiB,EACjBC,gBAAgB,EAChBC,QAAQ,EACRC,SAAS,EACTC,WAAW,EACXC,WAAW,EACXC,0BAA0B,EAC1BC,YAAY,EACZC,OAAO,EACPC,QAAQ,QACL,cAAc","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dateinfo.js","names":[],"sources":["../../../src/types/dateinfo.ts"],"sourcesContent":["export interface UseDateInfoOptions {\n /**\n * The date, that should be displayed\n */\n date: Date;\n /**\n * Additional text for \"shouldShowDateToNowDifference\" prop. Writes a text before the calculated time\n */\n preText?: string;\n /**\n * Adds the current year to the display\n */\n shouldShowYear?: boolean;\n /**\n * Adds the time to the display.\n */\n shouldShowTime?: boolean;\n /**\n * Whether the relative day of week to today should be shown (today, yesterday or tomorrow).\n */\n shouldShowRelativeDayOfWeek?: boolean;\n /**\n * Shortens the day and month text to maximum three digits\n */\n shouldUseShortText?: boolean;\n /**\n * Adds the day of week to the display\n */\n shouldShowDayOfWeek?: boolean;\n /**\n * Shows the difference from the date to now. The component handles updates itself.\n */\n shouldShowDateToNowDifference?: boolean;\n}\n"],"mappings":"","ignoreList":[]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isAfter, isBefore, startOfMonth } from "./date";
|
|
2
2
|
export const getMonthAndYear = date => {
|
|
3
3
|
const month = date.getMonth() + 1;
|
|
4
4
|
const year = date.getFullYear();
|
|
@@ -41,8 +41,9 @@ export const formatMonth = _ref2 => {
|
|
|
41
41
|
month,
|
|
42
42
|
locale
|
|
43
43
|
} = _ref2;
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
const date = new Date(2022, month - 1, 1);
|
|
45
|
+
return date.toLocaleString(locale, {
|
|
46
|
+
month: 'long'
|
|
46
47
|
});
|
|
47
48
|
};
|
|
48
49
|
export const findNextDate = (date, dateArray) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"calendar.js","names":["
|
|
1
|
+
{"version":3,"file":"calendar.js","names":["isAfter","isBefore","startOfMonth","getMonthAndYear","date","month","getMonth","year","getFullYear","isDateInRange","_ref","minDate","maxDate","currentDate","monthStartOfCurrentDate","monthStartOfMaxDate","monthStartOfMinDate","getNewDate","index","newDate","Date","setMonth","setFullYear","formatMonth","_ref2","locale","toLocaleString","findNextDate","dateArray","futureDates","filter","d","sort","a","b","getTime","getYearsBetween","startDate","endDate","startYear","endYear","years","push"],"sources":["../../../src/utils/calendar.ts"],"sourcesContent":["import type { EMonth, IMonth } from '../types/calendar';\nimport {isAfter, isBefore, startOfMonth} from \"./date\";\nimport {Language} from \"chayns-api\";\n\nexport const getMonthAndYear = (date: Date): IMonth => {\n const month = date.getMonth() + 1;\n\n const year = date.getFullYear();\n\n return {\n month,\n year,\n };\n};\n\ninterface FormatMonthOptions {\n month: EMonth;\n locale: Language;\n}\n\ninterface IsDateInRange {\n minDate: Date;\n maxDate: Date;\n currentDate: Date;\n}\n\nexport const isDateInRange = ({ minDate, maxDate, currentDate }: IsDateInRange): Date => {\n const monthStartOfCurrentDate = startOfMonth(currentDate);\n const monthStartOfMaxDate = startOfMonth(maxDate);\n const monthStartOfMinDate = startOfMonth(minDate);\n\n switch (true) {\n case isAfter(monthStartOfCurrentDate, monthStartOfMaxDate):\n return monthStartOfMaxDate;\n case isBefore(monthStartOfCurrentDate, monthStartOfMinDate):\n return monthStartOfMinDate;\n default:\n return monthStartOfCurrentDate;\n }\n};\n\nexport const getNewDate = (index: number, currentDate: Date) => {\n const newDate = new Date(currentDate);\n newDate.setMonth(currentDate.getMonth() + index);\n\n if (currentDate.getMonth() === 11 && newDate.getMonth() === 0) {\n newDate.setFullYear(currentDate.getFullYear() + 1);\n }\n\n if (currentDate.getMonth() === 0 && newDate.getMonth() === 11) {\n newDate.setFullYear(currentDate.getFullYear() - 1);\n }\n\n return newDate;\n};\n\nexport const formatMonth = ({ month, locale }: FormatMonthOptions) => {\n const date = new Date(2022, month - 1, 1);\n return date.toLocaleString(locale, { month: 'long' });\n}\n\nexport const findNextDate = (date: Date, dateArray: Date[]): Date | undefined => {\n const futureDates = dateArray.filter((d) => d > date);\n\n futureDates.sort((a, b) => a.getTime() - b.getTime());\n\n return futureDates[0];\n};\n\nexport const getYearsBetween = (startDate: Date, endDate: Date): number[] => {\n const startYear = startDate.getFullYear();\n const endYear = endDate.getFullYear();\n const years: number[] = [];\n\n for (let year = startYear; year <= endYear; year++) {\n years.push(year);\n }\n\n return years;\n};\n"],"mappings":"AACA,SAAQA,OAAO,EAAEC,QAAQ,EAAEC,YAAY,QAAO,QAAQ;AAGtD,OAAO,MAAMC,eAAe,GAAIC,IAAU,IAAa;EACnD,MAAMC,KAAK,GAAGD,IAAI,CAACE,QAAQ,CAAC,CAAC,GAAG,CAAC;EAEjC,MAAMC,IAAI,GAAGH,IAAI,CAACI,WAAW,CAAC,CAAC;EAE/B,OAAO;IACHH,KAAK;IACLE;EACJ,CAAC;AACL,CAAC;AAaD,OAAO,MAAME,aAAa,GAAGC,IAAA,IAA4D;EAAA,IAA3D;IAAEC,OAAO;IAAEC,OAAO;IAAEC;EAA2B,CAAC,GAAAH,IAAA;EAC1E,MAAMI,uBAAuB,GAAGZ,YAAY,CAACW,WAAW,CAAC;EACzD,MAAME,mBAAmB,GAAGb,YAAY,CAACU,OAAO,CAAC;EACjD,MAAMI,mBAAmB,GAAGd,YAAY,CAACS,OAAO,CAAC;EAEjD,QAAQ,IAAI;IACR,KAAKX,OAAO,CAACc,uBAAuB,EAAEC,mBAAmB,CAAC;MACtD,OAAOA,mBAAmB;IAC9B,KAAK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AACG,IAAI,CAAC,CAACC,CAAC,EAAEC,CAAC,KAAKD,CAAC,CAACE,OAAO,CAAC,CAAC,GAAGD,CAAC,CAACC,OAAO,CAAC,CAAC,CAAC;EAErD,OAAON,WAAW,CAAC,CAAC,CAAC;AACzB,CAAC;AAED,OAAO,MAAMO,eAAe,GAAGA,CAACC,SAAe,EAAEC,OAAa,KAAe;EACzE,MAAMC,SAAS,GAAGF,SAAS,CAAC7B,WAAW,CAAC,CAAC;EACzC,MAAMgC,OAAO,GAAGF,OAAO,CAAC9B,WAAW,CAAC,CAAC;EACrC,MAAMiC,KAAe,GAAG,EAAE;EAE1B,KAAK,IAAIlC,IAAI,GAAGgC,SAAS,EAAEhC,IAAI,IAAIiC,OAAO,EAAEjC,IAAI,EAAE,EAAE;IAChDkC,KAAK,CAACC,IAAI,CAACnC,IAAI,CAAC;EACpB;EAEA,OAAOkC,KAAK;AAChB,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
export const isToday = date => {
|
|
2
|
+
const today = new Date();
|
|
3
|
+
return today.toDateString() === date.toDateString();
|
|
4
|
+
};
|
|
5
|
+
export const isTomorrow = date => {
|
|
6
|
+
const tomorrow = new Date();
|
|
7
|
+
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
8
|
+
return tomorrow.toDateString() === date.toDateString();
|
|
9
|
+
};
|
|
10
|
+
export const isYesterday = date => {
|
|
11
|
+
const yesterday = new Date();
|
|
12
|
+
yesterday.setDate(yesterday.getDate() - 1);
|
|
13
|
+
return yesterday.toDateString() === date.toDateString();
|
|
14
|
+
};
|
|
15
|
+
export const isCurrentYear = date => {
|
|
16
|
+
const currentYear = new Date().getFullYear();
|
|
17
|
+
const yearOfGivenDate = date.getFullYear();
|
|
18
|
+
return currentYear === yearOfGivenDate;
|
|
19
|
+
};
|
|
20
|
+
export const getIsDateNearToday = date => {
|
|
21
|
+
const today = new Date();
|
|
22
|
+
today.setHours(0, 0, 0, 0);
|
|
23
|
+
const targetDate = new Date(date);
|
|
24
|
+
targetDate.setHours(0, 0, 0, 0);
|
|
25
|
+
const diffInDays = (targetDate.getTime() - today.getTime()) / (1000 * 60 * 60 * 24);
|
|
26
|
+
return diffInDays === 0 || diffInDays === -1 || diffInDays === 1;
|
|
27
|
+
};
|
|
28
|
+
export const isMorning = date => {
|
|
29
|
+
const hours = date.getHours();
|
|
30
|
+
return hours >= 0 && hours < 12;
|
|
31
|
+
};
|
|
32
|
+
export const isAfter = (firstDate, secondDate) => {
|
|
33
|
+
return firstDate.getTime() > secondDate.getTime();
|
|
34
|
+
};
|
|
35
|
+
export const isBefore = (firstDate, secondDate) => {
|
|
36
|
+
return firstDate.getTime() < secondDate.getTime();
|
|
37
|
+
};
|
|
38
|
+
export const startOfMonth = date => {
|
|
39
|
+
return new Date(date.getFullYear(), date.getMonth(), 1);
|
|
40
|
+
};
|
|
41
|
+
export const addYears = (date, years) => {
|
|
42
|
+
return new Date(date.getFullYear() + years, date.getMonth(), date.getDate());
|
|
43
|
+
};
|
|
44
|
+
export const differenceInCalendarMonths = (firstDate, secondDate) => {
|
|
45
|
+
return (firstDate.getFullYear() - secondDate.getFullYear()) * 12 + (firstDate.getMonth() - secondDate.getMonth());
|
|
46
|
+
};
|
|
47
|
+
export const isSameDay = (firstDate, secondDate) => {
|
|
48
|
+
return firstDate.getFullYear() === secondDate.getFullYear() && firstDate.getMonth() === secondDate.getMonth() && firstDate.getDate() === secondDate.getDate();
|
|
49
|
+
};
|
|
50
|
+
export const isSameMonth = (firstDate, secondDate) => {
|
|
51
|
+
return firstDate.getFullYear() === secondDate.getFullYear() && firstDate.getMonth() === secondDate.getMonth();
|
|
52
|
+
};
|
|
53
|
+
export const isWithinInterval = (date, interval) => {
|
|
54
|
+
return date.getTime() >= interval.start.getTime() && date.getTime() <= interval.end.getTime();
|
|
55
|
+
};
|
|
56
|
+
export const subYears = (date, years) => {
|
|
57
|
+
return new Date(date.getFullYear() - years, date.getMonth(), date.getDate());
|
|
58
|
+
};
|
|
59
|
+
export const startOfWeek = date => {
|
|
60
|
+
const day = date.getDay();
|
|
61
|
+
const diff = day === 0 ? -6 : 1 - day;
|
|
62
|
+
const start = new Date(date);
|
|
63
|
+
start.setDate(date.getDate() + diff);
|
|
64
|
+
start.setHours(0, 0, 0, 0);
|
|
65
|
+
return start;
|
|
66
|
+
};
|
|
67
|
+
export const endOfWeek = date => {
|
|
68
|
+
const day = date.getDay();
|
|
69
|
+
const diff = day === 0 ? 0 : 7 - day;
|
|
70
|
+
const end = new Date(date);
|
|
71
|
+
end.setDate(date.getDate() + diff);
|
|
72
|
+
end.setHours(23, 59, 59, 999);
|
|
73
|
+
return end;
|
|
74
|
+
};
|
|
75
|
+
export const eachDayOfInterval = interval => {
|
|
76
|
+
const days = [];
|
|
77
|
+
const currentDate = new Date(interval.start);
|
|
78
|
+
while (currentDate <= interval.end) {
|
|
79
|
+
days.push(new Date(currentDate));
|
|
80
|
+
currentDate.setDate(currentDate.getDate() + 1);
|
|
81
|
+
}
|
|
82
|
+
return days;
|
|
83
|
+
};
|
|
84
|
+
export const addDays = (date, days) => {
|
|
85
|
+
const result = new Date(date);
|
|
86
|
+
result.setDate(date.getDate() + days);
|
|
87
|
+
return result;
|
|
88
|
+
};
|
|
89
|
+
//# sourceMappingURL=date.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date.js","names":["isToday","date","today","Date","toDateString","isTomorrow","tomorrow","setDate","getDate","isYesterday","yesterday","isCurrentYear","currentYear","getFullYear","yearOfGivenDate","getIsDateNearToday","setHours","targetDate","diffInDays","getTime","isMorning","hours","getHours","isAfter","firstDate","secondDate","isBefore","startOfMonth","getMonth","addYears","years","differenceInCalendarMonths","isSameDay","isSameMonth","isWithinInterval","interval","start","end","subYears","startOfWeek","day","getDay","diff","endOfWeek","eachDayOfInterval","days","currentDate","push","addDays","result"],"sources":["../../../src/utils/date.ts"],"sourcesContent":["export const isToday = (date: Date): boolean => {\n const today = new Date();\n return today.toDateString() === date.toDateString();\n};\n\nexport const isTomorrow = (date: Date): boolean => {\n const tomorrow = new Date();\n tomorrow.setDate(tomorrow.getDate() + 1);\n return tomorrow.toDateString() === date.toDateString();\n};\n\nexport const isYesterday = (date: Date): boolean => {\n const yesterday = new Date();\n yesterday.setDate(yesterday.getDate() - 1);\n return yesterday.toDateString() === date.toDateString();\n};\n\nexport const isCurrentYear = (date: Date): boolean => {\n const currentYear = new Date().getFullYear();\n const yearOfGivenDate = date.getFullYear();\n return currentYear === yearOfGivenDate;\n};\n\nexport const getIsDateNearToday = (date: Date) => {\n const today = new Date();\n today.setHours(0, 0, 0, 0);\n\n const targetDate = new Date(date);\n targetDate.setHours(0, 0, 0, 0);\n\n const diffInDays = (targetDate.getTime() - today.getTime()) / (1000 * 60 * 60 * 24);\n\n return diffInDays === 0 || diffInDays === -1 || diffInDays === 1;\n};\n\nexport const isMorning = (date: Date) => {\n const hours = date.getHours();\n\n return hours >= 0 && hours < 12;\n};\n\nexport const isAfter = (firstDate: Date, secondDate: Date): boolean => {\n return firstDate.getTime() > secondDate.getTime();\n}\n\nexport const isBefore = (firstDate: Date, secondDate: Date): boolean => {\n return firstDate.getTime() < secondDate.getTime();\n}\n\nexport const startOfMonth = (date: Date): Date => {\n return new Date(date.getFullYear(), date.getMonth(), 1);\n}\n\nexport const addYears = (date: Date, years: number): Date => {\n return new Date(date.getFullYear() + years, date.getMonth(), date.getDate());\n};\n\nexport const differenceInCalendarMonths = (firstDate: Date, secondDate: Date): number => {\n return (firstDate.getFullYear() - secondDate.getFullYear()) * 12 + (firstDate.getMonth() - secondDate.getMonth());\n};\n\nexport const isSameDay = (firstDate: Date, secondDate: Date): boolean => {\n return (\n firstDate.getFullYear() === secondDate.getFullYear() &&\n firstDate.getMonth() === secondDate.getMonth() &&\n firstDate.getDate() === secondDate.getDate()\n );\n};\n\nexport const isSameMonth = (firstDate: Date, secondDate: Date): boolean => {\n return (\n firstDate.getFullYear() === secondDate.getFullYear() &&\n firstDate.getMonth() === secondDate.getMonth()\n );\n};\n\ninterface Interval {\n start: Date;\n end: Date;\n}\n\nexport const isWithinInterval = (date: Date, interval: Interval): boolean => {\n return date.getTime() >= interval.start.getTime() && date.getTime() <= interval.end.getTime();\n};\n\nexport const subYears = (date: Date, years: number): Date => {\n return new Date(date.getFullYear() - years, date.getMonth(), date.getDate());\n};\n\nexport const startOfWeek = (date: Date): Date => {\n const day = date.getDay();\n const diff = day === 0 ? -6 : 1 - day;\n const start = new Date(date);\n start.setDate(date.getDate() + diff);\n start.setHours(0, 0, 0, 0);\n return start;\n};\n\nexport const endOfWeek = (date: Date): Date => {\n const day = date.getDay();\n const diff = day === 0 ? 0 : 7 - day;\n const end = new Date(date);\n end.setDate(date.getDate() + diff);\n end.setHours(23, 59, 59, 999);\n return end;\n};\n\nexport const eachDayOfInterval = (interval: { start: Date, end: Date }): Date[] => {\n const days: Date[] = [];\n const currentDate = new Date(interval.start);\n\n while (currentDate <= interval.end) {\n days.push(new Date(currentDate));\n currentDate.setDate(currentDate.getDate() + 1);\n }\n\n return days;\n};\n\nexport const addDays = (date: Date, days: number): Date => {\n const result = new Date(date);\n result.setDate(date.getDate() + days);\n return result;\n};\n"],"mappings":"AAAA,OAAO,MAAMA,OAAO,GAAIC,IAAU,IAAc;EAC5C,MAAMC,KAAK,GAAG,IAAIC,IAAI,CAAC,CAAC;EACxB,OAAOD,KAAK,CAACE,YAAY,CAAC,CAAC,KAAKH,IAAI,CAACG,YAAY,CAAC,CAAC;AACvD,CAAC;AAED,OAAO,MAAMC,UAAU,GAAIJ,IAAU,IAAc;EAC/C,MAAMK,QAAQ,GAAG,IAAIH,IAAI,CAAC,CAAC;EAC3BG,QAAQ,CAACC,OAAO,CAACD,QAAQ,CAACE,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC;EACxC,OAAOF,QAAQ,CAACF,YAAY,CAAC,CAAC,KAAKH,IAAI,CAACG,YAAY,CAAC,CAAC;AAC1D,CAAC;AAED,OAAO,MAAMK,WAAW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