perf: Improve getAggregatedAvailability by reducing reliance on Day.js (#11224)
parent
f25f8e0909
commit
3132c64205
|
@ -22,25 +22,33 @@ export const getAggregatedAvailability = (
|
||||||
return mergeOverlappingDateRanges(availability);
|
return mergeOverlappingDateRanges(availability);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function isSameDay(date1: Date, date2: Date) {
|
||||||
|
return (
|
||||||
|
date1.getUTCFullYear() === date2.getUTCFullYear() &&
|
||||||
|
date1.getUTCMonth() === date2.getUTCMonth() &&
|
||||||
|
date1.getUTCDate() === date2.getUTCDate()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function mergeOverlappingDateRanges(dateRanges: DateRange[]) {
|
function mergeOverlappingDateRanges(dateRanges: DateRange[]) {
|
||||||
const sortedDateRanges = dateRanges.sort((a, b) => a.start.diff(b.start)); //is it already sorted before?
|
dateRanges.sort((a, b) => a.start.valueOf() - b.start.valueOf());
|
||||||
|
|
||||||
const mergedDateRanges: DateRange[] = [];
|
const mergedDateRanges: DateRange[] = [];
|
||||||
|
|
||||||
let currentRange = sortedDateRanges[0];
|
let currentRange = dateRanges[0];
|
||||||
if (!currentRange) {
|
if (!currentRange) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 1; i < sortedDateRanges.length; i++) {
|
for (let i = 1; i < dateRanges.length; i++) {
|
||||||
const nextRange = sortedDateRanges[i];
|
const nextRange = dateRanges[i];
|
||||||
if (
|
if (
|
||||||
currentRange.start.utc().format("DD MM YY") === nextRange.start.utc().format("DD MM YY") &&
|
isSameDay(currentRange.start.toDate(), nextRange.start.toDate()) &&
|
||||||
currentRange.end.isAfter(nextRange.start)
|
currentRange.end.valueOf() > nextRange.start.valueOf()
|
||||||
) {
|
) {
|
||||||
currentRange = {
|
currentRange = {
|
||||||
start: currentRange.start,
|
start: currentRange.start,
|
||||||
end: currentRange.end.isAfter(nextRange.end) ? currentRange.end : nextRange.end,
|
end: currentRange.end.valueOf() > nextRange.end.valueOf() ? currentRange.end : nextRange.end,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
mergedDateRanges.push(currentRange);
|
mergedDateRanges.push(currentRange);
|
||||||
|
|
Loading…
Reference in New Issue