@happychef/algorithm 1.3.2 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/.claude/settings.local.json +16 -0
  2. package/.github/workflows/ci-cd.yml +80 -80
  3. package/BRANCH_PROTECTION_SETUP.md +167 -167
  4. package/CHANGELOG.md +8 -8
  5. package/README.md +144 -144
  6. package/RESERVERINGEN_GIDS.md +986 -986
  7. package/__tests__/crossMidnight.test.js +63 -0
  8. package/__tests__/crossMidnightTimeblocks.test.js +312 -0
  9. package/__tests__/edgeCases.test.js +271 -0
  10. package/__tests__/filters.test.js +276 -276
  11. package/__tests__/isDateAvailable.test.js +179 -175
  12. package/__tests__/isTimeAvailable.test.js +174 -168
  13. package/__tests__/restaurantData.test.js +422 -422
  14. package/__tests__/tableHelpers.test.js +247 -247
  15. package/assignTables.js +506 -444
  16. package/changes/2025/December/PR2___change.md +14 -14
  17. package/changes/2025/December/PR3_add__change.md +20 -20
  18. package/changes/2025/December/PR4___.md +15 -15
  19. package/changes/2025/December/PR5___.md +15 -15
  20. package/changes/2025/December/PR6__del_.md +17 -17
  21. package/changes/2025/December/PR7_add__change.md +21 -21
  22. package/changes/2026/February/PR15_add__change.md +21 -21
  23. package/changes/2026/February/PR16_add__.md +20 -0
  24. package/changes/2026/February/PR16_add_getDateClosingReasons.md +31 -31
  25. package/changes/2026/January/PR10_add__change.md +21 -21
  26. package/changes/2026/January/PR11_add__change.md +19 -19
  27. package/changes/2026/January/PR12_add__.md +21 -21
  28. package/changes/2026/January/PR13_add__change.md +20 -20
  29. package/changes/2026/January/PR14_add__change.md +19 -19
  30. package/changes/2026/January/PR8_add__change.md +38 -38
  31. package/changes/2026/January/PR9_add__change.md +19 -19
  32. package/dateHelpers.js +31 -0
  33. package/filters/maxArrivalsFilter.js +114 -114
  34. package/filters/maxGroupsFilter.js +221 -221
  35. package/filters/timeFilter.js +89 -89
  36. package/getAvailableTimeblocks.js +158 -158
  37. package/getDateClosingReasons.js +193 -193
  38. package/grouping.js +162 -162
  39. package/index.js +48 -43
  40. package/isDateAvailable.js +80 -80
  41. package/isDateAvailableWithTableCheck.js +172 -172
  42. package/isTimeAvailable.js +26 -26
  43. package/jest.config.js +23 -23
  44. package/package.json +27 -27
  45. package/processing/dailyGuestCounts.js +73 -73
  46. package/processing/mealTypeCount.js +133 -133
  47. package/processing/timeblocksAvailable.js +344 -182
  48. package/reservation_data/counter.js +82 -75
  49. package/restaurant_data/exceptions.js +150 -150
  50. package/restaurant_data/openinghours.js +142 -142
  51. package/simulateTableAssignment.js +833 -726
  52. package/tableHelpers.js +209 -209
  53. package/tables/time/parseTime.js +19 -19
  54. package/tables/time/shifts.js +7 -7
  55. package/tables/utils/calculateDistance.js +13 -13
  56. package/tables/utils/isTableFreeForAllSlots.js +14 -14
  57. package/tables/utils/isTemporaryTableValid.js +39 -39
  58. package/test/test_counter.js +194 -194
  59. package/test/test_dailyCount.js +81 -81
  60. package/test/test_datesAvailable.js +106 -106
  61. package/test/test_exceptions.js +172 -172
  62. package/test/test_isDateAvailable.js +330 -330
  63. package/test/test_mealTypeCount.js +54 -54
  64. package/test/test_timesAvailable.js +88 -88
  65. package/test-detailed-filter.js +100 -100
  66. package/test-lunch-debug.js +110 -110
  67. package/test-max-arrivals-filter.js +79 -79
  68. package/test-meal-stop-fix.js +147 -147
  69. package/test-meal-stop-simple.js +93 -93
  70. package/test-timezone-debug.js +47 -47
  71. package/test.js +336 -336
@@ -1,75 +1,82 @@
1
- // counter.js
2
-
3
- /**
4
- * Parses a time string in "HH:MM" format into minutes since midnight.
5
- * @param {string} timeStr - The time string in "HH:MM" format.
6
- * @returns {number} The time in minutes since midnight.
7
- */
8
- function parseTime(timeStr) {
9
- const [hours, minutes] = timeStr.split(':').map(Number);
10
- return hours * 60 + minutes;
11
- }
12
-
13
- function getDuurReservatie(data) {
14
- let duurReservatie = 120;
15
- if (
16
- data['general-settings'] &&
17
- data['general-settings'].duurReservatie &&
18
- parseInt(data['general-settings'].duurReservatie, 10) > 0
19
- ) {
20
- duurReservatie = parseInt(data['general-settings'].duurReservatie, 10);
21
- }
22
-
23
- return duurReservatie;
24
- }
25
-
26
- /**
27
- * Calculates the total number of guests for reservations that cover a specific hour on a specific date.
28
- * @param {Object} data - The main data object containing general settings.
29
- * @param {Array} reservations - An array of reservation objects.
30
- * @param {string} hour - The hour to check in "HH:MM" format.
31
- * @param {string} dateStr - The date string in "YYYY-MM-DD" format.
32
- * @returns {number} The total number of guests for that hour on the specified date.
33
- */
34
- function getGuestCountAtHour(data, reservations, hour, dateStr) {
35
- // Get 'duurReservatie' from general settings, default to 120 if not set or zero
36
- let duurReservatie = getDuurReservatie(data)
37
-
38
- // Convert the target hour to minutes since midnight
39
- const targetTime = parseTime(hour);
40
-
41
- let totalGuests = 0;
42
-
43
- for (const reservation of reservations) {
44
- // Only consider reservations on the specified date
45
- if (reservation.date !== dateStr) {
46
- continue;
47
- }
48
-
49
- const startTime = parseTime(reservation.time);
50
-
51
- // Use reservation specific duration if available, else default
52
- let rDuration = duurReservatie;
53
- if (reservation.duration) {
54
- const rawDur = reservation.duration.$numberInt ?? reservation.duration;
55
- const parsed = parseInt(rawDur, 10);
56
- if (!isNaN(parsed) && parsed > 0) {
57
- rDuration = parsed;
58
- }
59
- }
60
-
61
- const endTime = startTime + rDuration;
62
- // Check if the target time is within the reservation time range
63
- // Start time is inclusive, end time is exclusive
64
- if (targetTime >= startTime && targetTime < endTime) {
65
- totalGuests += parseInt(reservation.guests, 10);
66
- }
67
- }
68
-
69
- return totalGuests;
70
- }
71
-
72
- module.exports = {
73
- getGuestCountAtHour,
74
- };
75
-
1
+ // counter.js
2
+
3
+ const { getPrevDateStr } = require('../dateHelpers');
4
+
5
+ /**
6
+ * Parses a time string in "HH:MM" format into minutes since midnight.
7
+ * @param {string} timeStr - The time string in "HH:MM" format.
8
+ * @returns {number} The time in minutes since midnight.
9
+ */
10
+ function parseTime(timeStr) {
11
+ const [hours, minutes] = timeStr.split(':').map(Number);
12
+ return hours * 60 + minutes;
13
+ }
14
+
15
+ function getDuurReservatie(data) {
16
+ let duurReservatie = 120;
17
+ if (
18
+ data['general-settings'] &&
19
+ data['general-settings'].duurReservatie &&
20
+ parseInt(data['general-settings'].duurReservatie, 10) > 0
21
+ ) {
22
+ duurReservatie = parseInt(data['general-settings'].duurReservatie, 10);
23
+ }
24
+
25
+ return duurReservatie;
26
+ }
27
+
28
+ /**
29
+ * Parses a reservation's duration, falling back to the default.
30
+ */
31
+ function getReservationDuration(reservation, defaultDuration) {
32
+ if (reservation.duration) {
33
+ const rawDur = reservation.duration.$numberInt ?? reservation.duration;
34
+ const parsed = parseInt(rawDur, 10);
35
+ if (!isNaN(parsed) && parsed > 0) {
36
+ return parsed;
37
+ }
38
+ }
39
+ return defaultDuration;
40
+ }
41
+
42
+ /**
43
+ * Calculates the total number of guests for reservations that cover a specific hour on a specific date.
44
+ * Also includes guests from previous-day reservations whose duration extends past midnight.
45
+ * @param {Object} data - The main data object containing general settings.
46
+ * @param {Array} reservations - An array of reservation objects.
47
+ * @param {string} hour - The hour to check in "HH:MM" format.
48
+ * @param {string} dateStr - The date string in "YYYY-MM-DD" format.
49
+ * @returns {number} The total number of guests for that hour on the specified date.
50
+ */
51
+ function getGuestCountAtHour(data, reservations, hour, dateStr) {
52
+ const duurReservatie = getDuurReservatie(data);
53
+ const targetTime = parseTime(hour);
54
+ const prevDateStr = getPrevDateStr(dateStr);
55
+
56
+ let totalGuests = 0;
57
+
58
+ for (const reservation of reservations) {
59
+ const startTime = parseTime(reservation.time);
60
+ const rDuration = getReservationDuration(reservation, duurReservatie);
61
+ const endTime = startTime + rDuration;
62
+
63
+ if (reservation.date === dateStr) {
64
+ // Same-day reservation
65
+ if (targetTime >= startTime && targetTime < endTime) {
66
+ totalGuests += parseInt(reservation.guests, 10);
67
+ }
68
+ } else if (reservation.date === prevDateStr && endTime > 1440) {
69
+ // Previous-day reservation that crosses midnight
70
+ const spilloverEnd = endTime - 1440;
71
+ if (targetTime < spilloverEnd) {
72
+ totalGuests += parseInt(reservation.guests, 10);
73
+ }
74
+ }
75
+ }
76
+
77
+ return totalGuests;
78
+ }
79
+
80
+ module.exports = {
81
+ getGuestCountAtHour,
82
+ };
@@ -1,150 +1,150 @@
1
- // exceptions.js
2
-
3
- const {
4
- getDataByDateAndMeal,
5
- getDataByDateAndTime,
6
- getMealTypeByTime,
7
- parseTime,
8
- parseDateString,
9
- shifts,
10
- daysOfWeekEnglish,
11
- daysOfWeekDutch,
12
- } = require('./openinghours');
13
-
14
- function isDateInRange(dateStr, startDateStr, endDateStr) {
15
- const date = parseDateString(dateStr);
16
- const startDate = parseDateString(startDateStr);
17
- const endDate = parseDateString(endDateStr);
18
- if (isNaN(date) || isNaN(startDate) || isNaN(endDate)) {
19
- return false;
20
- }
21
- return date >= startDate && date <= endDate;
22
- }
23
-
24
- function getDutchDayOfWeek(date) {
25
- const dayIndex = date.getDay();
26
- return daysOfWeekDutch[dayIndex];
27
- }
28
-
29
- function doesExceptionApply(exception, dateStr, dateDayOfWeekDutch, mealType) {
30
- const { timeframe, startDate, endDate, daysOfWeek } = exception;
31
-
32
- if (!isDateInRange(dateStr, startDate, endDate)) {
33
- return false;
34
- }
35
-
36
- if (Array.isArray(daysOfWeek) && daysOfWeek.length > 0) {
37
- const daysOfWeekLower = daysOfWeek.map(day => day.toLowerCase());
38
- if (!daysOfWeekLower.includes(dateDayOfWeekDutch)) {
39
- return false;
40
- }
41
- }
42
-
43
- if (timeframe === 'Volledige Dag' || timeframe === mealType) {
44
- return true;
45
- }
46
-
47
- return false;
48
- }
49
-
50
- function getDuurReservatie(data) {
51
- let duurReservatie = 120;
52
- if (
53
- data['general-settings'] &&
54
- data['general-settings'].duurReservatie &&
55
- parseInt(data['general-settings'].duurReservatie, 10) > 0
56
- ) {
57
- duurReservatie = parseInt(data['general-settings'].duurReservatie, 10);
58
- }
59
- return duurReservatie;
60
- }
61
-
62
- function addDuurReservatieToEndTime(mealData, data) {
63
- const duurReservatie = getDuurReservatie(data);
64
- const endMinutes = parseTime(mealData.endTime);
65
- const newEndMinutes = endMinutes + duurReservatie;
66
- const hours = String(Math.floor(newEndMinutes / 60)).padStart(2, '0');
67
- const minutes = String(newEndMinutes % 60).padStart(2, '0');
68
- mealData.endTime = `${hours}:${minutes}`;
69
- }
70
-
71
- function mapExceptionToMealData(exception, data) {
72
- let mealData = {
73
- enabled: true,
74
- startTime: exception.startHour,
75
- endTime: exception.endHour,
76
- maxCapacityEnabled: exception.maxSeats ? true : false,
77
- maxCapacity: exception.maxSeats || null,
78
- shiftsEnabled: false,
79
- shifts: [],
80
- };
81
-
82
- // Add duurReservatie to endTime
83
- addDuurReservatieToEndTime(mealData, data);
84
-
85
- return mealData;
86
- }
87
-
88
- function getDataByDateAndMealWithExceptions(data, dateStr, mealType) {
89
- const exceptions = data.exceptions || [];
90
- const date = parseDateString(dateStr);
91
- if (isNaN(date)) {
92
- return null;
93
- }
94
- const dateDayOfWeekDutch = getDutchDayOfWeek(date).toLowerCase();
95
-
96
- const exceptionTypesPriority = ['Opening', 'Sluiting', 'Uitzondering'];
97
-
98
- for (const exceptionType of exceptionTypesPriority) {
99
- for (const exception of exceptions) {
100
- if (exception.type === exceptionType) {
101
- if (doesExceptionApply(exception, dateStr, dateDayOfWeekDutch, mealType)) {
102
- if (exceptionType === 'Sluiting') {
103
- return null;
104
- } else {
105
- return mapExceptionToMealData(exception, data);
106
- }
107
- }
108
- }
109
- }
110
- }
111
-
112
- return getDataByDateAndMeal(data, dateStr, mealType);
113
- }
114
-
115
- function shouldIncludeEndTime(mealType, endTime) {
116
- if ((mealType === 'breakfast' && endTime === '11:00') ||
117
- (mealType === 'lunch' && endTime === '16:00')) {
118
- return false;
119
- }
120
- return true;
121
- }
122
-
123
- function getDataByDateAndTimeWithExceptions(data, dateStr, timeStr) {
124
- const mealType = getMealTypeByTime(timeStr);
125
- if (!mealType) {
126
- return null;
127
- }
128
- const mealData = getDataByDateAndMealWithExceptions(data, dateStr, mealType);
129
- if (!mealData) {
130
- return null;
131
- }
132
-
133
- const requestedTime = parseTime(timeStr);
134
- const startTime = parseTime(mealData.startTime);
135
- const endTime = parseTime(mealData.endTime);
136
-
137
- const includeEndTime = shouldIncludeEndTime(mealType, mealData.endTime);
138
-
139
- const timeFallsWithin =
140
- includeEndTime
141
- ? requestedTime >= startTime && requestedTime <= endTime
142
- : requestedTime >= startTime && requestedTime < endTime;
143
-
144
- return timeFallsWithin ? mealData : null;
145
- }
146
-
147
- module.exports = {
148
- getDataByDateAndMealWithExceptions,
149
- getDataByDateAndTimeWithExceptions,
150
- };
1
+ // exceptions.js
2
+
3
+ const {
4
+ getDataByDateAndMeal,
5
+ getDataByDateAndTime,
6
+ getMealTypeByTime,
7
+ parseTime,
8
+ parseDateString,
9
+ shifts,
10
+ daysOfWeekEnglish,
11
+ daysOfWeekDutch,
12
+ } = require('./openinghours');
13
+
14
+ function isDateInRange(dateStr, startDateStr, endDateStr) {
15
+ const date = parseDateString(dateStr);
16
+ const startDate = parseDateString(startDateStr);
17
+ const endDate = parseDateString(endDateStr);
18
+ if (isNaN(date) || isNaN(startDate) || isNaN(endDate)) {
19
+ return false;
20
+ }
21
+ return date >= startDate && date <= endDate;
22
+ }
23
+
24
+ function getDutchDayOfWeek(date) {
25
+ const dayIndex = date.getDay();
26
+ return daysOfWeekDutch[dayIndex];
27
+ }
28
+
29
+ function doesExceptionApply(exception, dateStr, dateDayOfWeekDutch, mealType) {
30
+ const { timeframe, startDate, endDate, daysOfWeek } = exception;
31
+
32
+ if (!isDateInRange(dateStr, startDate, endDate)) {
33
+ return false;
34
+ }
35
+
36
+ if (Array.isArray(daysOfWeek) && daysOfWeek.length > 0) {
37
+ const daysOfWeekLower = daysOfWeek.map(day => day.toLowerCase());
38
+ if (!daysOfWeekLower.includes(dateDayOfWeekDutch)) {
39
+ return false;
40
+ }
41
+ }
42
+
43
+ if (timeframe === 'Volledige Dag' || timeframe === mealType) {
44
+ return true;
45
+ }
46
+
47
+ return false;
48
+ }
49
+
50
+ function getDuurReservatie(data) {
51
+ let duurReservatie = 120;
52
+ if (
53
+ data['general-settings'] &&
54
+ data['general-settings'].duurReservatie &&
55
+ parseInt(data['general-settings'].duurReservatie, 10) > 0
56
+ ) {
57
+ duurReservatie = parseInt(data['general-settings'].duurReservatie, 10);
58
+ }
59
+ return duurReservatie;
60
+ }
61
+
62
+ function addDuurReservatieToEndTime(mealData, data) {
63
+ const duurReservatie = getDuurReservatie(data);
64
+ const endMinutes = parseTime(mealData.endTime);
65
+ const newEndMinutes = endMinutes + duurReservatie;
66
+ const hours = String(Math.floor(newEndMinutes / 60)).padStart(2, '0');
67
+ const minutes = String(newEndMinutes % 60).padStart(2, '0');
68
+ mealData.endTime = `${hours}:${minutes}`;
69
+ }
70
+
71
+ function mapExceptionToMealData(exception, data) {
72
+ let mealData = {
73
+ enabled: true,
74
+ startTime: exception.startHour,
75
+ endTime: exception.endHour,
76
+ maxCapacityEnabled: exception.maxSeats ? true : false,
77
+ maxCapacity: exception.maxSeats || null,
78
+ shiftsEnabled: false,
79
+ shifts: [],
80
+ };
81
+
82
+ // Add duurReservatie to endTime
83
+ addDuurReservatieToEndTime(mealData, data);
84
+
85
+ return mealData;
86
+ }
87
+
88
+ function getDataByDateAndMealWithExceptions(data, dateStr, mealType) {
89
+ const exceptions = data.exceptions || [];
90
+ const date = parseDateString(dateStr);
91
+ if (isNaN(date)) {
92
+ return null;
93
+ }
94
+ const dateDayOfWeekDutch = getDutchDayOfWeek(date).toLowerCase();
95
+
96
+ const exceptionTypesPriority = ['Opening', 'Sluiting', 'Uitzondering'];
97
+
98
+ for (const exceptionType of exceptionTypesPriority) {
99
+ for (const exception of exceptions) {
100
+ if (exception.type === exceptionType) {
101
+ if (doesExceptionApply(exception, dateStr, dateDayOfWeekDutch, mealType)) {
102
+ if (exceptionType === 'Sluiting') {
103
+ return null;
104
+ } else {
105
+ return mapExceptionToMealData(exception, data);
106
+ }
107
+ }
108
+ }
109
+ }
110
+ }
111
+
112
+ return getDataByDateAndMeal(data, dateStr, mealType);
113
+ }
114
+
115
+ function shouldIncludeEndTime(mealType, endTime) {
116
+ if ((mealType === 'breakfast' && endTime === '11:00') ||
117
+ (mealType === 'lunch' && endTime === '16:00')) {
118
+ return false;
119
+ }
120
+ return true;
121
+ }
122
+
123
+ function getDataByDateAndTimeWithExceptions(data, dateStr, timeStr) {
124
+ const mealType = getMealTypeByTime(timeStr);
125
+ if (!mealType) {
126
+ return null;
127
+ }
128
+ const mealData = getDataByDateAndMealWithExceptions(data, dateStr, mealType);
129
+ if (!mealData) {
130
+ return null;
131
+ }
132
+
133
+ const requestedTime = parseTime(timeStr);
134
+ const startTime = parseTime(mealData.startTime);
135
+ const endTime = parseTime(mealData.endTime);
136
+
137
+ const includeEndTime = shouldIncludeEndTime(mealType, mealData.endTime);
138
+
139
+ const timeFallsWithin =
140
+ includeEndTime
141
+ ? requestedTime >= startTime && requestedTime <= endTime
142
+ : requestedTime >= startTime && requestedTime < endTime;
143
+
144
+ return timeFallsWithin ? mealData : null;
145
+ }
146
+
147
+ module.exports = {
148
+ getDataByDateAndMealWithExceptions,
149
+ getDataByDateAndTimeWithExceptions,
150
+ };