Hong-Phuc Bui
2024-10-16 f8613c9ce2bd4b74b11727d2eae204f49151bcba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* 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;
}