fromis_9/frontend-temp/src/utils/schedule.js

123 lines
3.3 KiB
JavaScript
Raw Normal View History

/**
* 스케줄 관련 유틸리티 함수
*/
/**
* 스케줄에서 카테고리 ID 추출
* 검색 결과와 일반 데이터의 형식 차이를 처리
* @param {object} schedule - 스케줄 객체
* @returns {number|null} 카테고리 ID
*/
export function getCategoryId(schedule) {
return schedule.category?.id ?? schedule.category_id ?? schedule.categoryId ?? null;
}
/**
* 스케줄에서 카테고리 정보 추출
* @param {object} schedule - 스케줄 객체
* @returns {{ id: number, name: string, color: string }}
*/
export function getCategoryInfo(schedule) {
return {
id: getCategoryId(schedule),
name: schedule.category?.name ?? schedule.category_name ?? schedule.categoryName ?? '미분류',
color: schedule.category?.color ?? schedule.category_color ?? schedule.categoryColor ?? '#9CA3AF',
};
}
/**
* 스케줄에서 날짜 추출
* datetime 형식과 date 형식 모두 처리
* @param {object} schedule - 스케줄 객체
* @returns {string} YYYY-MM-DD 형식 날짜
*/
export function getScheduleDate(schedule) {
const datetime = schedule.datetime || schedule.date;
if (!datetime) return '';
return datetime.split(' ')[0].split('T')[0];
}
/**
* 스케줄에서 시간 추출
* @param {object} schedule - 스케줄 객체
* @returns {string|null} HH:mm 형식 시간 또는 null
*/
export function getScheduleTime(schedule) {
if (schedule.time) {
return schedule.time.slice(0, 5);
}
const datetime = schedule.datetime;
if (datetime && (datetime.includes(' ') || datetime.includes('T'))) {
const timePart = datetime.includes(' ')
? datetime.split(' ')[1]
: datetime.split('T')[1];
return timePart?.slice(0, 5) || null;
}
return null;
}
/**
* 스케줄에서 멤버 목록 추출
* 다양한 형식 처리 (문자열 배열, 객체 배열)
* @param {object} schedule - 스케줄 객체
* @returns {Array<{ id: number, name: string }>}
*/
export function getMemberList(schedule) {
const members = schedule.members || [];
if (members.length === 0) return [];
// 문자열 배열인 경우 (검색 결과)
if (typeof members[0] === 'string') {
return members.map((name, idx) => ({ id: idx, name }));
}
// 객체 배열인 경우
return members;
}
/**
* 생일 스케줄인지 확인
* @param {object} schedule - 스케줄 객체
* @returns {boolean}
*/
export function isBirthdaySchedule(schedule) {
const title = schedule.title || '';
return title.includes('생일') || title.includes('birthday');
}
/**
* 날짜별로 스케줄 그룹화
* @param {Array} schedules - 스케줄 배열
* @returns {Map<string, Array>} 날짜별 그룹화된
*/
export function groupSchedulesByDate(schedules) {
const groups = new Map();
for (const schedule of schedules) {
const date = getScheduleDate(schedule);
if (!groups.has(date)) {
groups.set(date, []);
}
groups.get(date).push(schedule);
}
return groups;
}
/**
* 카테고리별 스케줄 계산
* @param {Array} schedules - 스케줄 배열
* @returns {Map<number, number>} 카테고리 ID별 개수
*/
export function countByCategory(schedules) {
const counts = new Map();
for (const schedule of schedules) {
const categoryId = getCategoryId(schedule);
counts.set(categoryId, (counts.get(categoryId) || 0) + 1);
}
return counts;
}