Improved JSdoc

This commit is contained in:
Arnaud Vergnet 2020-03-22 12:15:16 +01:00
parent c6fb369863
commit bf0c81166c

View file

@ -34,7 +34,7 @@ export default class PlanningEventManager {
* @param event2Date Event 2 date in format YYYY-MM-DD HH:MM * @param event2Date Event 2 date in format YYYY-MM-DD HH:MM
* @return {boolean} * @return {boolean}
*/ */
static isEventBefore(event1Date: string, event2Date: string) { static isEventBefore(event1Date: string, event2Date: string): boolean {
let date1 = PlanningEventManager.stringToDate(event1Date); let date1 = PlanningEventManager.stringToDate(event1Date);
let date2 = PlanningEventManager.stringToDate(event2Date); let date2 = PlanningEventManager.stringToDate(event2Date);
if (date1 !== null && date2 !== null) if (date1 !== null && date2 !== null)
@ -50,7 +50,7 @@ export default class PlanningEventManager {
* @param dateString The string to get the date from * @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 * @return {string|null} Date in format YYYY:MM:DD or null if given string is invalid
*/ */
static getDateOnlyString(dateString: string) { static getDateOnlyString(dateString: string): string | null {
if (PlanningEventManager.isEventDateStringFormatValid(dateString)) if (PlanningEventManager.isEventDateStringFormatValid(dateString))
return dateString.split(" ")[0]; return dateString.split(" ")[0];
else else
@ -64,7 +64,7 @@ export default class PlanningEventManager {
* @param dateString The string to check * @param dateString The string to check
* @return {boolean} * @return {boolean}
*/ */
static isEventDateStringFormatValid(dateString: ?string) { static isEventDateStringFormatValid(dateString: ?string): boolean {
return dateString !== undefined return dateString !== undefined
&& dateString !== null && dateString !== null
&& PlanningEventManager.dateRegExp.test(dateString); && PlanningEventManager.dateRegExp.test(dateString);
@ -107,7 +107,7 @@ export default class PlanningEventManager {
* @param date The date object to convert * @param date The date object to convert
* @return {string} The converted string * @return {string} The converted string
*/ */
static dateToString(date: Date) { static dateToString(date: Date): string {
const day = String(date.getDate()).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0');
const month = String(date.getMonth() + 1).padStart(2, '0'); //January is 0! const month = String(date.getMonth() + 1).padStart(2, '0'); //January is 0!
const year = date.getFullYear(); const year = date.getFullYear();
@ -171,7 +171,15 @@ export default class PlanningEventManager {
return true; return true;
} }
static generateEmptyCalendar(numberOfMonths: number) { /**
* 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 {
const end = new Date(new Date().setMonth(new Date().getMonth() + numberOfMonths + 1)); const end = new Date(new Date().setMonth(new Date().getMonth() + numberOfMonths + 1));
let daysOfYear = {}; let daysOfYear = {};
for (let d = new Date(); d <= end; d.setDate(d.getDate() + 1)) { for (let d = new Date(); d <= end; d.setDate(d.getDate() + 1)) {
@ -183,33 +191,52 @@ export default class PlanningEventManager {
return daysOfYear; return daysOfYear;
} }
static generateEventAgenda(eventList: Array<eventObject>, numberOfMonths: number) { /**
* 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); let agendaItems = PlanningEventManager.generateEmptyCalendar(numberOfMonths);
for (let i = 0; i < eventList.length; i++) { for (let i = 0; i < eventList.length; i++) {
const dateString = PlanningEventManager.getDateOnlyString(eventList[i].date_begin); const dateString = PlanningEventManager.getDateOnlyString(eventList[i].date_begin);
console.log(dateString); if (dateString !== null) {
if (dateString !== null) const eventArray = agendaItems[dateString];
this.pushEventInOrder(agendaItems, eventList[i], dateString); if (eventArray !== undefined)
this.pushEventInOrder(eventArray, eventList[i]);
}
} }
return agendaItems; return agendaItems;
} }
static pushEventInOrder(agendaItems: Object, event: eventObject, startDate: string) { /**
if (agendaItems[startDate] !== undefined) { * Adds events to the given array depending on their starting date.
if (agendaItems[startDate].length === 0) *
agendaItems[startDate].push(event); * 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 { else {
for (let i = 0; i < agendaItems[startDate].length; i++) { for (let i = 0; i < eventArray.length; i++) {
if (PlanningEventManager.isEventBefore(event.date_begin, agendaItems[startDate][i].date_end)) { if (PlanningEventManager.isEventBefore(event.date_begin, eventArray[i].date_end)) {
agendaItems[startDate].splice(i, 0, event); eventArray.splice(i, 0, event);
break; break;
} else if (i === agendaItems[startDate].length - 1) { } else if (i === eventArray.length - 1) {
agendaItems[startDate].push(event); eventArray.push(event);
break; break;
} }
} }
} }
} }
}
} }