Skip to content

fix: updated calcualtions for showSixWeeks #2562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions src/dateutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,26 @@ export function weekDayNames(firstDayOfWeek = 0) {
return weekDaysNames;
}

export function page(date: XDate, firstDayOfWeek = 0, showSixWeeks = false) {
const days = month(date);
const calculateDays = (dates: XDate[], from: XDate, to: XDate) => {
let before: XDate[] = [];
let after: XDate[] = [];
const days = dates.map(day => day.clone());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this to insure immutability, I'm not sure if this is indeed needed tho

if (isLTE(from, days[0])) {
before = fromTo(from, days[0]);
}
if (isGTE(to, days[days.length - 1])) {
after = fromTo(days[days.length - 1], to);
}
return before.concat(days.slice(1, days.length - 1), after);
};

export function page(date: XDate, firstDayOfWeek = 0, showSixWeeks = false) {
const days = month(date);

const fdow = (7 + firstDayOfWeek) % 7 || 7;
const ldow = (fdow + 6) % 7;

firstDayOfWeek = firstDayOfWeek || 0;

const from = days[0].clone();
const daysBefore = from.getDay();

if (from.getDay() !== fdow) {
from.addDays(-(from.getDay() + 7 - fdow) % 7);
Expand All @@ -148,21 +156,17 @@ export function page(date: XDate, firstDayOfWeek = 0, showSixWeeks = false) {
to.addDays((ldow + 7 - day) % 7);
}

const daysForSixWeeks = (daysBefore + days.length) / 6 >= 6;
const daysOnPage = calculateDays(days, from, to).length;
const daysForSixWeeks = daysOnPage / 7 >= 6;

if (showSixWeeks && !daysForSixWeeks) {
if (42 - daysOnPage > 7) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for months with 28 days (5 weeks) (Feb 2026) => add 7 days before
Otherwise there's situation where 2 weeks from next month are displayed at the end and it just doesnt look right

If it bothers you we can just do to.addDays(42 - daysOnPage) but it's uglier in my opinion

Copy link
Author

@MaksymA26 MaksymA26 Nov 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

42 = 6weeks * 7days
Should I move it to variable or you can understand from the context what it is ?

from.addDays(-7);
}
to.addDays(7);
}

if (isLTE(from, days[0])) {
before = fromTo(from, days[0]);
}

if (isGTE(to, days[days.length - 1])) {
after = fromTo(days[days.length - 1], to);
}

return before.concat(days.slice(1, days.length - 1), after);
return calculateDays(days, from, to);
}

export function isDateNotInRange(date: XDate, minDate: string, maxDate: string) {
Expand Down