Hong-Phuc Bui
2025-01-16 2889de7f0c2d587a17fbd322af57c29e84238620
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* 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;
}