6-1. 함수 추출하기
적용 시점
절차
예시
❌ Before
export const getCalendarInfo = ({
year,
month,
futureDate,
pastDate,
selectedButtonOrder,
}: {
year: number;
month: number;
futureDate: string;
pastDate: string;
selectedButtonOrder: number;
}) => {
const firstDay = new Date(year, month - 1, 1).getDay();
const lastDate = new Date(year, month, 0).getDate();
const data: { available: boolean; date: string | number; backgroundPoint: boolean }[][] = [];
let nowDate = 1;
let nowDay = 0;
let weekIdx = 0;
while (nowDate <= lastDate) {
if (nowDay === 0) {
data.push([]);
}
if (weekIdx === 0) {
if (nowDay < firstDay) {
data[weekIdx].push({ available: false, date: '', backgroundPoint: false });
nowDay += 1;
continue;
}
}
const nowDateToTime = new Date(`${year}-${month < 10 ? 0 : ''}${month}-${nowDate < 10 ? '0' : ''}${nowDate}`).getTime();
const pastDateToTime = new Date(pastDate).getTime();
const futureDateToTime = new Date(futureDate).getTime();
const dueDateToTime = new Date().getTime();
data[weekIdx].push({
available: getClickAvailable({ selectedButtonOrder, dueDateToTime, nowDateToTime }),
date: nowDate,
backgroundPoint: pastDateToTime <= nowDateToTime && nowDateToTime <= futureDateToTime,
});
nowDate += 1;
nowDay += 1;
if (nowDay === 7) {
nowDay = 0;
weekIdx += 1;
}
}
return data;
};⭕ After
Last updated