-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
230 lines (199 loc) · 7.52 KB
/
index.js
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
const fedHolidays = require("@18f/us-federal-holidays")
const isAHoliday = fedHolidays.isAHoliday;
const endOfYear = require("date-fns/endOfYear");
const eachDayOfInterval = require("date-fns/eachDayOfInterval");
const min = require("date-fns/min");
const max = require("date-fns/max");
const isSameDay = require("date-fns/isSameDay");
const isSameMonth = require("date-fns/isSameMonth");
const isSameYear = require("date-fns/isSameYear");
const differenceInCalendarDays = require('date-fns/differenceInCalendarDays')
const format = require('date-fns/format');
const compareAsc = require('date-fns/compareAsc');
const chalk = require("chalk");
const process = require('process');
const resetTerminalCursor = () => {
process.stdout.write("\033[1;1H");
};
const clearScreen = () => {
process.stdout.write("\033[2J")
process.stdout.write("\033[2;1H");
};
const areDatesEqual = (date1, date2) => {
return isSameDay(date1, date2) && isSameMonth(date1, date2) && isSameYear(date1, date2);
}
const isDateInList = (date, dateList) => {
for(i = 0; i < dateList.length; i++) {
if(areDatesEqual(date, dateList[i])) {
return true;
}
}
return false;
}
const intervalForYear = year => {
return {
startDate: new Date(year, 0, 1),
endDate: new Date(year, 11, 31)
}
}
const randomDate = (start, end) => {
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}
const randomDateInterval = (start, end) => {
const startDate = randomDate(start, end)
const endDate = randomDate(startDate, end)
return {
startDate: startDate,
endDate: endDate
}
}
const generateStats = schedule => {
let stats = {
averageDaysBetweenDates: -1,
maxDaysBetweenDates: -1,
minDaysBetweenDates: -1,
distribution: {}
}
stats.averageDaysBetweenDates = differenceInCalendarDays(schedule.endDate, schedule.startDate) / schedule.scheduledDates.length;
schedule.scheduledDates = schedule.scheduledDates.sort(compareAsc);
for(i = 0; i < schedule.scheduledDates.length - 1; i++) {
const difference = differenceInCalendarDays(
schedule.scheduledDates[i+1],
schedule.scheduledDates[i]
)
if(!stats.distribution) {
stats.distribution = { [difference]: 1 }
} else if(Object.keys(stats.distribution).includes(difference.toString())) {
stats.distribution[difference] = stats.distribution[difference] + 1
} else if(!Object.keys(stats.distribution).includes(difference.toString())) {
stats.distribution[difference] = 1
}
if(i == 0) {
stats.minDaysBetweenDates = difference
stats.maxDaysBetweenDates = difference
} else {
if(stats.minDaysBetweenDates > difference) {
stats.minDaysBetweenDates = difference;
}
if(stats.maxDaysBetweenDates < difference) {
stats.maxDaysBetweenDates = difference;
}
}
}
return stats;
}
/**
Params:
year, Integer: The year to generate dates for
schedule, [Date]: An array of dates that you want to schedule
checkIfDateIsAcceptable, Function: A function that determines
whether a day is available for scheduling or not.
Blue Days - Unscheduled Acceptable days
Red Days - Unscheduled Unacceptable days
Green Days - Scheduled Acceptable days
Yellow Days - Scheduled Unacceptable days
Returns an array of colored lines that can be used to visualize
what a schedule looks like
*/
const generateOutputForYear = (year, schedule, checkIfDateIsAcceptable) => {
const {startDate, endDate} = intervalForYear(year);
const datesInYear = eachDayOfInterval({start: startDate, end: endDate})
const scheduleStartDate = schedule.startDate;
const scheduleEndDate = schedule.endDate;
return datesInYear.map( date => {
let coloredDay = "";
const isScheduledDay = isDateInList(date, schedule.scheduledDates);
if(checkIfDateIsAcceptable(date)) {
if(isScheduledDay) {
coloredDay += chalk.green('|')
} else {
coloredDay += chalk.blue('|')
}
} else {
if(isScheduledDay) {
coloredDay += chalk.yellow('|')
} else {
coloredDay += chalk.red('|')
}
}
if(areDatesEqual(date, scheduleStartDate)) {
coloredDay = chalk.magentaBright('[') + coloredDay
} else if(areDatesEqual(date, scheduleEndDate)) {
coloredDay = coloredDay + chalk.magentaBright(']')
}
return coloredDay;
})
};
/**
Prints out a visualization of applying your scheduler and
checkIfDateIsAcceptable functions to one or more calendar years.
Params:
years, [Integer]: An array of years that you want to visualize.
scheduler, Function: A function that will generate a schedule
checkIfDateIsAcceptable, Function: A function that determines
whether a day is available for scheduling or not.
*/
const displayVisualizations = (years, scheduler, checkIfDateIsAcceptable, timeoutPerIteration) => {
const visualizations = years.map((year, index) => {
return new Promise((resolve, reject) =>
setTimeout(
() => {
clearScreen();
const schedule = scheduler(year);
const stats = generateStats(schedule);
const infoOutput = `Iteration ${chalk.yellow(index+1)} of ${chalk.yellow(years.length)} -- ${schedule.scheduledDates.length} Scheduled Days on [${format(schedule.startDate, "yyyy-MM-dd")}, ${format(schedule.endDate, "yyyy-MM-dd")}]`
const visualizationOutput = `${generateOutputForYear(
year,
schedule,
checkIfDateIsAcceptable).join("")
}`
const statsOutput = `Scheduled Day Statistics:\n\t Avg. Between Dates:\t ${stats.averageDaysBetweenDates}\n\t Min. Days Between Dates: ${stats.minDaysBetweenDates}\n\t Max Days Between Dates: ${stats.maxDaysBetweenDates}`
const distributionOutput = (Object.keys(stats.distribution)).map((key) => {
let row = [`${key}: `]
for(let i = 0; i < stats.distribution[key]; i++) {
row.push(chalk.whiteBright('|'))
}
return row.join("");
}).join("\n")
process.stdout.write(visualizationOutput + "\n" + infoOutput + "\n\n" + statsOutput + "\n\n" + distributionOutput);
resolve();
},
timeoutPerIteration * index
)
)
});
Promise.all(visualizations).then( () => {resetTerminalCursor();} );
};
const years = [2020, 2021, 2022, 2023, 2024]
const timeoutPerIteration = 1000;
/*
Return true if a date should be open for scheduling.
Return false if a date should not be open for scheduling.
*/
const checkIfDateIsAcceptable = date => {
if(isAHoliday(date)) {
return false;
} else {
return true;
}
}
/**
Return [Date] for each day in the year that you want to schedule.
*/
const scheduler = year => {
// Generate random interval
const { startDate, endDate } = intervalForYear(year);
const interval = randomDateInterval(startDate, endDate);
const daysInInterval = differenceInCalendarDays(interval.endDate, interval.startDate)
const numberOfScheduledDates = Math.floor(Math.random() * daysInInterval) + 1;
let randomSchedule = {
startDate: interval.startDate,
endDate: interval.endDate,
scheduledDates: []
};
for(i = 0; i < numberOfScheduledDates; i++) {
randomSchedule.scheduledDates.push(randomDate(interval.startDate, interval.endDate));
}
return randomSchedule;
}
displayVisualizations(years, scheduler, checkIfDateIsAcceptable, timeoutPerIteration);