/* use function style */ /** * create a calendar view as ASCII-String, suitable for console program * or for debug. * * @param {object} calendar an object with structure like * ```{ * (TODO: Doc) * }``` * @param {object} locationCode * * @return {string} ASCII-String represents a month in a calendar. * */ export function asciiCalendar(calendar, locationCode) { let dayNames = locationCode["weekDays"]; const month = locationCode["months"][calendar["month"]], year = calendar["year"]; let view = "\n\n"; for (let day of calendar["weekDays"]) { view += dayNames[day] + ' '; } view += "\n"; let title = `${month} ${year}`; let width = view.length - 4; let padWidth = (width - title.length)/2|0; // only integer part title = ' '.repeat(padWidth)+title; view = `${title}${view}` let week = ""; let dayWidth = `${dayNames[0]}`.length; for(let w of calendar["weeks"]) { for(let d of w) { let day = `${d}`; if (day.length === 0) { day = ' '.repeat(dayWidth); } else if (day.length === 1) { day = `0${day}`; } day = ' '.repeat(dayWidth - day.length) + day; week += (day + ' '); } week += "\n"; } return view + week; } export function domCalendar(calendar, location) { const month = location.months[calendar.month], year = calendar.year; let htmlSegment = ``; const dayNames = location["weekDays"]; for(let d of calendar.weekDays) { htmlSegment += `` } htmlSegment += "\n\n"; for(let w of calendar.weeks) { htmlSegment += ""; for(let d of w) { htmlSegment += ``; } htmlSegment += "\n"; } htmlSegment += "
${month} ${year}
${dayNames[d]}
${d}
"; return htmlSegment; } export function verticalCalendar(calendar, location){ const days = [ [], [], [], [], [], [], [] ]; /* 0 -> 1 1 -> 2 2 -> 3 3 -> 4 4 -> 5 5 -> 6 6 -> 0 * */ for(const [idx,day] of days.entries() ) { const idxOfWeekdays = (idx + location.firstDayOfWeek) % 7; const dayName = location.weekDays[idxOfWeekdays]; day.push(`${dayName}`); } for(const week of calendar.weeks) { for(const [idx, day] of week.entries() ) { days[idx].push(`${day}`); } } let cal = "\n"; for(const day of days) { cal += "" + day.join("") + "\n" } cal += "
"; return cal; }