/* 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 = `<table> <caption>${month} ${year}</caption> <thead><tr>`; const dayNames = location["weekDays"]; for(let d of calendar.weekDays) { htmlSegment += `<td>${dayNames[d]}</td>` } htmlSegment += "</tr>\n</thead>\n"; for(let w of calendar.weeks) { htmlSegment += "<tr>"; for(let d of w) { htmlSegment += `<td>${d}</td>`; } htmlSegment += "</tr>\n"; } htmlSegment += "</table>"; 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(`<td>${dayName}</td>`); } for(const week of calendar.weeks) { for(const [idx, day] of week.entries() ) { days[idx].push(`<td>${day}</td>`); } } let cal = "<table>\n"; for(const day of days) { cal += "<tr>" + day.join("") + "</tr>\n" } cal += "</table>"; return cal; }