Skip to content

Commit

Permalink
fix: оптимизация обработки отсутствующих данных и улучшение isWorkingDay
Browse files Browse the repository at this point in the history
- Исправлена уязвимость при отсутствии данных для праздников и сокращенных дней.
- Оптимизированы функции getHolidays, getShortDays и getWorkingHolidays: вызовы производятся один раз.
- Добавлена безопасная обработка отсутствующих данных (пустые массивы по умолчанию).
- Улучшена производительность функции isWorkingDay.
  • Loading branch information
iposho committed Dec 19, 2024
1 parent f5b3ce1 commit 999eb29
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/helpers/countShortDays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ export const countShortDays = (year: number, month: number): number => {
let count = 0;
const date = new Date(Date.UTC(year, month, 1));

const shortDays = getShortDays(year) || [];

while (date.getUTCMonth() === month) {
if (getShortDays(year).some((e) => new Date(e.date).valueOf() === date.valueOf())) {
if (shortDays.some((e) => new Date(e.date).valueOf() === date.valueOf())) {
count++;
}
date.setUTCDate(date.getUTCDate() + 1);
Expand Down
7 changes: 5 additions & 2 deletions src/helpers/countWorkingDays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ export const countWorkingDays = (year: number, month: number): number => {
let count = 0;
const date = new Date(Date.UTC(year, month, 1));

const holidays = getHolidays(year) || [];
const workingHolidays = getWorkingHolidays(year) || [];

while (date.getUTCMonth() === month) {
const isWeekend = date.getUTCDay() === 0 || date.getUTCDay() === 6;
const isHoliday = getHolidays(year).some((e) => new Date(e.date).valueOf() === date.valueOf());
const isWorkingHoliday = getWorkingHolidays(year).some((e) => new Date(e.date).valueOf() === date.valueOf());
const isHoliday = holidays.some((e) => new Date(e.date).valueOf() === date.valueOf());
const isWorkingHoliday = workingHolidays.some((e) => new Date(e.date).valueOf() === date.valueOf());

if ((!isWeekend && !isHoliday) || (isWeekend && isWorkingHoliday)) {
count++;
Expand Down
19 changes: 12 additions & 7 deletions src/helpers/isWorkingDay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const isWeekend = (date: Date): boolean => {
return day === 0 || day === 6;
};

const isWeekendWorking = (date: Date): boolean => {
const isWeekendWorking = (date: Date, workingHolidays: { date: string }[]): boolean => {
const day = date.getUTCDay();
return day === 6 && getWorkingHolidays(date.getUTCFullYear()).some(
return day === 6 && workingHolidays.some(
(e) => new Date(e.date).valueOf() === date.valueOf(),
);
};
Expand All @@ -28,8 +28,13 @@ export const isWorkingDay = (year: number, month: number, day: number): WorkingD
const date = new Date(Date.UTC(year, month - 1, day));
const monthName = new Intl.DateTimeFormat('en-US', { month: 'long' }).format(date);

const isHoliday = getHolidays(year).some((e) => new Date(e.date).valueOf() === date.valueOf());
const isShortDay = getShortDays(year).some((e) => new Date(e.date).valueOf() === date.valueOf());
const holidays = getHolidays(year) || [];
const shortDays = getShortDays(year) || [];
const workingHolidays = getWorkingHolidays(year) || [];

const isHoliday = holidays.some((e) => new Date(e.date).valueOf() === date.valueOf());
const isShortDay = shortDays.some((e) => new Date(e.date).valueOf() === date.valueOf());
const isWorkingHoliday = isWeekendWorking(date, workingHolidays);

const result: WorkingDayResult = {
year: Number(year),
Expand All @@ -38,16 +43,16 @@ export const isWorkingDay = (year: number, month: number, day: number): WorkingD
id: month - 1,
},
date,
isWorkingDay: !isHoliday && (isShortDay || (!isWeekend(date) || isWeekendWorking(date))),
isWorkingDay: !isHoliday && (!isWeekend(date) || isWorkingHoliday),
};

if (isHoliday) {
const holiday = getHolidays(year).find((el) => new Date(el.date).valueOf() === date.valueOf());
const holiday = holidays.find((el) => new Date(el.date).valueOf() === date.valueOf());
if (holiday) result.holiday = holiday.name;
}

if (isShortDay) {
const shortDay = getShortDays(year).find((el) => new Date(el.date).valueOf() === date.valueOf());
const shortDay = shortDays.find((el) => new Date(el.date).valueOf() === date.valueOf());
if (shortDay) {
result.isShortDay = true;
result.holiday = shortDay.name;
Expand Down

0 comments on commit 999eb29

Please sign in to comment.