All files Planning.js

100% Statements 68/68
92.31% Branches 36/39
100% Functions 11/11
100% Lines 65/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244                                  1x                 1x                     19x 19x 19x 17x   2x                     196x 192x   4x                     271x                         62x 62x 44x 44x 44x 44x         44x             18x   62x                     189x 189x 189x 189x 189x 189x                                 7x 7x 7x   7x 2x   2x     1x   1x   5x 1x       7x                         12x 10x         2x                       3x 3x 3x 3x 185x   185x 185x   3x                             1x 1x 5x 5x 5x 5x 5x       1x                       9x 4x   5x 6x 2x 2x 4x 3x 3x            
// @flow
 
export type eventObject = {
    id: number,
    title: string,
    logo: string,
    date_begin: string,
    date_end: string,
    description: string,
    club: string,
    category_id: number,
    url: string,
};
 
export default class PlanningEventManager {
 
    // Regex used to check date string validity
    static dateRegExp = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/;
 
    /**
     * Gets the current day string representation in the format
     * YYYY-MM-DD
     *
     * @return {string} The string representation
     */
    static getCurrentDateString(): string {
        return PlanningEventManager.dateToString(new Date(Date.now()));
    }
 
    /**
     * Checks if the given date is before the other.
     *
     * @param event1Date Event 1 date in format YYYY-MM-DD HH:MM
     * @param event2Date Event 2 date in format YYYY-MM-DD HH:MM
     * @return {boolean}
     */
    static isEventBefore(event1Date: string, event2Date: string): boolean {
        let date1 = PlanningEventManager.stringToDate(event1Date);
        let date2 = PlanningEventManager.stringToDate(event2Date);
        if (date1 !== null && date2 !== null)
            return date1 < date2;
        else
            return false;
    }
 
    /**
     * Gets only the date part of the given event date string in the format
     * YYYY-MM-DD HH:MM
     *
     * @param dateString The string to get the date from
     * @return {string|null} Date in format YYYY:MM:DD or null if given string is invalid
     */
    static getDateOnlyString(dateString: string): string | null {
        if (PlanningEventManager.isEventDateStringFormatValid(dateString))
            return dateString.split(" ")[0];
        else
            return null;
    }
 
    /**
     * Checks if the given date string is in the format
     * YYYY-MM-DD HH:MM
     *
     * @param dateString The string to check
     * @return {boolean}
     */
    static isEventDateStringFormatValid(dateString: ?string): boolean {
        return dateString !== undefined
            && dateString !== null
            && PlanningEventManager.dateRegExp.test(dateString);
    }
 
    /**
     * Converts the given date string to a date object.<br>
     * Accepted format: YYYY-MM-DD HH:MM
     *
     * @param dateString The string to convert
     * @return {Date|null} The date object or null if the given string is invalid
     */
    static stringToDate(dateString: string): Date | null {
        let date = new Date();
        if (PlanningEventManager.isEventDateStringFormatValid(dateString)) {
            let stringArray = dateString.split(' ');
            let dateArray = stringArray[0].split('-');
            let timeArray = stringArray[1].split(':');
            date.setFullYear(
                parseInt(dateArray[0]),
                parseInt(dateArray[1]) - 1, // Month range from 0 to 11
                parseInt(dateArray[2])
            );
            date.setHours(
                parseInt(timeArray[0]),
                parseInt(timeArray[1]),
                0,
                0,
            );
        } else
            date = null;
 
        return date;
    }
 
    /**
     * Converts a date object to a string in the format
     * YYYY-MM-DD HH-MM-SS
     *
     * @param date The date object to convert
     * @return {string} The converted string
     */
    static dateToString(date: Date): string {
        const day = String(date.getDate()).padStart(2, '0');
        const month = String(date.getMonth() + 1).padStart(2, '0'); //January is 0!
        const year = date.getFullYear();
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes;
    }
 
    /**
     * Returns a string corresponding to the event start and end times in the following format:
     *
     * HH:MM - HH:MM
     *
     * If the end date is not specified or is equal to start time, only start time will be shown.
     *
     * If the end date is not on the same day, 23:59 will be shown as end time
     *
     * @param start Start time in YYYY-MM-DD HH:MM:SS format
     * @param end End time in YYYY-MM-DD HH:MM:SS format
     * @return {string} Formatted string or "/ - /" on error
     */
    static getFormattedEventTime(start: string, end: string): string {
        let formattedStr = '/ - /';
        let startDate = PlanningEventManager.stringToDate(start);
        let endDate = PlanningEventManager.stringToDate(end);
 
        if (startDate !== null && endDate !== null && startDate.getTime() !== endDate.getTime()) {
            formattedStr = String(startDate.getHours()).padStart(2, '0') + ':'
                + String(startDate.getMinutes()).padStart(2, '0') + ' - ';
            if (endDate.getFullYear() > startDate.getFullYear()
                || endDate.getMonth() > startDate.getMonth()
                || endDate.getDate() > startDate.getDate())
                formattedStr += '23:59';
            else
                formattedStr += String(endDate.getHours()).padStart(2, '0') + ':'
                    + String(endDate.getMinutes()).padStart(2, '0');
        } else if (startDate !== null)
            formattedStr =
                String(startDate.getHours()).padStart(2, '0') + ':'
                + String(startDate.getMinutes()).padStart(2, '0');
 
        return formattedStr
    }
 
    /**
     * Checks if the given description can be considered empty.
     * <br>
     * An empty description is composed only of whitespace, <b>br</b> or <b>p</b> tags
     *
     *
     * @param description The text to check
     * @return {boolean}
     */
    static isDescriptionEmpty(description: ?string): boolean {
        if (description !== undefined && description !== null) {
            return description
                .split('<p>').join('') // Equivalent to a replace all
                .split('</p>').join('')
                .split('<br>').join('').trim() === '';
        } else
            return true;
    }
 
    /**
     * Generates an object with an empty array for each key.
     * Each key is a date string in the format
     * YYYY-MM-DD
     *
     * @param numberOfMonths The number of months to create, starting from the current date
     * @return {Object}
     */
    static generateEmptyCalendar(numberOfMonths: number): Object {
        let end = new Date(Date.now());
        end.setMonth(end.getMonth() + numberOfMonths);
        let daysOfYear = {};
        for (let d = new Date(Date.now()); d <= end; d.setDate(d.getDate() + 1)) {
            const dateString = PlanningEventManager.getDateOnlyString(
                PlanningEventManager.dateToString(new Date(d)));
            Eif (dateString !== null)
                daysOfYear[dateString] = []
        }
        return daysOfYear;
    }
 
    /**
     * Generates an object with an array of eventObject at each key.
     * Each key is a date string in the format
     * YYYY-MM-DD.
     *
     * If no event is available at the given key, the array will be empty
     *
     * @param eventList The list of events to map to the agenda
     * @param numberOfMonths The number of months to create the agenda for
     * @return {Object}
     */
    static generateEventAgenda(eventList: Array<eventObject>, numberOfMonths: number): Object {
        let agendaItems = PlanningEventManager.generateEmptyCalendar(numberOfMonths);
        for (let i = 0; i < eventList.length; i++) {
            const dateString = PlanningEventManager.getDateOnlyString(eventList[i].date_begin);
            Eif (dateString !== null) {
                const eventArray = agendaItems[dateString];
                Eif (eventArray !== undefined)
                    this.pushEventInOrder(eventArray, eventList[i]);
            }
 
        }
        return agendaItems;
    }
 
    /**
     * Adds events to the given array depending on their starting date.
     *
     * Events starting before are added at the front.
     *
     * @param eventArray The array to hold sorted events
     * @param event The event to add to the array
     */
    static pushEventInOrder(eventArray: Array<eventObject>, event: eventObject): Object {
        if (eventArray.length === 0)
            eventArray.push(event);
        else {
            for (let i = 0; i < eventArray.length; i++) {
                if (PlanningEventManager.isEventBefore(event.date_begin, eventArray[i].date_begin)) {
                    eventArray.splice(i, 0, event);
                    break;
                } else if (i === eventArray.length - 1) {
                    eventArray.push(event);
                    break;
                }
            }
        }
    }
}