@happychef/algorithm 1.2.16 → 1.2.17

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.
@@ -1,247 +1,247 @@
1
- const {
2
- parseTime,
3
- getMealTypeByTime,
4
- getAllTables,
5
- isTemporaryTableValid,
6
- shifts
7
- } = require('../tableHelpers');
8
-
9
- describe('tableHelpers - parseTime', () => {
10
- test('should parse valid time string correctly', () => {
11
- expect(parseTime('12:30')).toBe(750); // 12*60 + 30 = 750
12
- expect(parseTime('00:00')).toBe(0);
13
- expect(parseTime('23:59')).toBe(1439);
14
- });
15
-
16
- test('should return NaN for invalid time strings', () => {
17
- expect(parseTime('')).toBeNaN();
18
- expect(parseTime(null)).toBeNaN();
19
- expect(parseTime(undefined)).toBeNaN();
20
- expect(parseTime('25:00')).toBeNaN(); // Invalid hour
21
- expect(parseTime('12:60')).toBeNaN(); // Invalid minute
22
- expect(parseTime('12')).toBeNaN(); // Missing colon
23
- expect(parseTime('ab:cd')).toBeNaN(); // Not numbers
24
- });
25
-
26
- test('should handle edge cases', () => {
27
- expect(parseTime('0:0')).toBe(0);
28
- expect(parseTime('1:1')).toBe(61);
29
- });
30
- });
31
-
32
- describe('tableHelpers - getMealTypeByTime', () => {
33
- test('should identify breakfast times correctly', () => {
34
- expect(getMealTypeByTime('07:00')).toBe('breakfast');
35
- expect(getMealTypeByTime('09:30')).toBe('breakfast');
36
- expect(getMealTypeByTime('10:59')).toBe('breakfast');
37
- });
38
-
39
- test('should identify lunch times correctly', () => {
40
- expect(getMealTypeByTime('11:00')).toBe('lunch');
41
- expect(getMealTypeByTime('12:30')).toBe('lunch');
42
- expect(getMealTypeByTime('15:59')).toBe('lunch');
43
- });
44
-
45
- test('should identify dinner times correctly', () => {
46
- expect(getMealTypeByTime('16:00')).toBe('dinner');
47
- expect(getMealTypeByTime('18:30')).toBe('dinner');
48
- expect(getMealTypeByTime('22:59')).toBe('dinner');
49
- });
50
-
51
- test('should return null for times outside defined shifts', () => {
52
- expect(getMealTypeByTime('06:59')).toBeNull();
53
- expect(getMealTypeByTime('23:00')).toBeNull();
54
- expect(getMealTypeByTime('02:00')).toBeNull();
55
- });
56
-
57
- test('should return null for invalid time strings', () => {
58
- expect(getMealTypeByTime('invalid')).toBeNull();
59
- expect(getMealTypeByTime('')).toBeNull();
60
- expect(getMealTypeByTime(null)).toBeNull();
61
- });
62
- });
63
-
64
- describe('tableHelpers - getAllTables', () => {
65
- test('should extract tables correctly from restaurant data', () => {
66
- const restaurantData = {
67
- floors: [
68
- {
69
- tables: [
70
- {
71
- objectType: 'Tafel',
72
- id: 'table1',
73
- tableNumber: 1,
74
- minCapacity: 2,
75
- maxCapacity: 4,
76
- priority: 1,
77
- x: 10,
78
- y: 20,
79
- isTemporary: false
80
- },
81
- {
82
- objectType: 'Tafel',
83
- id: 'table2',
84
- tableNumber: { $numberInt: '2' },
85
- minCapacity: { $numberInt: '4' },
86
- maxCapacity: { $numberInt: '6' },
87
- priority: { $numberInt: '2' },
88
- x: { $numberInt: '30' },
89
- y: { $numberInt: '40' },
90
- isTemporary: false
91
- }
92
- ]
93
- }
94
- ]
95
- };
96
-
97
- const tables = getAllTables(restaurantData);
98
- expect(tables).toHaveLength(2);
99
- expect(tables[0].tableNumber).toBe(1);
100
- expect(tables[0].maxCapacity).toBe(4);
101
- expect(tables[1].tableNumber).toBe(2);
102
- expect(tables[1].maxCapacity).toBe(6);
103
- });
104
-
105
- test('should sort tables by maxCapacity, priority, and minCapacity', () => {
106
- const restaurantData = {
107
- floors: [
108
- {
109
- tables: [
110
- {
111
- objectType: 'Tafel',
112
- id: 'table1',
113
- tableNumber: 1,
114
- minCapacity: 2,
115
- maxCapacity: 6,
116
- priority: 1,
117
- x: 10,
118
- y: 20
119
- },
120
- {
121
- objectType: 'Tafel',
122
- id: 'table2',
123
- tableNumber: 2,
124
- minCapacity: 2,
125
- maxCapacity: 4,
126
- priority: 1,
127
- x: 30,
128
- y: 40
129
- }
130
- ]
131
- }
132
- ]
133
- };
134
-
135
- const tables = getAllTables(restaurantData);
136
- expect(tables[0].tableNumber).toBe(2); // Smaller maxCapacity comes first
137
- expect(tables[1].tableNumber).toBe(1);
138
- });
139
-
140
- test('should handle empty or missing data gracefully', () => {
141
- expect(getAllTables({})).toEqual([]);
142
- expect(getAllTables({ floors: [] })).toEqual([]);
143
- expect(getAllTables(null)).toEqual([]);
144
- });
145
-
146
- test('should filter out non-Tafel objects', () => {
147
- const restaurantData = {
148
- floors: [
149
- {
150
- tables: [
151
- {
152
- objectType: 'Wall',
153
- id: 'wall1',
154
- tableNumber: 1
155
- },
156
- {
157
- objectType: 'Tafel',
158
- id: 'table1',
159
- tableNumber: 2,
160
- minCapacity: 2,
161
- maxCapacity: 4,
162
- priority: 1,
163
- x: 10,
164
- y: 20
165
- }
166
- ]
167
- }
168
- ]
169
- };
170
-
171
- const tables = getAllTables(restaurantData);
172
- expect(tables).toHaveLength(1);
173
- expect(tables[0].tableNumber).toBe(2);
174
- });
175
- });
176
-
177
- describe('tableHelpers - isTemporaryTableValid', () => {
178
- test('should return true for non-temporary tables', () => {
179
- const table = {
180
- tableNumber: 1,
181
- isTemporary: false
182
- };
183
- expect(isTemporaryTableValid(table, '2025-06-15', '12:00')).toBe(true);
184
- });
185
-
186
- test('should validate temporary table within date range and correct meal type', () => {
187
- const table = {
188
- tableNumber: 1,
189
- isTemporary: true,
190
- startDate: '2025-06-01',
191
- endDate: '2025-06-30',
192
- application: 'lunch'
193
- };
194
- expect(isTemporaryTableValid(table, '2025-06-15', '12:00')).toBe(true);
195
- });
196
-
197
- test('should reject temporary table outside date range', () => {
198
- const table = {
199
- tableNumber: 1,
200
- isTemporary: true,
201
- startDate: '2025-06-01',
202
- endDate: '2025-06-30',
203
- application: 'lunch'
204
- };
205
- expect(isTemporaryTableValid(table, '2025-07-01', '12:00')).toBe(false);
206
- expect(isTemporaryTableValid(table, '2025-05-31', '12:00')).toBe(false);
207
- });
208
-
209
- test('should reject temporary table with wrong meal type', () => {
210
- const table = {
211
- tableNumber: 1,
212
- isTemporary: true,
213
- startDate: '2025-06-01',
214
- endDate: '2025-06-30',
215
- application: 'dinner'
216
- };
217
- expect(isTemporaryTableValid(table, '2025-06-15', '12:00')).toBe(false); // lunch time
218
- });
219
-
220
- test('should reject temporary table with missing date range', () => {
221
- const table = {
222
- tableNumber: 1,
223
- isTemporary: true,
224
- application: 'lunch'
225
- };
226
- expect(isTemporaryTableValid(table, '2025-06-15', '12:00')).toBe(false);
227
- });
228
-
229
- test('should reject temporary table with invalid time', () => {
230
- const table = {
231
- tableNumber: 1,
232
- isTemporary: true,
233
- startDate: '2025-06-01',
234
- endDate: '2025-06-30',
235
- application: 'lunch'
236
- };
237
- expect(isTemporaryTableValid(table, '2025-06-15', 'invalid')).toBe(false);
238
- });
239
- });
240
-
241
- describe('tableHelpers - shifts constant', () => {
242
- test('should have correct shift definitions', () => {
243
- expect(shifts.breakfast).toEqual({ start: '07:00', end: '11:00' });
244
- expect(shifts.lunch).toEqual({ start: '11:00', end: '16:00' });
245
- expect(shifts.dinner).toEqual({ start: '16:00', end: '23:00' });
246
- });
247
- });
1
+ const {
2
+ parseTime,
3
+ getMealTypeByTime,
4
+ getAllTables,
5
+ isTemporaryTableValid,
6
+ shifts
7
+ } = require('../tableHelpers');
8
+
9
+ describe('tableHelpers - parseTime', () => {
10
+ test('should parse valid time string correctly', () => {
11
+ expect(parseTime('12:30')).toBe(750); // 12*60 + 30 = 750
12
+ expect(parseTime('00:00')).toBe(0);
13
+ expect(parseTime('23:59')).toBe(1439);
14
+ });
15
+
16
+ test('should return NaN for invalid time strings', () => {
17
+ expect(parseTime('')).toBeNaN();
18
+ expect(parseTime(null)).toBeNaN();
19
+ expect(parseTime(undefined)).toBeNaN();
20
+ expect(parseTime('25:00')).toBeNaN(); // Invalid hour
21
+ expect(parseTime('12:60')).toBeNaN(); // Invalid minute
22
+ expect(parseTime('12')).toBeNaN(); // Missing colon
23
+ expect(parseTime('ab:cd')).toBeNaN(); // Not numbers
24
+ });
25
+
26
+ test('should handle edge cases', () => {
27
+ expect(parseTime('0:0')).toBe(0);
28
+ expect(parseTime('1:1')).toBe(61);
29
+ });
30
+ });
31
+
32
+ describe('tableHelpers - getMealTypeByTime', () => {
33
+ test('should identify breakfast times correctly', () => {
34
+ expect(getMealTypeByTime('07:00')).toBe('breakfast');
35
+ expect(getMealTypeByTime('09:30')).toBe('breakfast');
36
+ expect(getMealTypeByTime('10:59')).toBe('breakfast');
37
+ });
38
+
39
+ test('should identify lunch times correctly', () => {
40
+ expect(getMealTypeByTime('11:00')).toBe('lunch');
41
+ expect(getMealTypeByTime('12:30')).toBe('lunch');
42
+ expect(getMealTypeByTime('15:59')).toBe('lunch');
43
+ });
44
+
45
+ test('should identify dinner times correctly', () => {
46
+ expect(getMealTypeByTime('16:00')).toBe('dinner');
47
+ expect(getMealTypeByTime('18:30')).toBe('dinner');
48
+ expect(getMealTypeByTime('22:59')).toBe('dinner');
49
+ });
50
+
51
+ test('should return null for times outside defined shifts', () => {
52
+ expect(getMealTypeByTime('06:59')).toBeNull();
53
+ expect(getMealTypeByTime('23:00')).toBeNull();
54
+ expect(getMealTypeByTime('02:00')).toBeNull();
55
+ });
56
+
57
+ test('should return null for invalid time strings', () => {
58
+ expect(getMealTypeByTime('invalid')).toBeNull();
59
+ expect(getMealTypeByTime('')).toBeNull();
60
+ expect(getMealTypeByTime(null)).toBeNull();
61
+ });
62
+ });
63
+
64
+ describe('tableHelpers - getAllTables', () => {
65
+ test('should extract tables correctly from restaurant data', () => {
66
+ const restaurantData = {
67
+ floors: [
68
+ {
69
+ tables: [
70
+ {
71
+ objectType: 'Tafel',
72
+ id: 'table1',
73
+ tableNumber: 1,
74
+ minCapacity: 2,
75
+ maxCapacity: 4,
76
+ priority: 1,
77
+ x: 10,
78
+ y: 20,
79
+ isTemporary: false
80
+ },
81
+ {
82
+ objectType: 'Tafel',
83
+ id: 'table2',
84
+ tableNumber: { $numberInt: '2' },
85
+ minCapacity: { $numberInt: '4' },
86
+ maxCapacity: { $numberInt: '6' },
87
+ priority: { $numberInt: '2' },
88
+ x: { $numberInt: '30' },
89
+ y: { $numberInt: '40' },
90
+ isTemporary: false
91
+ }
92
+ ]
93
+ }
94
+ ]
95
+ };
96
+
97
+ const tables = getAllTables(restaurantData);
98
+ expect(tables).toHaveLength(2);
99
+ expect(tables[0].tableNumber).toBe(1);
100
+ expect(tables[0].maxCapacity).toBe(4);
101
+ expect(tables[1].tableNumber).toBe(2);
102
+ expect(tables[1].maxCapacity).toBe(6);
103
+ });
104
+
105
+ test('should sort tables by maxCapacity, priority, and minCapacity', () => {
106
+ const restaurantData = {
107
+ floors: [
108
+ {
109
+ tables: [
110
+ {
111
+ objectType: 'Tafel',
112
+ id: 'table1',
113
+ tableNumber: 1,
114
+ minCapacity: 2,
115
+ maxCapacity: 6,
116
+ priority: 1,
117
+ x: 10,
118
+ y: 20
119
+ },
120
+ {
121
+ objectType: 'Tafel',
122
+ id: 'table2',
123
+ tableNumber: 2,
124
+ minCapacity: 2,
125
+ maxCapacity: 4,
126
+ priority: 1,
127
+ x: 30,
128
+ y: 40
129
+ }
130
+ ]
131
+ }
132
+ ]
133
+ };
134
+
135
+ const tables = getAllTables(restaurantData);
136
+ expect(tables[0].tableNumber).toBe(2); // Smaller maxCapacity comes first
137
+ expect(tables[1].tableNumber).toBe(1);
138
+ });
139
+
140
+ test('should handle empty or missing data gracefully', () => {
141
+ expect(getAllTables({})).toEqual([]);
142
+ expect(getAllTables({ floors: [] })).toEqual([]);
143
+ expect(getAllTables(null)).toEqual([]);
144
+ });
145
+
146
+ test('should filter out non-Tafel objects', () => {
147
+ const restaurantData = {
148
+ floors: [
149
+ {
150
+ tables: [
151
+ {
152
+ objectType: 'Wall',
153
+ id: 'wall1',
154
+ tableNumber: 1
155
+ },
156
+ {
157
+ objectType: 'Tafel',
158
+ id: 'table1',
159
+ tableNumber: 2,
160
+ minCapacity: 2,
161
+ maxCapacity: 4,
162
+ priority: 1,
163
+ x: 10,
164
+ y: 20
165
+ }
166
+ ]
167
+ }
168
+ ]
169
+ };
170
+
171
+ const tables = getAllTables(restaurantData);
172
+ expect(tables).toHaveLength(1);
173
+ expect(tables[0].tableNumber).toBe(2);
174
+ });
175
+ });
176
+
177
+ describe('tableHelpers - isTemporaryTableValid', () => {
178
+ test('should return true for non-temporary tables', () => {
179
+ const table = {
180
+ tableNumber: 1,
181
+ isTemporary: false
182
+ };
183
+ expect(isTemporaryTableValid(table, '2025-06-15', '12:00')).toBe(true);
184
+ });
185
+
186
+ test('should validate temporary table within date range and correct meal type', () => {
187
+ const table = {
188
+ tableNumber: 1,
189
+ isTemporary: true,
190
+ startDate: '2025-06-01',
191
+ endDate: '2025-06-30',
192
+ application: 'lunch'
193
+ };
194
+ expect(isTemporaryTableValid(table, '2025-06-15', '12:00')).toBe(true);
195
+ });
196
+
197
+ test('should reject temporary table outside date range', () => {
198
+ const table = {
199
+ tableNumber: 1,
200
+ isTemporary: true,
201
+ startDate: '2025-06-01',
202
+ endDate: '2025-06-30',
203
+ application: 'lunch'
204
+ };
205
+ expect(isTemporaryTableValid(table, '2025-07-01', '12:00')).toBe(false);
206
+ expect(isTemporaryTableValid(table, '2025-05-31', '12:00')).toBe(false);
207
+ });
208
+
209
+ test('should reject temporary table with wrong meal type', () => {
210
+ const table = {
211
+ tableNumber: 1,
212
+ isTemporary: true,
213
+ startDate: '2025-06-01',
214
+ endDate: '2025-06-30',
215
+ application: 'dinner'
216
+ };
217
+ expect(isTemporaryTableValid(table, '2025-06-15', '12:00')).toBe(false); // lunch time
218
+ });
219
+
220
+ test('should reject temporary table with missing date range', () => {
221
+ const table = {
222
+ tableNumber: 1,
223
+ isTemporary: true,
224
+ application: 'lunch'
225
+ };
226
+ expect(isTemporaryTableValid(table, '2025-06-15', '12:00')).toBe(false);
227
+ });
228
+
229
+ test('should reject temporary table with invalid time', () => {
230
+ const table = {
231
+ tableNumber: 1,
232
+ isTemporary: true,
233
+ startDate: '2025-06-01',
234
+ endDate: '2025-06-30',
235
+ application: 'lunch'
236
+ };
237
+ expect(isTemporaryTableValid(table, '2025-06-15', 'invalid')).toBe(false);
238
+ });
239
+ });
240
+
241
+ describe('tableHelpers - shifts constant', () => {
242
+ test('should have correct shift definitions', () => {
243
+ expect(shifts.breakfast).toEqual({ start: '07:00', end: '11:00' });
244
+ expect(shifts.lunch).toEqual({ start: '11:00', end: '16:00' });
245
+ expect(shifts.dinner).toEqual({ start: '16:00', end: '23:00' });
246
+ });
247
+ });
@@ -0,0 +1,21 @@
1
+ # PR 13 - Fix timezone issues in date parsing for day-of-week calculations
2
+
3
+ **Actions:**
4
+
5
+ ## Changes Summary
6
+ ADDED:
7
+ - New function `parseDateString(dateStr)` added to `openinghours.js` for parsing YYYY-MM-DD date strings using local timezone components.
8
+ - New export of `parseDateString` from `openinghours.js` for use in other modules.
9
+
10
+ NO_REMOVALS
11
+
12
+ CHANGED:
13
+ - Modified `isDateInRange()` function in `exceptions.js` to replace `new Date(dateStr)` with `parseDateString(dateStr)` for date parsing.
14
+ - Modified `getDataByDateAndMealWithExceptions()` function in `exceptions.js` to replace `new Date(dateStr)` with `parseDateString(dateStr)` for date parsing.
15
+ - Modified `getDataByDateAndMeal()` function in `openinghours.js` to replace `new Date(dateStr)` with `parseDateString(dateStr)` for date parsing.
16
+
17
+ ---
18
+
19
+ **Author:** thibaultvandesompele2
20
+ **Date:** 2026-01-26
21
+ **PR Link:** https://github.com/thibaultvandesompele2/15-happy-algorithm/pull/13
package/jest.config.js CHANGED
@@ -1,23 +1,23 @@
1
- module.exports = {
2
- testEnvironment: 'node',
3
- coverageDirectory: 'coverage',
4
- collectCoverageFrom: [
5
- '**/*.js',
6
- '!**/node_modules/**',
7
- '!**/coverage/**',
8
- '!jest.config.js',
9
- '!test.js',
10
- '!test-*.js',
11
- '!**/__tests__/**'
12
- ],
13
- testMatch: [
14
- '**/__tests__/**/*.test.js'
15
- ],
16
- testPathIgnorePatterns: [
17
- '/node_modules/',
18
- '/test\\.js$',
19
- '/test-.*\\.js$'
20
- ],
21
- verbose: true,
22
- testTimeout: 10000
23
- };
1
+ module.exports = {
2
+ testEnvironment: 'node',
3
+ coverageDirectory: 'coverage',
4
+ collectCoverageFrom: [
5
+ '**/*.js',
6
+ '!**/node_modules/**',
7
+ '!**/coverage/**',
8
+ '!jest.config.js',
9
+ '!test.js',
10
+ '!test-*.js',
11
+ '!**/__tests__/**'
12
+ ],
13
+ testMatch: [
14
+ '**/__tests__/**/*.test.js'
15
+ ],
16
+ testPathIgnorePatterns: [
17
+ '/node_modules/',
18
+ '/test\\.js$',
19
+ '/test-.*\\.js$'
20
+ ],
21
+ verbose: true,
22
+ testTimeout: 10000
23
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@happychef/algorithm",
3
- "version": "1.2.16",
3
+ "version": "1.2.17",
4
4
  "description": "Restaurant and reservation algorithm utilities",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -5,15 +5,16 @@ const {
5
5
  getDataByDateAndTime,
6
6
  getMealTypeByTime,
7
7
  parseTime,
8
+ parseDateString,
8
9
  shifts,
9
10
  daysOfWeekEnglish,
10
11
  daysOfWeekDutch,
11
12
  } = require('./openinghours');
12
13
 
13
14
  function isDateInRange(dateStr, startDateStr, endDateStr) {
14
- const date = new Date(dateStr);
15
- const startDate = new Date(startDateStr);
16
- const endDate = new Date(endDateStr);
15
+ const date = parseDateString(dateStr);
16
+ const startDate = parseDateString(startDateStr);
17
+ const endDate = parseDateString(endDateStr);
17
18
  if (isNaN(date) || isNaN(startDate) || isNaN(endDate)) {
18
19
  return false;
19
20
  }
@@ -86,7 +87,7 @@ function mapExceptionToMealData(exception, data) {
86
87
 
87
88
  function getDataByDateAndMealWithExceptions(data, dateStr, mealType) {
88
89
  const exceptions = data.exceptions || [];
89
- const date = new Date(dateStr);
90
+ const date = parseDateString(dateStr);
90
91
  if (isNaN(date)) {
91
92
  return null;
92
93
  }
@@ -14,6 +14,21 @@ function parseTime(timeStr) {
14
14
  return hours * 60 + minutes;
15
15
  }
16
16
 
17
+ /**
18
+ * Parse a YYYY-MM-DD date string consistently across browsers.
19
+ * Using new Date(dateStr) directly causes browser differences:
20
+ * - Chrome parses "YYYY-MM-DD" as local midnight
21
+ * - Firefox parses "YYYY-MM-DD" as UTC midnight
22
+ * This function ensures consistent local time parsing.
23
+ */
24
+ function parseDateString(dateStr) {
25
+ if (!dateStr || typeof dateStr !== 'string') {
26
+ return new Date(NaN);
27
+ }
28
+ const [year, month, day] = dateStr.split('-').map(Number);
29
+ return new Date(year, month - 1, day);
30
+ }
31
+
17
32
  function getMealTypeByTime(timeStr) {
18
33
  const time = parseTime(timeStr);
19
34
  for (const [mealType, shift] of Object.entries(shifts)) {
@@ -73,7 +88,7 @@ function addDuurReservatieToEndTime(mealData, data) {
73
88
  }
74
89
 
75
90
  function getDataByDateAndMeal(data, dateStr, mealType) {
76
- const date = new Date(dateStr);
91
+ const date = parseDateString(dateStr);
77
92
  if (isNaN(date)) {
78
93
  return null;
79
94
  }
@@ -119,5 +134,6 @@ module.exports = {
119
134
  daysOfWeekDutch,
120
135
  getMealTypeByTime,
121
136
  parseTime,
137
+ parseDateString,
122
138
  shifts,
123
139
  };