@nowline/layout 0.8.5 → 0.9.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 (339) hide show
  1. package/dist/axis-columns.d.ts +70 -0
  2. package/dist/axis-columns.d.ts.map +1 -0
  3. package/dist/axis-columns.js +104 -0
  4. package/dist/axis-columns.js.map +1 -0
  5. package/dist/band-scale.d.ts.map +1 -1
  6. package/dist/calendar-resolver.d.ts +27 -0
  7. package/dist/calendar-resolver.d.ts.map +1 -0
  8. package/dist/calendar-resolver.js +132 -0
  9. package/dist/calendar-resolver.js.map +1 -0
  10. package/dist/calendar.d.ts +0 -19
  11. package/dist/calendar.d.ts.map +1 -1
  12. package/dist/calendar.js +4 -44
  13. package/dist/calendar.js.map +1 -1
  14. package/dist/canvas-width.d.ts +23 -0
  15. package/dist/canvas-width.d.ts.map +1 -0
  16. package/dist/canvas-width.js +26 -0
  17. package/dist/canvas-width.js.map +1 -0
  18. package/dist/capacity.d.ts +28 -0
  19. package/dist/capacity.d.ts.map +1 -1
  20. package/dist/capacity.js +64 -29
  21. package/dist/capacity.js.map +1 -1
  22. package/dist/chart-translation.d.ts +22 -0
  23. package/dist/chart-translation.d.ts.map +1 -0
  24. package/dist/chart-translation.js +83 -0
  25. package/dist/chart-translation.js.map +1 -0
  26. package/dist/container-header-geometry.d.ts +64 -0
  27. package/dist/container-header-geometry.d.ts.map +1 -0
  28. package/dist/container-header-geometry.js +89 -0
  29. package/dist/container-header-geometry.js.map +1 -0
  30. package/dist/dsl-utils.d.ts +3 -4
  31. package/dist/dsl-utils.d.ts.map +1 -1
  32. package/dist/dsl-utils.js +8 -25
  33. package/dist/dsl-utils.js.map +1 -1
  34. package/dist/edge-routing.d.ts +69 -78
  35. package/dist/edge-routing.d.ts.map +1 -1
  36. package/dist/edge-routing.js +271 -401
  37. package/dist/edge-routing.js.map +1 -1
  38. package/dist/frame-tab-geometry.d.ts +24 -14
  39. package/dist/frame-tab-geometry.d.ts.map +1 -1
  40. package/dist/frame-tab-geometry.js +69 -23
  41. package/dist/frame-tab-geometry.js.map +1 -1
  42. package/dist/header-card-geometry.d.ts +12 -0
  43. package/dist/header-card-geometry.d.ts.map +1 -1
  44. package/dist/header-card-geometry.js +14 -2
  45. package/dist/header-card-geometry.js.map +1 -1
  46. package/dist/header-logo.d.ts +28 -0
  47. package/dist/header-logo.d.ts.map +1 -0
  48. package/dist/header-logo.js +51 -0
  49. package/dist/header-logo.js.map +1 -0
  50. package/dist/i18n.d.ts +11 -0
  51. package/dist/i18n.d.ts.map +1 -1
  52. package/dist/i18n.js +11 -1
  53. package/dist/i18n.js.map +1 -1
  54. package/dist/index.d.ts +20 -8
  55. package/dist/index.d.ts.map +1 -1
  56. package/dist/index.js +17 -7
  57. package/dist/index.js.map +1 -1
  58. package/dist/inline-date-pin-geometry.d.ts +19 -11
  59. package/dist/inline-date-pin-geometry.d.ts.map +1 -1
  60. package/dist/inline-date-pin-geometry.js +45 -46
  61. package/dist/inline-date-pin-geometry.js.map +1 -1
  62. package/dist/item-bar-geometry.d.ts +192 -4
  63. package/dist/item-bar-geometry.d.ts.map +1 -1
  64. package/dist/item-bar-geometry.js +258 -5
  65. package/dist/item-bar-geometry.js.map +1 -1
  66. package/dist/item-port-geometry.d.ts +13 -0
  67. package/dist/item-port-geometry.d.ts.map +1 -0
  68. package/dist/item-port-geometry.js +63 -0
  69. package/dist/item-port-geometry.js.map +1 -0
  70. package/dist/layout-context.d.ts +115 -51
  71. package/dist/layout-context.d.ts.map +1 -1
  72. package/dist/layout-context.js +96 -5
  73. package/dist/layout-context.js.map +1 -1
  74. package/dist/layout-insights.d.ts.map +1 -1
  75. package/dist/layout-insights.js +73 -1
  76. package/dist/layout-insights.js.map +1 -1
  77. package/dist/layout.d.ts +35 -0
  78. package/dist/layout.d.ts.map +1 -1
  79. package/dist/layout.js +18 -1315
  80. package/dist/layout.js.map +1 -1
  81. package/dist/nodes/anchor-node.d.ts.map +1 -1
  82. package/dist/nodes/anchor-node.js +11 -4
  83. package/dist/nodes/anchor-node.js.map +1 -1
  84. package/dist/nodes/content-extent.d.ts +24 -0
  85. package/dist/nodes/content-extent.d.ts.map +1 -0
  86. package/dist/nodes/content-extent.js +100 -0
  87. package/dist/nodes/content-extent.js.map +1 -0
  88. package/dist/nodes/dependency-edges.d.ts +6 -0
  89. package/dist/nodes/dependency-edges.d.ts.map +1 -0
  90. package/dist/nodes/dependency-edges.js +148 -0
  91. package/dist/nodes/dependency-edges.js.map +1 -0
  92. package/dist/nodes/footnote-node.d.ts +24 -3
  93. package/dist/nodes/footnote-node.d.ts.map +1 -1
  94. package/dist/nodes/footnote-node.js +129 -25
  95. package/dist/nodes/footnote-node.js.map +1 -1
  96. package/dist/nodes/group-node.d.ts +10 -21
  97. package/dist/nodes/group-node.d.ts.map +1 -1
  98. package/dist/nodes/group-node.js +79 -154
  99. package/dist/nodes/group-node.js.map +1 -1
  100. package/dist/nodes/header-node.d.ts +56 -0
  101. package/dist/nodes/header-node.d.ts.map +1 -0
  102. package/dist/nodes/header-node.js +163 -0
  103. package/dist/nodes/header-node.js.map +1 -0
  104. package/dist/nodes/include-node.d.ts +22 -12
  105. package/dist/nodes/include-node.d.ts.map +1 -1
  106. package/dist/nodes/include-node.js +95 -62
  107. package/dist/nodes/include-node.js.map +1 -1
  108. package/dist/nodes/item-measure.d.ts +135 -0
  109. package/dist/nodes/item-measure.d.ts.map +1 -0
  110. package/dist/nodes/item-measure.js +514 -0
  111. package/dist/nodes/item-measure.js.map +1 -0
  112. package/dist/nodes/item-node.d.ts +162 -9
  113. package/dist/nodes/item-node.d.ts.map +1 -1
  114. package/dist/nodes/item-node.js +256 -69
  115. package/dist/nodes/item-node.js.map +1 -1
  116. package/dist/nodes/marker-band.d.ts +118 -0
  117. package/dist/nodes/marker-band.d.ts.map +1 -0
  118. package/dist/nodes/marker-band.js +303 -0
  119. package/dist/nodes/marker-band.js.map +1 -0
  120. package/dist/nodes/marker-geometry.js +1 -1
  121. package/dist/nodes/marker-geometry.js.map +1 -1
  122. package/dist/nodes/milestone-node.d.ts +112 -15
  123. package/dist/nodes/milestone-node.d.ts.map +1 -1
  124. package/dist/nodes/milestone-node.js +246 -95
  125. package/dist/nodes/milestone-node.js.map +1 -1
  126. package/dist/nodes/nowline-node.d.ts +8 -0
  127. package/dist/nodes/nowline-node.d.ts.map +1 -0
  128. package/dist/nodes/nowline-node.js +74 -0
  129. package/dist/nodes/nowline-node.js.map +1 -0
  130. package/dist/nodes/parallel-node.d.ts +7 -12
  131. package/dist/nodes/parallel-node.d.ts.map +1 -1
  132. package/dist/nodes/parallel-node.js +66 -32
  133. package/dist/nodes/parallel-node.js.map +1 -1
  134. package/dist/nodes/region-markers.d.ts +41 -0
  135. package/dist/nodes/region-markers.d.ts.map +1 -0
  136. package/dist/nodes/region-markers.js +130 -0
  137. package/dist/nodes/region-markers.js.map +1 -0
  138. package/dist/nodes/roadmap-node.d.ts +9 -72
  139. package/dist/nodes/roadmap-node.d.ts.map +1 -1
  140. package/dist/nodes/roadmap-node.js +38 -788
  141. package/dist/nodes/roadmap-node.js.map +1 -1
  142. package/dist/nodes/row-track.d.ts +33 -0
  143. package/dist/nodes/row-track.d.ts.map +1 -0
  144. package/dist/nodes/row-track.js +170 -0
  145. package/dist/nodes/row-track.js.map +1 -0
  146. package/dist/nodes/sequence-item.d.ts +25 -0
  147. package/dist/nodes/sequence-item.d.ts.map +1 -0
  148. package/dist/nodes/sequence-item.js +396 -0
  149. package/dist/nodes/sequence-item.js.map +1 -0
  150. package/dist/nodes/sequence-one.d.ts +5 -0
  151. package/dist/nodes/sequence-one.d.ts.map +1 -0
  152. package/dist/nodes/sequence-one.js +26 -0
  153. package/dist/nodes/sequence-one.js.map +1 -0
  154. package/dist/nodes/swimlane-node.d.ts +5 -17
  155. package/dist/nodes/swimlane-node.d.ts.map +1 -1
  156. package/dist/nodes/swimlane-node.js +62 -147
  157. package/dist/nodes/swimlane-node.js.map +1 -1
  158. package/dist/nodes/wave-node.d.ts +52 -0
  159. package/dist/nodes/wave-node.d.ts.map +1 -0
  160. package/dist/nodes/wave-node.js +483 -0
  161. package/dist/nodes/wave-node.js.map +1 -0
  162. package/dist/non-working-display.d.ts +14 -0
  163. package/dist/non-working-display.d.ts.map +1 -0
  164. package/dist/non-working-display.js +28 -0
  165. package/dist/non-working-display.js.map +1 -0
  166. package/dist/phases/date-window.d.ts +48 -0
  167. package/dist/phases/date-window.d.ts.map +1 -0
  168. package/dist/phases/date-window.js +130 -0
  169. package/dist/phases/date-window.js.map +1 -0
  170. package/dist/phases/finalize.d.ts +198 -0
  171. package/dist/phases/finalize.d.ts.map +1 -0
  172. package/dist/phases/finalize.js +1058 -0
  173. package/dist/phases/finalize.js.map +1 -0
  174. package/dist/phases/measure.d.ts +77 -0
  175. package/dist/phases/measure.d.ts.map +1 -0
  176. package/dist/phases/measure.js +222 -0
  177. package/dist/phases/measure.js.map +1 -0
  178. package/dist/phases/place.d.ts +28 -0
  179. package/dist/phases/place.d.ts.map +1 -0
  180. package/dist/phases/place.js +225 -0
  181. package/dist/phases/place.js.map +1 -0
  182. package/dist/renderable.d.ts.map +1 -1
  183. package/dist/renderable.js +23 -3
  184. package/dist/renderable.js.map +1 -1
  185. package/dist/resolve-today.d.ts +11 -1
  186. package/dist/resolve-today.d.ts.map +1 -1
  187. package/dist/resolve-today.js +26 -12
  188. package/dist/resolve-today.js.map +1 -1
  189. package/dist/row-packer.d.ts +42 -16
  190. package/dist/row-packer.d.ts.map +1 -1
  191. package/dist/row-packer.js +51 -78
  192. package/dist/row-packer.js.map +1 -1
  193. package/dist/schedule.d.ts +58 -1
  194. package/dist/schedule.d.ts.map +1 -1
  195. package/dist/schedule.js +280 -106
  196. package/dist/schedule.js.map +1 -1
  197. package/dist/start-rules.d.ts +27 -0
  198. package/dist/start-rules.d.ts.map +1 -0
  199. package/dist/start-rules.js +91 -0
  200. package/dist/start-rules.js.map +1 -0
  201. package/dist/start-terms.d.ts +23 -0
  202. package/dist/start-terms.d.ts.map +1 -0
  203. package/dist/start-terms.js +27 -0
  204. package/dist/start-terms.js.map +1 -0
  205. package/dist/status-dot.d.ts +46 -0
  206. package/dist/status-dot.d.ts.map +1 -0
  207. package/dist/status-dot.js +78 -0
  208. package/dist/status-dot.js.map +1 -0
  209. package/dist/style-resolution.d.ts.map +1 -1
  210. package/dist/style-resolution.js +23 -43
  211. package/dist/style-resolution.js.map +1 -1
  212. package/dist/text-measure.d.ts +28 -0
  213. package/dist/text-measure.d.ts.map +1 -0
  214. package/dist/text-measure.js +70 -0
  215. package/dist/text-measure.js.map +1 -0
  216. package/dist/themes/contrast.d.ts +4 -0
  217. package/dist/themes/contrast.d.ts.map +1 -0
  218. package/dist/themes/contrast.js +32 -0
  219. package/dist/themes/contrast.js.map +1 -0
  220. package/dist/themes/dark.d.ts.map +1 -1
  221. package/dist/themes/dark.js +28 -1
  222. package/dist/themes/dark.js.map +1 -1
  223. package/dist/themes/grayscale.d.ts.map +1 -1
  224. package/dist/themes/grayscale.js +28 -1
  225. package/dist/themes/grayscale.js.map +1 -1
  226. package/dist/themes/index.d.ts +4 -11
  227. package/dist/themes/index.d.ts.map +1 -1
  228. package/dist/themes/index.js +6 -4
  229. package/dist/themes/index.js.map +1 -1
  230. package/dist/themes/light.d.ts.map +1 -1
  231. package/dist/themes/light.js +28 -1
  232. package/dist/themes/light.js.map +1 -1
  233. package/dist/themes/shape.d.ts +31 -0
  234. package/dist/themes/shape.d.ts.map +1 -1
  235. package/dist/themes/shared.d.ts +44 -50
  236. package/dist/themes/shared.d.ts.map +1 -1
  237. package/dist/themes/shared.js +79 -83
  238. package/dist/themes/shared.js.map +1 -1
  239. package/dist/time-scale.d.ts +42 -6
  240. package/dist/time-scale.d.ts.map +1 -1
  241. package/dist/time-scale.js +109 -17
  242. package/dist/time-scale.js.map +1 -1
  243. package/dist/types.d.ts +528 -11
  244. package/dist/types.d.ts.map +1 -1
  245. package/dist/view-preset.d.ts +61 -6
  246. package/dist/view-preset.d.ts.map +1 -1
  247. package/dist/view-preset.js +439 -24
  248. package/dist/view-preset.js.map +1 -1
  249. package/dist/wave-barrier.d.ts +107 -0
  250. package/dist/wave-barrier.d.ts.map +1 -0
  251. package/dist/wave-barrier.js +174 -0
  252. package/dist/wave-barrier.js.map +1 -0
  253. package/dist/wave-hatch.d.ts +4 -0
  254. package/dist/wave-hatch.d.ts.map +1 -0
  255. package/dist/wave-hatch.js +21 -0
  256. package/dist/wave-hatch.js.map +1 -0
  257. package/dist/wave-layout.d.ts +99 -0
  258. package/dist/wave-layout.d.ts.map +1 -0
  259. package/dist/wave-layout.js +130 -0
  260. package/dist/wave-layout.js.map +1 -0
  261. package/dist/working-calendar.d.ts +128 -4
  262. package/dist/working-calendar.d.ts.map +1 -1
  263. package/dist/working-calendar.js +725 -51
  264. package/dist/working-calendar.js.map +1 -1
  265. package/dist/working-span.d.ts +11 -0
  266. package/dist/working-span.d.ts.map +1 -0
  267. package/dist/working-span.js +18 -0
  268. package/dist/working-span.js.map +1 -0
  269. package/package.json +7 -7
  270. package/src/axis-columns.ts +140 -0
  271. package/src/calendar-resolver.ts +161 -0
  272. package/src/calendar.ts +4 -45
  273. package/src/canvas-width.ts +36 -0
  274. package/src/capacity.ts +71 -30
  275. package/src/chart-translation.ts +88 -0
  276. package/src/container-header-geometry.ts +116 -0
  277. package/src/dsl-utils.ts +8 -26
  278. package/src/edge-routing.ts +347 -491
  279. package/src/frame-tab-geometry.ts +78 -25
  280. package/src/header-card-geometry.ts +17 -2
  281. package/src/header-logo.ts +67 -0
  282. package/src/i18n.ts +22 -1
  283. package/src/index.ts +97 -12
  284. package/src/inline-date-pin-geometry.ts +55 -58
  285. package/src/item-bar-geometry.ts +341 -5
  286. package/src/item-port-geometry.ts +69 -0
  287. package/src/layout-context.ts +210 -72
  288. package/src/layout-insights.ts +109 -2
  289. package/src/layout.ts +57 -1530
  290. package/src/nodes/anchor-node.ts +11 -4
  291. package/src/nodes/content-extent.ts +107 -0
  292. package/src/nodes/dependency-edges.ts +171 -0
  293. package/src/nodes/footnote-node.ts +171 -31
  294. package/src/nodes/group-node.ts +92 -215
  295. package/src/nodes/header-node.ts +255 -0
  296. package/src/nodes/include-node.ts +124 -110
  297. package/src/nodes/item-measure.ts +723 -0
  298. package/src/nodes/item-node.ts +358 -80
  299. package/src/nodes/marker-band.ts +406 -0
  300. package/src/nodes/marker-geometry.ts +1 -1
  301. package/src/nodes/milestone-node.ts +321 -136
  302. package/src/nodes/nowline-node.ts +87 -0
  303. package/src/nodes/parallel-node.ts +69 -39
  304. package/src/nodes/region-markers.ts +194 -0
  305. package/src/nodes/roadmap-node.ts +46 -950
  306. package/src/nodes/row-track.ts +217 -0
  307. package/src/nodes/sequence-item.ts +433 -0
  308. package/src/nodes/sequence-one.ts +40 -0
  309. package/src/nodes/swimlane-node.ts +66 -206
  310. package/src/nodes/wave-node.ts +609 -0
  311. package/src/non-working-display.ts +32 -0
  312. package/src/phases/date-window.ts +191 -0
  313. package/src/phases/finalize.ts +1303 -0
  314. package/src/phases/measure.ts +329 -0
  315. package/src/phases/place.ts +318 -0
  316. package/src/renderable.ts +23 -3
  317. package/src/resolve-today.ts +28 -25
  318. package/src/row-packer.ts +85 -84
  319. package/src/schedule.ts +362 -114
  320. package/src/start-rules.ts +113 -0
  321. package/src/start-terms.ts +38 -0
  322. package/src/status-dot.ts +81 -0
  323. package/src/style-resolution.ts +27 -57
  324. package/src/text-measure.ts +71 -0
  325. package/src/themes/contrast.ts +34 -0
  326. package/src/themes/dark.ts +28 -1
  327. package/src/themes/grayscale.ts +28 -1
  328. package/src/themes/index.ts +10 -7
  329. package/src/themes/light.ts +28 -1
  330. package/src/themes/shape.ts +38 -0
  331. package/src/themes/shared.ts +85 -93
  332. package/src/time-scale.ts +117 -19
  333. package/src/types.ts +529 -20
  334. package/src/view-preset.ts +520 -27
  335. package/src/wave-barrier.ts +242 -0
  336. package/src/wave-hatch.ts +25 -0
  337. package/src/wave-layout.ts +203 -0
  338. package/src/working-calendar.ts +875 -57
  339. package/src/working-span.ts +19 -0
@@ -1,58 +1,149 @@
1
- // WorkingCalendar primitive — strategy interface for non-continuous
2
- // time models (skip weekends, holidays, custom shutdowns). Today the
3
- // rendering pipeline treats time as continuous; the existing
4
- // `CalendarConfig` only changes how `1w` literals expand into days,
5
- // not whether ticks skip weekends. WorkingCalendar establishes the
6
- // API surface for the future weekend/holiday work without making
7
- // `TimeScale` and `ViewPreset` aware of `CalendarConfig` directly.
1
+ // WorkingCalendar: the two jobs a calendar does (specs/working-calendar.md
2
+ // §3.1).
8
3
  //
9
- // The default factories `continuousCalendar()` and
10
- // `fromCalendarConfig(cal)` are pass-through: every calendar day is a
11
- // working day, units expand using `CalendarConfig.daysPer*`. Future
12
- // non-continuous calendars override `nextWorkingDay` and `addUnits`
13
- // without changing the consumer surface.
14
- import { addDays as addCalendarDays } from './calendar.js';
15
- export function fromCalendarConfig(cal) {
16
- return {
17
- daysPerUnit: (unit) => daysPerUnitForCalendar(unit, cal),
18
- addUnits: (date, count, unit) => addCalendarDays(date, count * daysPerUnitForCalendar(unit, cal)),
19
- // The continuous model used today treats every calendar day as a
20
- // working day; non-continuous calendars will override this to
21
- // implement weekend/holiday skipping when the work lands.
22
- isWorkingDay: () => true,
23
- };
4
+ // 1. Duration arithmetic. `daysPerUnit` turns `1w` / `1m` / `1q` / `1y` into
5
+ // a count of working days from the `CalendarConfig` preset. The
6
+ // non-working rules never change it.
7
+ // 2. Non-working days. A list of `CalendarRule`s (the preset weekend plus
8
+ // holidays, closures, recurring windows and working exceptions) decides
9
+ // which UTC dates consume no work. `workingIndexOf` and
10
+ // `dateAtWorkingIndex` map between dates and a working-day index;
11
+ // `nonWorkingRuns` lists the maximal non-working stretches to draw, and
12
+ // `workingExceptionRuns` the days titled `working` rules put back.
13
+ //
14
+ // Precedence is resolved once, at construction, into a sorted list of
15
+ // disjoint pieces covering every day: a weekly mask (the open-ended week or
16
+ // a bounded recurrence pattern) or an all-working / all-non-working dated
17
+ // stretch. Queries cost O(pieces in the span) with whole-week arithmetic
18
+ // inside a piece; nothing walks day by day except the one-off pass over the
19
+ // finite days of titled dated `working` rules.
20
+ //
21
+ // Identity guarantee: a calendar with no non-working day (`calendar:full`,
22
+ // `calendar:custom`, `continuousCalendar()`, or rules that cancel out)
23
+ // delegates to `daysBetween` / `addDays`, so its answers, including the
24
+ // rounding and `addDays`' truncation of fractional days, match continuous
25
+ // calendar-day time exactly.
26
+ import { addDays, daysBetween } from './calendar.js';
27
+ /** The rules a calendar preset implies: the weekend for business, nothing otherwise. */
28
+ export function presetRules(mode) {
29
+ if (mode !== 'business')
30
+ return [];
31
+ return [{ working: false, every: new Set([0, 6]), id: 'weekend', title: 'Weekend' }];
32
+ }
33
+ /**
34
+ * Build the working calendar for a resolved `CalendarConfig`. `rules`
35
+ * defaults to the preset's (the weekend for business). Malformed rules are
36
+ * dropped silently because included files are parsed without validation;
37
+ * throws `RangeError` for a weekday outside 0..6 or an unbounded stretch
38
+ * with no working weekday.
39
+ */
40
+ export function fromCalendarConfig(cal, rules = presetRules(cal.mode)) {
41
+ return buildCalendar((unit) => daysPerUnitForCalendar(unit, cal), rules);
24
42
  }
43
+ /** Every day is a working day; units use the full-calendar day counts. */
25
44
  export function continuousCalendar() {
26
- return {
27
- daysPerUnit: (unit) => {
28
- switch (unit) {
29
- case 'days':
30
- return 1;
31
- case 'weeks':
32
- return 7;
33
- case 'months':
34
- return 30;
35
- case 'quarters':
36
- return 91;
37
- case 'years':
38
- return 365;
39
- }
40
- },
41
- addUnits: (date, count, unit) => {
42
- const days = count *
43
- (unit === 'days'
44
- ? 1
45
- : unit === 'weeks'
46
- ? 7
47
- : unit === 'months'
48
- ? 30
49
- : unit === 'quarters'
50
- ? 91
51
- : 365);
52
- return addCalendarDays(date, days);
53
- },
54
- isWorkingDay: () => true,
45
+ return buildCalendar(continuousDaysPerUnit, []);
46
+ }
47
+ /**
48
+ * The exclusive end date of a span of working-day indices [s, e) from
49
+ * `base` (specs/working-calendar.md §5.2): the day after the last whole
50
+ * working day the span covers, so a Mon–Fri item ends on Saturday. A span
51
+ * covering no whole working day ends on its start date. Indices floor, as
52
+ * in `dateAtWorkingIndex`.
53
+ *
54
+ * On the identity path it is `addDays(base, e)`, truncation included, so
55
+ * calendar-day spans keep their exact dates.
56
+ */
57
+ export function spanEndDate(calendar, base, s, e) {
58
+ if (!calendar.hasNonWorkingDays)
59
+ return addDays(base, e);
60
+ const last = Math.floor(e);
61
+ if (last <= Math.floor(s))
62
+ return calendar.dateAtWorkingIndex(base, s);
63
+ return addDays(calendar.dateAtWorkingIndex(base, last - 1), 1);
64
+ }
65
+ /**
66
+ * The days in the horizon whose working state differs from the open-ended
67
+ * week (`workingWeekdays`), as maximal runs split wherever the state flips.
68
+ *
69
+ * The horizon is [from, to) together with every finite bound of every rule:
70
+ * its `dates`, and its `start` / `end` when set. A half-open bounded
71
+ * recurrence is therefore expanded only inside that horizon, so a declared
72
+ * holiday outside the roadmap window is still listed and nothing iterates
73
+ * forever. Titles come from the dated or ranged rules (no `every`) whose
74
+ * `working` equals the run's state and that cover a day of it, in
75
+ * `working.rules` order, de-duplicated.
76
+ */
77
+ export function calendarExceptions(working, from, to) {
78
+ const windows = [];
79
+ const add = (start, end) => {
80
+ if (!Number.isNaN(start) && !Number.isNaN(end) && start < end)
81
+ windows.push({ start, end });
82
+ };
83
+ add(dayNumber(from), dayNumber(to));
84
+ for (const rule of working.rules) {
85
+ for (const date of rule.dates ?? [])
86
+ add(dayNumber(date), dayNumber(date) + 1);
87
+ const start = rule.start === undefined ? undefined : dayNumber(rule.start);
88
+ const end = rule.end === undefined ? undefined : dayNumber(rule.end);
89
+ if (start !== undefined)
90
+ add(start, start + 1);
91
+ if (end !== undefined)
92
+ add(end, end + 1);
93
+ if (start !== undefined && end !== undefined)
94
+ add(start, end + 1);
95
+ }
96
+ windows.sort((a, b) => a.start - b.start || a.end - b.end);
97
+ const out = [];
98
+ let open;
99
+ const close = () => {
100
+ if (!open)
101
+ return;
102
+ const titles = exceptionTitles(working.rules, open.from, open.through, open.working);
103
+ out.push({
104
+ from: dateOfDay(open.from),
105
+ through: dateOfDay(open.through),
106
+ working: open.working,
107
+ ...(titles.length > 0 ? { titles } : {}),
108
+ });
109
+ open = undefined;
55
110
  };
111
+ let day = -Infinity;
112
+ for (const window of windows) {
113
+ for (let d = Math.max(day, window.start); d < window.end; d++) {
114
+ const state = working.isWorkingDay(dateOfDay(d));
115
+ if (state === working.workingWeekdays.has(weekdayOf(d))) {
116
+ close();
117
+ }
118
+ else if (open && open.through === d - 1 && open.working === state) {
119
+ open.through = d;
120
+ }
121
+ else {
122
+ close();
123
+ open = { from: d, through: d, working: state };
124
+ }
125
+ day = d + 1;
126
+ }
127
+ }
128
+ close();
129
+ return out;
130
+ }
131
+ /** Titles of the dated or ranged rules of `state` that cover a day in [from, through]. */
132
+ function exceptionTitles(rules, from, through, state) {
133
+ const titles = [];
134
+ for (const rule of rules) {
135
+ if (rule.every !== undefined || rule.working !== state || rule.title === undefined)
136
+ continue;
137
+ const covers = rule.dates
138
+ ? rule.dates.some((date) => dayNumber(date) >= from && dayNumber(date) <= through)
139
+ : rule.start !== undefined &&
140
+ rule.end !== undefined &&
141
+ dayNumber(rule.start) <= through &&
142
+ dayNumber(rule.end) >= from;
143
+ if (covers && !titles.includes(rule.title))
144
+ titles.push(rule.title);
145
+ }
146
+ return titles;
56
147
  }
57
148
  export function daysPerUnit(unit, cal) {
58
149
  return daysPerUnitForCalendar(unit, cal);
@@ -71,4 +162,587 @@ function daysPerUnitForCalendar(unit, cal) {
71
162
  return cal.daysPerYear;
72
163
  }
73
164
  }
165
+ /** Calendar days per `1<unit>` when every day counts (`calendar:full`). */
166
+ export function continuousDaysPerUnit(unit) {
167
+ switch (unit) {
168
+ case 'days':
169
+ return 1;
170
+ case 'weeks':
171
+ return 7;
172
+ case 'months':
173
+ return 30;
174
+ case 'quarters':
175
+ return 91;
176
+ case 'years':
177
+ return 365;
178
+ }
179
+ }
180
+ // ---------------------------------------------------------------------------
181
+ // Construction.
182
+ // ---------------------------------------------------------------------------
183
+ const DAY_MS = 86_400_000;
184
+ const DAYS_PER_WEEK = 7;
185
+ /** Day 0 (1970-01-01) is a Thursday. */
186
+ const EPOCH_WEEKDAY = 4;
187
+ /** Beyond this many working days every result is outside the Date range. */
188
+ const MAX_WORKING_INDEX = 1e8;
189
+ /** A remainder scan inside one weekly piece needs at most 7 steps. */
190
+ const MAX_REMAINDER_STEPS = 14;
191
+ function buildCalendar(unitDays, input) {
192
+ const normalized = [];
193
+ for (const rule of input) {
194
+ const result = normalizeRule(rule);
195
+ if (result)
196
+ normalized.push(result);
197
+ }
198
+ const rules = normalized.map((rule) => rule.copy);
199
+ const openMask = openWeekMask(normalized);
200
+ const segments = recurringSegments(openMask, boundedSpans(normalized));
201
+ const pieces = overlay(segments, datedSegments(normalized));
202
+ const exceptionRuns = titledWorkingRuns(normalized, segments);
203
+ const workingExceptionRuns = (from, to) => runsIntersecting(exceptionRuns, from, to);
204
+ const first = pieces[0];
205
+ const last = pieces[pieces.length - 1];
206
+ for (const piece of [first, last]) {
207
+ if (piece.kind !== 'mask' || piece.workingPerWeek === 0) {
208
+ throw new RangeError('working calendar: an unbounded stretch has no working weekday');
209
+ }
210
+ }
211
+ const hasNonWorkingDays = pieces.some((piece) => piece.kind === 'non-working' ||
212
+ (piece.kind === 'mask' && piece.workingPerWeek < DAYS_PER_WEEK));
213
+ const workingWeekdays = weekdaysOutside(openMask);
214
+ if (!hasNonWorkingDays) {
215
+ return identityCalendar(unitDays, rules, workingWeekdays, workingExceptionRuns);
216
+ }
217
+ const index = new WorkingDayIndex(pieces, titledRules(normalized, false));
218
+ const dateAtWorkingIndex = (base, workingIndex) => index.dateAt(base, workingIndex);
219
+ return {
220
+ rules,
221
+ hasNonWorkingDays: true,
222
+ weekStart: weekStartOf(openMask),
223
+ workingWeekdays,
224
+ daysPerUnit: unitDays,
225
+ addUnits: (date, count, unit) => dateAtWorkingIndex(date, count * unitDays(unit)),
226
+ isWorkingDay: (date) => index.isWorkingDay(date),
227
+ workingIndexOf: (base, date) => index.indexOf(base, date),
228
+ dateAtWorkingIndex,
229
+ nonWorkingRuns: (from, to) => index.runs(from, to),
230
+ workingExceptionRuns,
231
+ };
232
+ }
233
+ function identityCalendar(unitDays, rules, workingWeekdays, workingExceptionRuns) {
234
+ return {
235
+ rules,
236
+ hasNonWorkingDays: false,
237
+ weekStart: undefined,
238
+ workingWeekdays,
239
+ daysPerUnit: unitDays,
240
+ addUnits: (date, count, unit) => addDays(date, count * unitDays(unit)),
241
+ isWorkingDay: () => true,
242
+ workingIndexOf: (base, date) => daysBetween(base, date),
243
+ dateAtWorkingIndex: (base, index) => addDays(base, index),
244
+ nonWorkingRuns: () => [],
245
+ workingExceptionRuns,
246
+ };
247
+ }
248
+ /** Validate and deep-copy one rule; undefined drops it. */
249
+ function normalizeRule(rule) {
250
+ const mask = rule.every === undefined ? undefined : weekdayMask(rule.every);
251
+ const hasBound = rule.start !== undefined || rule.end !== undefined;
252
+ if (mask === 0)
253
+ return undefined;
254
+ if (rule.dates !== undefined && (mask !== undefined || hasBound))
255
+ return undefined;
256
+ const isRange = mask === undefined && rule.dates === undefined;
257
+ if (isRange && (rule.start === undefined || rule.end === undefined))
258
+ return undefined;
259
+ const start = rule.start === undefined ? -Infinity : dayNumber(rule.start);
260
+ const end = rule.end === undefined ? Infinity : dayNumber(rule.end);
261
+ if (Number.isNaN(start) || Number.isNaN(end) || end < start)
262
+ return undefined;
263
+ const dates = rule.dates?.map(dayNumber);
264
+ if (dates?.some(Number.isNaN))
265
+ return undefined;
266
+ const spans = dates
267
+ ? dates.map((day) => ({ start: day, end: day + 1 }))
268
+ : [{ start, end: end + 1 }];
269
+ return { copy: copyRule(rule), working: rule.working, mask, spans };
270
+ }
271
+ function copyRule(rule) {
272
+ const copy = { working: rule.working };
273
+ if (rule.every !== undefined)
274
+ copy.every = new Set(rule.every);
275
+ if (rule.dates !== undefined)
276
+ copy.dates = rule.dates.map((date) => new Date(date.getTime()));
277
+ if (rule.start !== undefined)
278
+ copy.start = new Date(rule.start.getTime());
279
+ if (rule.end !== undefined)
280
+ copy.end = new Date(rule.end.getTime());
281
+ if (rule.id !== undefined)
282
+ copy.id = rule.id;
283
+ if (rule.title !== undefined)
284
+ copy.title = rule.title;
285
+ return copy;
286
+ }
287
+ function weekdayMask(every) {
288
+ let mask = 0;
289
+ for (const weekday of every) {
290
+ if (!Number.isInteger(weekday) || weekday < 0 || weekday >= DAYS_PER_WEEK) {
291
+ throw new RangeError(`working calendar: weekday ${weekday} is outside 0..6`);
292
+ }
293
+ mask |= 1 << weekday;
294
+ }
295
+ return mask;
296
+ }
297
+ /** The open-ended week: non-working weekdays minus open-ended working ones. */
298
+ function openWeekMask(rules) {
299
+ let off = 0;
300
+ let working = 0;
301
+ for (const rule of rules) {
302
+ if (rule.mask === undefined || !isOpenEnded(rule.spans[0]))
303
+ continue;
304
+ if (rule.working)
305
+ working |= rule.mask;
306
+ else
307
+ off |= rule.mask;
308
+ }
309
+ return off & ~working;
310
+ }
311
+ /** The weekdays a mask leaves working. */
312
+ function weekdaysOutside(mask) {
313
+ const out = new Set();
314
+ for (let weekday = 0; weekday < DAYS_PER_WEEK; weekday++) {
315
+ if ((mask & (1 << weekday)) === 0)
316
+ out.add(weekday);
317
+ }
318
+ return out;
319
+ }
320
+ function isOpenEnded(span) {
321
+ return span.start === -Infinity && span.end === Infinity;
322
+ }
323
+ function boundedSpans(rules) {
324
+ const out = [];
325
+ for (const rule of rules) {
326
+ if (rule.mask === undefined || isOpenEnded(rule.spans[0]))
327
+ continue;
328
+ out.push({ ...rule.spans[0], working: rule.working, mask: rule.mask });
329
+ }
330
+ return out;
331
+ }
332
+ /**
333
+ * Split the line into segments with one weekly mask each: the open-ended
334
+ * week, overridden by every active bounded recurrence (working wins ties).
335
+ */
336
+ function recurringSegments(openMask, bounded) {
337
+ const offCount = new Array(DAYS_PER_WEEK).fill(0);
338
+ const workingCount = new Array(DAYS_PER_WEEK).fill(0);
339
+ const apply = (span, delta) => {
340
+ const counts = span.working ? workingCount : offCount;
341
+ for (let weekday = 0; weekday < DAYS_PER_WEEK; weekday++) {
342
+ if (span.mask & (1 << weekday))
343
+ counts[weekday] += delta;
344
+ }
345
+ };
346
+ const currentMask = () => {
347
+ let mask = openMask;
348
+ for (let weekday = 0; weekday < DAYS_PER_WEEK; weekday++) {
349
+ if (offCount[weekday] > 0)
350
+ mask |= 1 << weekday;
351
+ if (workingCount[weekday] > 0)
352
+ mask &= ~(1 << weekday);
353
+ }
354
+ return mask;
355
+ };
356
+ const events = [];
357
+ for (const span of bounded) {
358
+ if (span.start === -Infinity)
359
+ apply(span, 1);
360
+ else
361
+ events.push({ at: span.start, span, delta: 1 });
362
+ if (span.end !== Infinity)
363
+ events.push({ at: span.end, span, delta: -1 });
364
+ }
365
+ events.sort((a, b) => a.at - b.at);
366
+ const segments = [];
367
+ let start = -Infinity;
368
+ let next = 0;
369
+ for (;;) {
370
+ const end = next < events.length ? events[next].at : Infinity;
371
+ if (start < end)
372
+ pushMaskSegment(segments, { start, end, mask: currentMask() });
373
+ if (end === Infinity)
374
+ return segments;
375
+ while (next < events.length && events[next].at === end) {
376
+ apply(events[next].span, events[next].delta);
377
+ next++;
378
+ }
379
+ start = end;
380
+ }
381
+ }
382
+ function pushMaskSegment(segments, segment) {
383
+ const previous = segments[segments.length - 1];
384
+ if (previous && previous.mask === segment.mask)
385
+ previous.end = segment.end;
386
+ else
387
+ segments.push(segment);
388
+ }
389
+ /** Disjoint dated stretches, each all-working or all-non-working (working wins ties). */
390
+ function datedSegments(rules) {
391
+ const events = [];
392
+ for (const rule of rules) {
393
+ if (rule.mask !== undefined)
394
+ continue;
395
+ for (const span of rule.spans) {
396
+ events.push({ at: span.start, working: rule.working, delta: 1 });
397
+ events.push({ at: span.end, working: rule.working, delta: -1 });
398
+ }
399
+ }
400
+ events.sort((a, b) => a.at - b.at);
401
+ const segments = [];
402
+ let working = 0;
403
+ let off = 0;
404
+ let next = 0;
405
+ while (next < events.length) {
406
+ const start = events[next].at;
407
+ while (next < events.length && events[next].at === start) {
408
+ if (events[next].working)
409
+ working += events[next].delta;
410
+ else
411
+ off += events[next].delta;
412
+ next++;
413
+ }
414
+ if (next === events.length || (working === 0 && off === 0))
415
+ continue;
416
+ const kind = working > 0 ? 'working' : 'non-working';
417
+ const end = events[next].at;
418
+ const previous = segments[segments.length - 1];
419
+ if (previous && previous.kind === kind && previous.end === start)
420
+ previous.end = end;
421
+ else
422
+ segments.push({ start, end, kind });
423
+ }
424
+ return segments;
425
+ }
426
+ /** Lay the dated stretches over the weekly segments; dated rules beat recurring ones. */
427
+ function overlay(segments, dated) {
428
+ const pieces = [];
429
+ let first = 0;
430
+ for (const segment of segments) {
431
+ while (first < dated.length && dated[first].end <= segment.start)
432
+ first++;
433
+ let cursor = segment.start;
434
+ for (let i = first; i < dated.length && dated[i].start < segment.end; i++) {
435
+ const start = Math.max(dated[i].start, segment.start);
436
+ const end = Math.min(dated[i].end, segment.end);
437
+ if (cursor < start)
438
+ pieces.push(maskPiece(cursor, start, segment.mask));
439
+ pieces.push({ start, end, kind: dated[i].kind });
440
+ cursor = end;
441
+ }
442
+ if (cursor < segment.end)
443
+ pieces.push(maskPiece(cursor, segment.end, segment.mask));
444
+ }
445
+ return pieces;
446
+ }
447
+ function maskPiece(start, end, mask) {
448
+ const off = [];
449
+ for (let weekday = 0; weekday < DAYS_PER_WEEK; weekday++) {
450
+ off.push((mask & (1 << weekday)) !== 0);
451
+ }
452
+ const workingPerWeek = off.filter((isOff) => !isOff).length;
453
+ return { start, end, kind: 'mask', off, workingPerWeek };
454
+ }
455
+ /** Titled dated rules of one state, by first date then input order. */
456
+ function titledRules(rules, working) {
457
+ const titled = [];
458
+ for (const [order, rule] of rules.entries()) {
459
+ const title = rule.copy.title;
460
+ if (rule.mask !== undefined || rule.working !== working || title === undefined)
461
+ continue;
462
+ if (rule.spans.length === 0)
463
+ continue;
464
+ const first = rule.spans.reduce((min, span) => Math.min(min, span.start), Infinity);
465
+ titled.push({ title, spans: rule.spans, first, order });
466
+ }
467
+ titled.sort((a, b) => a.first - b.first || a.order - b.order);
468
+ return titled.map(({ title, spans }) => ({ title, spans }));
469
+ }
470
+ /**
471
+ * The runs of days a titled dated `working` rule turns from non-working to
472
+ * working, under the precedence of specs/working-calendar.md §4.1. A dated
473
+ * working rule wins every day it covers, so it turns a day exactly when the
474
+ * calendar without the dated working rules (the weekly segments under the
475
+ * dated closures) closes it. That catches a weekday make-up day inside a
476
+ * dated closure, which `calendarExceptions` cannot see because the open
477
+ * week works it. Dated rules are finite, so this walks their days once.
478
+ */
479
+ function titledWorkingRuns(rules, segments) {
480
+ const titled = titledRules(rules, true);
481
+ if (titled.length === 0)
482
+ return [];
483
+ const closures = rules.filter((rule) => !rule.working);
484
+ const baseline = overlay(segments, datedSegments(closures));
485
+ // Each turned day, with the indices (into `titled`) of the rules that turn it.
486
+ const turned = new Map();
487
+ titled.forEach((rule, i) => {
488
+ for (const span of rule.spans) {
489
+ for (let day = span.start; day < span.end; day++) {
490
+ if (!isNonWorkingIn(baseline, day))
491
+ continue;
492
+ const owners = turned.get(day) ?? new Set();
493
+ owners.add(i);
494
+ turned.set(day, owners);
495
+ }
496
+ }
497
+ });
498
+ const days = [...turned.keys()].sort((a, b) => a - b);
499
+ const out = [];
500
+ for (let i = 0; i < days.length;) {
501
+ let j = i;
502
+ const owners = new Set();
503
+ while (j < days.length && days[j] - days[i] === j - i) {
504
+ for (const owner of turned.get(days[j]) ?? [])
505
+ owners.add(owner);
506
+ j++;
507
+ }
508
+ out.push({
509
+ from: dateOfDay(days[i]),
510
+ through: dateOfDay(days[j - 1]),
511
+ titles: [...owners].sort((a, b) => a - b).map((owner) => titled[owner].title),
512
+ });
513
+ i = j;
514
+ }
515
+ return out;
516
+ }
517
+ /** Copies of the runs with a day in [from, to). */
518
+ function runsIntersecting(runs, fromDate, toDate) {
519
+ const from = dayNumber(fromDate);
520
+ const to = dayNumber(toDate);
521
+ if (Number.isNaN(from) || Number.isNaN(to))
522
+ return [];
523
+ return runs
524
+ .filter((run) => dayNumber(run.from) < to && dayNumber(run.through) >= from)
525
+ .map((run) => ({
526
+ from: new Date(run.from.getTime()),
527
+ through: new Date(run.through.getTime()),
528
+ titles: [...run.titles],
529
+ }));
530
+ }
531
+ /** First working weekday after the longest cyclic non-working run; ties go Sunday-first. */
532
+ function weekStartOf(openMask) {
533
+ const isOff = (weekday) => (openMask & (1 << weekday)) !== 0;
534
+ let best;
535
+ let bestLength = 0;
536
+ for (let weekday = 0; weekday < DAYS_PER_WEEK; weekday++) {
537
+ if (isOff(weekday) || !isOff((weekday + 6) % DAYS_PER_WEEK))
538
+ continue;
539
+ let length = 0;
540
+ while (length < DAYS_PER_WEEK && isOff((weekday + 6 - length) % DAYS_PER_WEEK))
541
+ length++;
542
+ if (length > bestLength) {
543
+ best = weekday;
544
+ bestLength = length;
545
+ }
546
+ }
547
+ return best;
548
+ }
549
+ // ---------------------------------------------------------------------------
550
+ // Queries over the resolved pieces (integer UTC day numbers).
551
+ // ---------------------------------------------------------------------------
552
+ function dayNumber(date) {
553
+ return Math.round(date.getTime() / DAY_MS);
554
+ }
555
+ function dateOfDay(day) {
556
+ return new Date(day * DAY_MS);
557
+ }
558
+ function weekdayOf(day) {
559
+ return (((day + EPOCH_WEEKDAY) % DAYS_PER_WEEK) + DAYS_PER_WEEK) % DAYS_PER_WEEK;
560
+ }
561
+ function remainderScanError() {
562
+ return new Error('working calendar: remainder scan exceeded its cap (internal error)');
563
+ }
564
+ /** The index of the piece holding `day`. */
565
+ function pieceIndexAt(pieces, day) {
566
+ let low = 0;
567
+ let high = pieces.length - 1;
568
+ while (low < high) {
569
+ const middle = (low + high) >> 1;
570
+ if (day >= pieces[middle].end)
571
+ low = middle + 1;
572
+ else
573
+ high = middle;
574
+ }
575
+ return low;
576
+ }
577
+ function isNonWorkingIn(pieces, day) {
578
+ const piece = pieces[pieceIndexAt(pieces, day)];
579
+ if (piece.kind === 'mask')
580
+ return piece.off[weekdayOf(day)];
581
+ return piece.kind === 'non-working';
582
+ }
583
+ /** Working days in [from, to), both finite and inside `piece`. */
584
+ function workingDaysIn(piece, from, to) {
585
+ if (piece.kind !== 'mask')
586
+ return piece.kind === 'working' ? to - from : 0;
587
+ const weeks = Math.floor((to - from) / DAYS_PER_WEEK);
588
+ let count = weeks * piece.workingPerWeek;
589
+ for (let day = from + weeks * DAYS_PER_WEEK; day < to; day++) {
590
+ if (!piece.off[weekdayOf(day)])
591
+ count++;
592
+ }
593
+ return count;
594
+ }
595
+ /** The working day `rank` (0-based) at or after `from`; it lies inside `piece`. */
596
+ function nthWorkingIn(piece, from, rank) {
597
+ if (piece.kind !== 'mask')
598
+ return from + rank;
599
+ let day = from + Math.floor(rank / piece.workingPerWeek) * DAYS_PER_WEEK;
600
+ let left = rank % piece.workingPerWeek;
601
+ for (let step = 0; step < MAX_REMAINDER_STEPS; step++, day++) {
602
+ if (piece.off[weekdayOf(day)])
603
+ continue;
604
+ if (left === 0)
605
+ return day;
606
+ left--;
607
+ }
608
+ throw remainderScanError();
609
+ }
610
+ /** The `rank`-th (1-based) working day before `end`; it lies inside `piece`. */
611
+ function nthWorkingBackIn(piece, end, rank) {
612
+ if (piece.kind !== 'mask')
613
+ return end - rank;
614
+ const weeks = Math.floor((rank - 1) / piece.workingPerWeek);
615
+ let day = end - weeks * DAYS_PER_WEEK;
616
+ let left = rank - weeks * piece.workingPerWeek;
617
+ for (let step = 0; step < MAX_REMAINDER_STEPS; step++) {
618
+ day--;
619
+ if (piece.off[weekdayOf(day)])
620
+ continue;
621
+ left--;
622
+ if (left === 0)
623
+ return day;
624
+ }
625
+ throw remainderScanError();
626
+ }
627
+ class WorkingDayIndex {
628
+ pieces;
629
+ titled;
630
+ constructor(pieces, titled) {
631
+ this.pieces = pieces;
632
+ this.titled = titled;
633
+ }
634
+ isWorkingDay(date) {
635
+ const day = dayNumber(date);
636
+ return Number.isNaN(day) || !this.isNonWorking(day);
637
+ }
638
+ indexOf(base, date) {
639
+ const baseDay = dayNumber(base);
640
+ const dateDay = dayNumber(date);
641
+ if (Number.isNaN(baseDay) || Number.isNaN(dateDay))
642
+ return Number.NaN;
643
+ const snapped = this.nthFrom(dateDay, 0);
644
+ return snapped >= baseDay ? this.count(baseDay, snapped) : -this.count(snapped, baseDay);
645
+ }
646
+ dateAt(base, index) {
647
+ const baseDay = dayNumber(base);
648
+ const rank = Math.floor(index);
649
+ if (Number.isNaN(baseDay) || !Number.isFinite(rank) || Math.abs(rank) > MAX_WORKING_INDEX) {
650
+ return new Date(Number.NaN);
651
+ }
652
+ return dateOfDay(rank >= 0 ? this.nthFrom(baseDay, rank) : this.nthBefore(baseDay, -rank));
653
+ }
654
+ runs(fromDate, toDate) {
655
+ const from = dayNumber(fromDate);
656
+ const to = dayNumber(toDate);
657
+ if (Number.isNaN(from) || Number.isNaN(to) || from >= to)
658
+ return [];
659
+ const out = [];
660
+ let day = this.isNonWorking(from)
661
+ ? this.nthBefore(from, 1) + 1
662
+ : this.nextNonWorking(from, to);
663
+ while (day < to) {
664
+ const through = this.nthFrom(day, 0) - 1;
665
+ out.push({
666
+ from: dateOfDay(day),
667
+ through: dateOfDay(through),
668
+ titles: this.titlesIn(day, through),
669
+ });
670
+ day = this.nextNonWorking(through + 1, to);
671
+ }
672
+ return out;
673
+ }
674
+ pieceAt(day) {
675
+ return pieceIndexAt(this.pieces, day);
676
+ }
677
+ isNonWorking(day) {
678
+ return isNonWorkingIn(this.pieces, day);
679
+ }
680
+ /** Working days in [from, to), from <= to. */
681
+ count(from, to) {
682
+ let total = 0;
683
+ for (let i = this.pieceAt(from); i < this.pieces.length && this.pieces[i].start < to; i++) {
684
+ const piece = this.pieces[i];
685
+ total += workingDaysIn(piece, Math.max(from, piece.start), Math.min(to, piece.end));
686
+ }
687
+ return total;
688
+ }
689
+ /** The working day `rank` (0-based) at or after `day`. */
690
+ nthFrom(day, rank) {
691
+ let position = day;
692
+ let left = rank;
693
+ for (let i = this.pieceAt(day); i < this.pieces.length; i++) {
694
+ const piece = this.pieces[i];
695
+ if (piece.end === Infinity)
696
+ return nthWorkingIn(piece, position, left);
697
+ const available = workingDaysIn(piece, position, piece.end);
698
+ if (left < available)
699
+ return nthWorkingIn(piece, position, left);
700
+ left -= available;
701
+ position = piece.end;
702
+ }
703
+ throw new Error('working calendar: forward walk left the last piece (internal error)');
704
+ }
705
+ /** The `rank`-th (1-based) working day before `day`. */
706
+ nthBefore(day, rank) {
707
+ let position = day;
708
+ let left = rank;
709
+ for (let i = this.pieceAt(day - 1); i >= 0; i--) {
710
+ const piece = this.pieces[i];
711
+ if (piece.start === -Infinity)
712
+ return nthWorkingBackIn(piece, position, left);
713
+ const available = workingDaysIn(piece, piece.start, position);
714
+ if (left <= available)
715
+ return nthWorkingBackIn(piece, position, left);
716
+ left -= available;
717
+ position = piece.start;
718
+ }
719
+ throw new Error('working calendar: backward walk left the first piece (internal error)');
720
+ }
721
+ /** First non-working day in [day, limit), or `limit`. */
722
+ nextNonWorking(day, limit) {
723
+ for (let i = this.pieceAt(day); i < this.pieces.length && this.pieces[i].start < limit; i++) {
724
+ const piece = this.pieces[i];
725
+ const from = Math.max(day, piece.start);
726
+ if (piece.kind === 'non-working')
727
+ return from;
728
+ if (piece.kind !== 'mask' || piece.workingPerWeek === DAYS_PER_WEEK)
729
+ continue;
730
+ for (let offset = 0; offset < DAYS_PER_WEEK && from + offset < piece.end; offset++) {
731
+ if (piece.off[weekdayOf(from + offset)])
732
+ return from + offset;
733
+ }
734
+ }
735
+ return limit;
736
+ }
737
+ /** Titles of the titled dated non-working rules with a day in [from, through]. */
738
+ titlesIn(from, through) {
739
+ const titles = [];
740
+ for (const rule of this.titled) {
741
+ if (rule.spans.some((span) => span.start <= through && span.end > from)) {
742
+ titles.push(rule.title);
743
+ }
744
+ }
745
+ return titles;
746
+ }
747
+ }
74
748
  //# sourceMappingURL=working-calendar.js.map