Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

Markdown · 1.4 KiB · at 9472e3c

Write mergeRanges(ranges), which merges a list of inclusive calendar-date ranges.

Each range is { start, end } where both are dates written YYYY-MM-DD: exactly a four-digit year, a two-digit month, and a two-digit day separated by -, with nothing else in the string. Dates are plain Gregorian calendar days (no time zones): the month must be 01 to 12 and the day must exist in that month, using the Gregorian leap-year rule (divisible by 4, except centuries not divisible by 400). Both ends are inclusive, so { start: "2024-01-01", end: "2024-01-01" } is a one-day range.

Validation runs over the input in order, and the first problem found is returned. For each range at position index, check start, then end, then their order:

  • A date that is not a valid date in this format fails with { kind: "invalid_date", index, value } where value is the offending string.
  • A range whose start is after its end fails with { kind: "inverted_range", index }.

If every range is valid, return the merged ranges sorted by start. Two ranges merge when they overlap or when they are adjacent, meaning the day after one range's end is the other range's start. Merging is transitive: keep merging until no two output ranges overlap or touch. Output dates use the same YYYY-MM-DD format.

An empty input returns an empty list. The input array and its range objects must not be modified; return new objects.