dabke 0.78.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 (194) hide show
  1. package/CHANGELOG.md +120 -0
  2. package/LICENSE +21 -0
  3. package/README.md +187 -0
  4. package/dist/client.d.ts +14 -0
  5. package/dist/client.d.ts.map +1 -0
  6. package/dist/client.js +42 -0
  7. package/dist/client.js.map +1 -0
  8. package/dist/client.schemas.d.ts +250 -0
  9. package/dist/client.schemas.d.ts.map +1 -0
  10. package/dist/client.schemas.js +137 -0
  11. package/dist/client.schemas.js.map +1 -0
  12. package/dist/client.types.d.ts +34 -0
  13. package/dist/client.types.d.ts.map +1 -0
  14. package/dist/client.types.js +18 -0
  15. package/dist/client.types.js.map +1 -0
  16. package/dist/cpsat/model-builder.d.ts +128 -0
  17. package/dist/cpsat/model-builder.d.ts.map +1 -0
  18. package/dist/cpsat/model-builder.js +640 -0
  19. package/dist/cpsat/model-builder.js.map +1 -0
  20. package/dist/cpsat/response.d.ts +74 -0
  21. package/dist/cpsat/response.d.ts.map +1 -0
  22. package/dist/cpsat/response.js +92 -0
  23. package/dist/cpsat/response.js.map +1 -0
  24. package/dist/cpsat/rules/assign-together.d.ts +23 -0
  25. package/dist/cpsat/rules/assign-together.d.ts.map +1 -0
  26. package/dist/cpsat/rules/assign-together.js +78 -0
  27. package/dist/cpsat/rules/assign-together.js.map +1 -0
  28. package/dist/cpsat/rules/employee-assignment-priority.d.ts +64 -0
  29. package/dist/cpsat/rules/employee-assignment-priority.d.ts.map +1 -0
  30. package/dist/cpsat/rules/employee-assignment-priority.js +151 -0
  31. package/dist/cpsat/rules/employee-assignment-priority.js.map +1 -0
  32. package/dist/cpsat/rules/index.d.ts +13 -0
  33. package/dist/cpsat/rules/index.d.ts.map +1 -0
  34. package/dist/cpsat/rules/index.js +13 -0
  35. package/dist/cpsat/rules/index.js.map +1 -0
  36. package/dist/cpsat/rules/location-preference.d.ts +29 -0
  37. package/dist/cpsat/rules/location-preference.d.ts.map +1 -0
  38. package/dist/cpsat/rules/location-preference.js +59 -0
  39. package/dist/cpsat/rules/location-preference.js.map +1 -0
  40. package/dist/cpsat/rules/max-consecutive-days.d.ts +28 -0
  41. package/dist/cpsat/rules/max-consecutive-days.d.ts.map +1 -0
  42. package/dist/cpsat/rules/max-consecutive-days.js +70 -0
  43. package/dist/cpsat/rules/max-consecutive-days.js.map +1 -0
  44. package/dist/cpsat/rules/max-hours-day.d.ts +57 -0
  45. package/dist/cpsat/rules/max-hours-day.d.ts.map +1 -0
  46. package/dist/cpsat/rules/max-hours-day.js +159 -0
  47. package/dist/cpsat/rules/max-hours-day.js.map +1 -0
  48. package/dist/cpsat/rules/max-hours-week.d.ts +62 -0
  49. package/dist/cpsat/rules/max-hours-week.d.ts.map +1 -0
  50. package/dist/cpsat/rules/max-hours-week.js +169 -0
  51. package/dist/cpsat/rules/max-hours-week.js.map +1 -0
  52. package/dist/cpsat/rules/max-shifts-day.d.ts +69 -0
  53. package/dist/cpsat/rules/max-shifts-day.d.ts.map +1 -0
  54. package/dist/cpsat/rules/max-shifts-day.js +170 -0
  55. package/dist/cpsat/rules/max-shifts-day.js.map +1 -0
  56. package/dist/cpsat/rules/min-consecutive-days.d.ts +29 -0
  57. package/dist/cpsat/rules/min-consecutive-days.d.ts.map +1 -0
  58. package/dist/cpsat/rules/min-consecutive-days.js +104 -0
  59. package/dist/cpsat/rules/min-consecutive-days.js.map +1 -0
  60. package/dist/cpsat/rules/min-hours-day.d.ts +28 -0
  61. package/dist/cpsat/rules/min-hours-day.d.ts.map +1 -0
  62. package/dist/cpsat/rules/min-hours-day.js +61 -0
  63. package/dist/cpsat/rules/min-hours-day.js.map +1 -0
  64. package/dist/cpsat/rules/min-hours-week.d.ts +29 -0
  65. package/dist/cpsat/rules/min-hours-week.d.ts.map +1 -0
  66. package/dist/cpsat/rules/min-hours-week.js +68 -0
  67. package/dist/cpsat/rules/min-hours-week.js.map +1 -0
  68. package/dist/cpsat/rules/min-rest-between-shifts.d.ts +28 -0
  69. package/dist/cpsat/rules/min-rest-between-shifts.d.ts.map +1 -0
  70. package/dist/cpsat/rules/min-rest-between-shifts.js +95 -0
  71. package/dist/cpsat/rules/min-rest-between-shifts.js.map +1 -0
  72. package/dist/cpsat/rules/registry.d.ts +7 -0
  73. package/dist/cpsat/rules/registry.d.ts.map +1 -0
  74. package/dist/cpsat/rules/registry.js +28 -0
  75. package/dist/cpsat/rules/registry.js.map +1 -0
  76. package/dist/cpsat/rules/resolver.d.ts +31 -0
  77. package/dist/cpsat/rules/resolver.d.ts.map +1 -0
  78. package/dist/cpsat/rules/resolver.js +124 -0
  79. package/dist/cpsat/rules/resolver.js.map +1 -0
  80. package/dist/cpsat/rules/rules.types.d.ts +32 -0
  81. package/dist/cpsat/rules/rules.types.d.ts.map +1 -0
  82. package/dist/cpsat/rules/rules.types.js +2 -0
  83. package/dist/cpsat/rules/rules.types.js.map +1 -0
  84. package/dist/cpsat/rules/scoping.d.ts +129 -0
  85. package/dist/cpsat/rules/scoping.d.ts.map +1 -0
  86. package/dist/cpsat/rules/scoping.js +190 -0
  87. package/dist/cpsat/rules/scoping.js.map +1 -0
  88. package/dist/cpsat/rules/time-off.d.ts +78 -0
  89. package/dist/cpsat/rules/time-off.d.ts.map +1 -0
  90. package/dist/cpsat/rules/time-off.js +261 -0
  91. package/dist/cpsat/rules/time-off.js.map +1 -0
  92. package/dist/cpsat/rules.d.ts +5 -0
  93. package/dist/cpsat/rules.d.ts.map +1 -0
  94. package/dist/cpsat/rules.js +4 -0
  95. package/dist/cpsat/rules.js.map +1 -0
  96. package/dist/cpsat/semantic-time.d.ts +198 -0
  97. package/dist/cpsat/semantic-time.d.ts.map +1 -0
  98. package/dist/cpsat/semantic-time.js +222 -0
  99. package/dist/cpsat/semantic-time.js.map +1 -0
  100. package/dist/cpsat/types.d.ts +180 -0
  101. package/dist/cpsat/types.d.ts.map +1 -0
  102. package/dist/cpsat/types.js +2 -0
  103. package/dist/cpsat/types.js.map +1 -0
  104. package/dist/cpsat/utils.d.ts +47 -0
  105. package/dist/cpsat/utils.d.ts.map +1 -0
  106. package/dist/cpsat/utils.js +92 -0
  107. package/dist/cpsat/utils.js.map +1 -0
  108. package/dist/cpsat/validation-reporter.d.ts +54 -0
  109. package/dist/cpsat/validation-reporter.d.ts.map +1 -0
  110. package/dist/cpsat/validation-reporter.js +261 -0
  111. package/dist/cpsat/validation-reporter.js.map +1 -0
  112. package/dist/cpsat/validation.types.d.ts +141 -0
  113. package/dist/cpsat/validation.types.d.ts.map +1 -0
  114. package/dist/cpsat/validation.types.js +14 -0
  115. package/dist/cpsat/validation.types.js.map +1 -0
  116. package/dist/datetime.utils.d.ts +245 -0
  117. package/dist/datetime.utils.d.ts.map +1 -0
  118. package/dist/datetime.utils.js +372 -0
  119. package/dist/datetime.utils.js.map +1 -0
  120. package/dist/errors.d.ts +12 -0
  121. package/dist/errors.d.ts.map +1 -0
  122. package/dist/errors.js +17 -0
  123. package/dist/errors.js.map +1 -0
  124. package/dist/index.d.ts +112 -0
  125. package/dist/index.d.ts.map +1 -0
  126. package/dist/index.js +116 -0
  127. package/dist/index.js.map +1 -0
  128. package/dist/llms.d.ts +5 -0
  129. package/dist/llms.d.ts.map +1 -0
  130. package/dist/llms.js +8 -0
  131. package/dist/llms.js.map +1 -0
  132. package/dist/testing/index.d.ts +12 -0
  133. package/dist/testing/index.d.ts.map +1 -0
  134. package/dist/testing/index.js +11 -0
  135. package/dist/testing/index.js.map +1 -0
  136. package/dist/testing/solver-container.d.ts +49 -0
  137. package/dist/testing/solver-container.d.ts.map +1 -0
  138. package/dist/testing/solver-container.js +127 -0
  139. package/dist/testing/solver-container.js.map +1 -0
  140. package/dist/types.d.ts +155 -0
  141. package/dist/types.d.ts.map +1 -0
  142. package/dist/types.js +20 -0
  143. package/dist/types.js.map +1 -0
  144. package/dist/validation.d.ts +105 -0
  145. package/dist/validation.d.ts.map +1 -0
  146. package/dist/validation.js +130 -0
  147. package/dist/validation.js.map +1 -0
  148. package/llms.txt +2188 -0
  149. package/package.json +76 -0
  150. package/solver/Dockerfile +31 -0
  151. package/solver/README.md +23 -0
  152. package/solver/pyproject.toml +28 -0
  153. package/solver/src/solver/__init__.py +1 -0
  154. package/solver/src/solver/app.py +24 -0
  155. package/solver/src/solver/models.py +120 -0
  156. package/solver/src/solver/solver.py +359 -0
  157. package/solver/tests/test_solver.py +156 -0
  158. package/solver/uv.lock +661 -0
  159. package/src/client.schemas.ts +163 -0
  160. package/src/client.ts +67 -0
  161. package/src/client.types.ts +66 -0
  162. package/src/cpsat/model-builder.ts +858 -0
  163. package/src/cpsat/response.ts +130 -0
  164. package/src/cpsat/rules/assign-together.ts +96 -0
  165. package/src/cpsat/rules/employee-assignment-priority.ts +182 -0
  166. package/src/cpsat/rules/index.ts +12 -0
  167. package/src/cpsat/rules/location-preference.ts +68 -0
  168. package/src/cpsat/rules/max-consecutive-days.ts +98 -0
  169. package/src/cpsat/rules/max-hours-day.ts +187 -0
  170. package/src/cpsat/rules/max-hours-week.ts +197 -0
  171. package/src/cpsat/rules/max-shifts-day.ts +198 -0
  172. package/src/cpsat/rules/min-consecutive-days.ts +140 -0
  173. package/src/cpsat/rules/min-hours-day.ts +69 -0
  174. package/src/cpsat/rules/min-hours-week.ts +77 -0
  175. package/src/cpsat/rules/min-rest-between-shifts.ts +121 -0
  176. package/src/cpsat/rules/registry.ts +49 -0
  177. package/src/cpsat/rules/resolver.ts +181 -0
  178. package/src/cpsat/rules/rules.types.ts +41 -0
  179. package/src/cpsat/rules/scoping.ts +340 -0
  180. package/src/cpsat/rules/time-off.ts +336 -0
  181. package/src/cpsat/rules.ts +27 -0
  182. package/src/cpsat/semantic-time.ts +463 -0
  183. package/src/cpsat/types.ts +194 -0
  184. package/src/cpsat/utils.ts +105 -0
  185. package/src/cpsat/validation-reporter.ts +366 -0
  186. package/src/cpsat/validation.types.ts +185 -0
  187. package/src/datetime.utils.ts +426 -0
  188. package/src/errors.ts +17 -0
  189. package/src/index.ts +289 -0
  190. package/src/llms.ts +9 -0
  191. package/src/testing/index.ts +12 -0
  192. package/src/testing/solver-container.ts +172 -0
  193. package/src/types.ts +191 -0
  194. package/src/validation.ts +188 -0
@@ -0,0 +1,426 @@
1
+ import { CalendarDate, DateTime, DateTimeRange, DayOfWeek, SchedulingPeriod } from "./types.js";
2
+
3
+ /**
4
+ * Converts a JavaScript Date to a CalendarDate
5
+ */
6
+ export function dateToCalendarDate(date: Date): CalendarDate {
7
+ return {
8
+ year: date.getFullYear(),
9
+ month: date.getMonth() + 1, // JS months are 0-indexed
10
+ day: date.getDate(),
11
+ };
12
+ }
13
+
14
+ /**
15
+ * Converts a DateTime to a JavaScript Date
16
+ * Internal helper function
17
+ */
18
+ export function dateTimeToDate(dateTime: DateTime): Date {
19
+ return new Date(
20
+ dateTime.year || 0,
21
+ (dateTime.month || 1) - 1,
22
+ dateTime.day || 1,
23
+ dateTime.hours || 0,
24
+ dateTime.minutes || 0,
25
+ dateTime.seconds || 0,
26
+ );
27
+ }
28
+
29
+ /**
30
+ * Compares two DateTimes
31
+ * Returns:
32
+ * -1 if dateTime1 < dateTime2
33
+ * 0 if dateTime1 = dateTime2
34
+ * 1 if dateTime1 > dateTime2
35
+ */
36
+ export function compareDateTimes(dateTime1: DateTime, dateTime2: DateTime): number {
37
+ const date1 = dateTimeToDate(dateTime1);
38
+ const date2 = dateTimeToDate(dateTime2);
39
+
40
+ if (date1 < date2) return -1;
41
+ if (date1 > date2) return 1;
42
+ return 0;
43
+ }
44
+
45
+ export const DAY_OF_WEEK_MAP = {
46
+ sunday: 0, // JavaScript Date week starts on Sunday
47
+ monday: 1,
48
+ tuesday: 2,
49
+ wednesday: 3,
50
+ thursday: 4,
51
+ friday: 5,
52
+ saturday: 6,
53
+ };
54
+
55
+ const DAY_NAMES = [
56
+ "sunday",
57
+ "monday",
58
+ "tuesday",
59
+ "wednesday",
60
+ "thursday",
61
+ "friday",
62
+ "saturday",
63
+ ] as const;
64
+
65
+ /**
66
+ * Helper to get the day of week name from a Date (local time)
67
+ */
68
+ export function toDayOfWeek(date: Date): DayOfWeek {
69
+ // getDay() always returns 0-6
70
+ return DAY_NAMES[date.getDay()] as DayOfWeek;
71
+ }
72
+
73
+ /**
74
+ * Helper to get the day of week name from a Date (UTC)
75
+ * Use this when working with date strings like "2026-01-10" that are timezone-agnostic.
76
+ */
77
+ export function toDayOfWeekUTC(date: Date): DayOfWeek {
78
+ // getUTCDay() always returns 0-6
79
+ return DAY_NAMES[date.getUTCDay()] as DayOfWeek;
80
+ }
81
+
82
+ /**
83
+ * Formats a date as YYYY-MM-DD string
84
+ */
85
+ export function formatDateString(date: Date): string {
86
+ const year = date.getFullYear();
87
+ const month = (date.getMonth() + 1).toString().padStart(2, "0");
88
+ const day = date.getDate().toString().padStart(2, "0");
89
+ return `${year}-${month}-${day}`;
90
+ }
91
+
92
+ /**
93
+ * Generates an array of day strings (YYYY-MM-DD) from a time horizon.
94
+ *
95
+ * @param horizon - The time horizon with start (inclusive) and end (inclusive) dates
96
+ * @returns Array of day strings in YYYY-MM-DD format
97
+ *
98
+ * @example
99
+ * ```typescript
100
+ * const days = generateDays({
101
+ * start: new Date('2025-01-01'),
102
+ * end: new Date('2025-01-04')
103
+ * });
104
+ * // Returns: ["2025-01-01", "2025-01-02", "2025-01-03", "2025-01-04"]
105
+ * ```
106
+ */
107
+ export function generateDays(horizon: { start: Date; end: Date }): string[] {
108
+ const days: string[] = [];
109
+ const current = new Date(horizon.start);
110
+
111
+ while (current <= horizon.end) {
112
+ days.push(formatDateString(current));
113
+ current.setDate(current.getDate() + 1);
114
+ }
115
+
116
+ return days;
117
+ }
118
+
119
+ /**
120
+ * Splits a time period into consecutive day ranges.
121
+ *
122
+ * Each range represents a single calendar day within the period from start to end.
123
+ * This is useful for rules that need to apply constraints on a per-day basis,
124
+ * such as maximum or minimum hours per day.
125
+ *
126
+ * @param start - The start date of the period (inclusive)
127
+ * @param end - The end date of the period (exclusive)
128
+ * @returns An array of [startDate, endDate] tuples, where each tuple represents
129
+ * a single day. The last range's end date will be the provided end date.
130
+ *
131
+ * @example
132
+ * ```typescript
133
+ * const ranges = splitPeriodIntoDays({
134
+ * start: new Date('2025-01-01'),
135
+ * end: new Date('2025-01-03')
136
+ * });
137
+ * // Returns:
138
+ * // [
139
+ * // [Date('2025-01-01'), Date('2025-01-02')],
140
+ * // [Date('2025-01-02'), Date('2025-01-03')]
141
+ * // ]
142
+ * ```
143
+ */
144
+ export function splitPeriodIntoDays({ start, end }: { start: Date; end: Date }): [Date, Date][] {
145
+ const ranges: [Date, Date][] = [];
146
+
147
+ let leftBound = new Date(start);
148
+ let done = false;
149
+
150
+ // Loop through each day
151
+ leftBound = new Date(start);
152
+ while (!done) {
153
+ // Create next day date
154
+ let rightBound = new Date(leftBound);
155
+ rightBound.setDate(rightBound.getDate() + 1);
156
+
157
+ if (rightBound >= end) {
158
+ rightBound = end;
159
+ done = true;
160
+ }
161
+
162
+ ranges.push([leftBound, rightBound]);
163
+ leftBound = rightBound;
164
+ }
165
+ return ranges;
166
+ }
167
+
168
+ /**
169
+ * Splits a time period into consecutive week ranges.
170
+ *
171
+ * Each range represents a week period starting on the specified day of the week.
172
+ * This is useful for rules that need to apply constraints on a per-week basis,
173
+ * such as maximum or minimum hours per week.
174
+ *
175
+ * The first range starts at the provided start date (not necessarily on weekStartsOn).
176
+ * Subsequent ranges align to the weekStartsOn day. The last range's end date will be
177
+ * the provided end date.
178
+ *
179
+ * @param start - The start date of the period (inclusive)
180
+ * @param end - The end date of the period (exclusive)
181
+ * @param weekStartsOn - The day of the week that weeks start on (e.g., "monday", "sunday")
182
+ * @returns An array of [startDate, endDate] tuples, where each tuple represents
183
+ * a week period aligned to the specified week start day.
184
+ *
185
+ * @example
186
+ * ```typescript
187
+ * const ranges = splitPeriodIntoWeeks({
188
+ * start: new Date('2025-01-01'), // Wednesday
189
+ * end: new Date('2025-01-15'),
190
+ * weekStartsOn: 'monday'
191
+ * });
192
+ * // Returns ranges starting from Jan 1 (Wed), then aligning to Mondays:
193
+ * // [
194
+ * // [Date('2025-01-01 Wed'), Date('2025-01-06 Mon')],
195
+ * // [Date('2025-01-06 Mon'), Date('2025-01-13 Mon')],
196
+ * // [Date('2025-01-13 Mon'), Date('2025-01-15 Wed')]
197
+ * // ]
198
+ * ```
199
+ */
200
+ export function splitPeriodIntoWeeks({
201
+ start,
202
+ end,
203
+ weekStartsOn,
204
+ }: {
205
+ start: Date;
206
+ end: Date;
207
+ weekStartsOn: DayOfWeek;
208
+ }): [Date, Date][] {
209
+ const DAYS_OF_WEEK: DayOfWeek[] = [
210
+ "sunday",
211
+ "monday",
212
+ "tuesday",
213
+ "wednesday",
214
+ "thursday",
215
+ "friday",
216
+ "saturday",
217
+ ];
218
+
219
+ const ranges: [Date, Date][] = [];
220
+
221
+ let leftBound = new Date(start);
222
+ let done = false;
223
+
224
+ const startDayIndex = DAYS_OF_WEEK.indexOf(weekStartsOn);
225
+
226
+ // Create weekly ranges
227
+ while (!done) {
228
+ const dayOfWeek = leftBound.getDay(); // 0 = Sunday, 1 = Monday, etc.
229
+ const daysToNextWeek = (7 - ((dayOfWeek - startDayIndex) % 7)) % 7;
230
+ const daysToAdd = daysToNextWeek === 0 ? 7 : daysToNextWeek;
231
+
232
+ let rightBound = new Date(leftBound);
233
+ rightBound.setDate(rightBound.getDate() + daysToAdd);
234
+
235
+ if (rightBound >= end) {
236
+ rightBound = end;
237
+ done = true;
238
+ }
239
+
240
+ ranges.push([leftBound, rightBound]);
241
+ leftBound = rightBound;
242
+ }
243
+ return ranges;
244
+ }
245
+
246
+ /**
247
+ * Checks if two DateTime ranges overlap in both date and time.
248
+ * Ranges overlap if they share any moment in time.
249
+ *
250
+ * Two ranges overlap if: range1.start < range2.end AND range2.start < range1.end
251
+ *
252
+ * @param range1 First time range with start (inclusive) and end (exclusive)
253
+ * @param range2 Second time range with start (inclusive) and end (exclusive)
254
+ * @returns true if ranges overlap, false otherwise
255
+ *
256
+ * @example
257
+ * ```typescript
258
+ * // Same day, overlapping times (9-17 overlaps with 12-20)
259
+ * dateTimeRangesOverlap(
260
+ * {
261
+ * start: { year: 2025, month: 6, day: 1, hours: 9, minutes: 0 },
262
+ * end: { year: 2025, month: 6, day: 1, hours: 17, minutes: 0 }
263
+ * },
264
+ * {
265
+ * start: { year: 2025, month: 6, day: 1, hours: 12, minutes: 0 },
266
+ * end: { year: 2025, month: 6, day: 1, hours: 20, minutes: 0 }
267
+ * }
268
+ * ); // true
269
+ *
270
+ * // Different days - no overlap
271
+ * dateTimeRangesOverlap(
272
+ * {
273
+ * start: { year: 2025, month: 6, day: 1, hours: 9, minutes: 0 },
274
+ * end: { year: 2025, month: 6, day: 1, hours: 17, minutes: 0 }
275
+ * },
276
+ * {
277
+ * start: { year: 2025, month: 6, day: 2, hours: 9, minutes: 0 },
278
+ * end: { year: 2025, month: 6, day: 2, hours: 17, minutes: 0 }
279
+ * }
280
+ * ); // false
281
+ *
282
+ * // Works naturally with Shift objects
283
+ * dateTimeRangesOverlap(
284
+ * { start: shift1.startDateTime, end: shift1.endDateTime },
285
+ * { start: shift2.startDateTime, end: shift2.endDateTime }
286
+ * );
287
+ * ```
288
+ */
289
+ export function dateTimeRangesOverlap(range1: DateTimeRange, range2: DateTimeRange): boolean {
290
+ // Use existing compareDateTimes for temporal comparison
291
+ // Ranges overlap if: start1 < end2 AND start2 < end1
292
+ return (
293
+ compareDateTimes(range1.start, range2.end) < 0 && compareDateTimes(range2.start, range1.end) < 0
294
+ );
295
+ }
296
+
297
+ /**
298
+ * Calculates the number of complete days between two dates
299
+ * @param start The start date
300
+ * @param end The end date
301
+ * @returns Number of complete days from start to end (can be negative if end < start)
302
+ *
303
+ * @example
304
+ * ```typescript
305
+ * daysBetween(new Date('2025-01-01'), new Date('2025-01-05')); // 4
306
+ * ```
307
+ */
308
+ export function daysBetween(start: Date, end: Date): number {
309
+ const ms = end.getTime() - start.getTime();
310
+ return Math.floor(ms / (1000 * 60 * 60 * 24));
311
+ }
312
+
313
+ /**
314
+ * Adds a number of minutes to a base date and returns a DateTime.
315
+ * Treats the base date as a reference point (typically midnight of horizon start),
316
+ * and the minutes parameter as absolute minutes from that point.
317
+ *
318
+ * @param baseDate The starting date (used as reference point)
319
+ * @param minutes Absolute minutes from the base date
320
+ * @returns DateTime representing the result
321
+ *
322
+ * @example
323
+ * ```typescript
324
+ * // Add 90 minutes from midnight
325
+ * addMinutesToDate(new Date('2025-01-01'), 90);
326
+ * // Returns: { year: 2025, month: 1, day: 1, hours: 1, minutes: 30 }
327
+ *
328
+ * // Add 1500 minutes (spans to next day)
329
+ * addMinutesToDate(new Date('2025-01-01'), 1500);
330
+ * // Returns: { year: 2025, month: 1, day: 2, hours: 1, minutes: 0 }
331
+ * ```
332
+ */
333
+ export function addMinutesToDate(baseDate: Date, minutes: number): DateTime {
334
+ const days = Math.floor(minutes / 1440);
335
+ const minutesInDay = minutes % 1440;
336
+
337
+ const targetDate = new Date(baseDate);
338
+ targetDate.setDate(targetDate.getDate() + days);
339
+
340
+ return {
341
+ year: targetDate.getFullYear(),
342
+ month: targetDate.getMonth() + 1,
343
+ day: targetDate.getDate(),
344
+ hours: Math.floor(minutesInDay / 60),
345
+ minutes: minutesInDay % 60,
346
+ };
347
+ }
348
+
349
+ /**
350
+ * Returns the points where a range should be split, filtered to within [start, end).
351
+ * Always includes range start. Sorted ascending.
352
+ */
353
+ export function splitPoints([start, end]: [number, number], splitAt: number[]): number[] {
354
+ const points = new Set<number>([start]);
355
+
356
+ for (const p of splitAt) {
357
+ if (p > start && p < end) points.add(p);
358
+ }
359
+
360
+ return [...points].toSorted((a, b) => a - b);
361
+ }
362
+
363
+ /**
364
+ * Computes the list of day strings (YYYY-MM-DD) from a SchedulingPeriod.
365
+ *
366
+ * For date ranges, generates all days between start and end (inclusive),
367
+ * optionally filtering to specific days of the week.
368
+ *
369
+ * For specific dates, returns the dates as-is (sorted).
370
+ *
371
+ * @param period - The scheduling period specification
372
+ * @returns Array of day strings in YYYY-MM-DD format, sorted chronologically
373
+ *
374
+ * @example Date range with day-of-week filter
375
+ * ```typescript
376
+ * const days = resolveDaysFromPeriod({
377
+ * dateRange: { start: '2025-02-03', end: '2025-02-09' },
378
+ * daysOfWeek: ['wednesday', 'friday'],
379
+ * });
380
+ * // Returns: ['2025-02-05', '2025-02-07'] (Wed and Fri only)
381
+ * ```
382
+ *
383
+ * @example Date range without filter
384
+ * ```typescript
385
+ * const days = resolveDaysFromPeriod({
386
+ * dateRange: { start: '2025-02-03', end: '2025-02-05' },
387
+ * });
388
+ * // Returns: ['2025-02-03', '2025-02-04', '2025-02-05']
389
+ * ```
390
+ *
391
+ * @example Specific dates
392
+ * ```typescript
393
+ * const days = resolveDaysFromPeriod({
394
+ * specificDates: ['2025-02-07', '2025-02-03', '2025-02-10'],
395
+ * });
396
+ * // Returns: ['2025-02-03', '2025-02-07', '2025-02-10'] (sorted)
397
+ * ```
398
+ */
399
+ export function resolveDaysFromPeriod(period: SchedulingPeriod): string[] {
400
+ if ("specificDates" in period && period.specificDates) {
401
+ return [...period.specificDates].toSorted();
402
+ }
403
+
404
+ const { dateRange, daysOfWeek } = period;
405
+ const startDate = new Date(`${dateRange.start}T00:00:00Z`);
406
+ const endDate = new Date(`${dateRange.end}T00:00:00Z`);
407
+
408
+ const allowedDays = daysOfWeek ? new Set(daysOfWeek) : null;
409
+ const days: string[] = [];
410
+
411
+ const current = new Date(startDate);
412
+ while (current <= endDate) {
413
+ const dayOfWeek = toDayOfWeekUTC(current);
414
+
415
+ if (!allowedDays || allowedDays.has(dayOfWeek)) {
416
+ const year = current.getUTCFullYear();
417
+ const month = (current.getUTCMonth() + 1).toString().padStart(2, "0");
418
+ const day = current.getUTCDate().toString().padStart(2, "0");
419
+ days.push(`${year}-${month}-${day}`);
420
+ }
421
+
422
+ current.setUTCDate(current.getUTCDate() + 1);
423
+ }
424
+
425
+ return days;
426
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Error thrown when Google OR Tools scheduling API requests fail.
3
+ *
4
+ * Contains the HTTP status code and raw response data from the API for debugging.
5
+ * Common causes include infeasible constraints, invalid requests, or API unavailability.
6
+ */
7
+ export class ORSchedulingError extends Error {
8
+ public readonly status: number;
9
+ public readonly data: unknown;
10
+
11
+ constructor(message: string, status: number, data: unknown) {
12
+ super(message);
13
+ this.name = "ORSchedulingError";
14
+ this.status = status;
15
+ this.data = data;
16
+ }
17
+ }