Skip to content

Commit

Permalink
fix: merge wide_intervals only if week indexes are the same with diff…
Browse files Browse the repository at this point in the history
…erent dates length
  • Loading branch information
guillaumj committed Nov 14, 2024
1 parent 4c57003 commit 6d4731c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/opening_hours_converter/opening_hours_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ def same_time?(o)
end
end


def same_date?(o)
if o.nil? || o.date.length != @date.length
false
Expand All @@ -295,12 +296,23 @@ def same_date?(o)
end

def mergeable?(o)
if o.nil? || o.date.length != @date.length
return true if o.nil?

if o.date.length != @date.length
if @date.any? { |date| date.wide_interval.type == "week" }
indexes = (@date + o.date).map do |date|
date.wide_interval.indexes
end.uniq

return false if indexes.length > 1
end

true
else
@date.each_with_index do |d, i|
return false if (d.wide_interval.start&.dig(:year) != o.date[i].wide_interval.start&.dig(:year)) && d.wide_interval.type != o.date[i].wide_interval.type || (d.wide_interval.type == "week" && o.date[i].wide_interval.type == "week" && d.wide_interval.indexes != o.date[i].wide_interval.indexes)
end

true
end
end
Expand Down
3 changes: 3 additions & 0 deletions spec/automaticaly_generated_no_combo_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3319,4 +3319,7 @@
it '2024 Week 3, 6, 9, 12-44/2, 47, 50 Mo; 2025 Week 1, 4, 7, 10, 13-43/2, 46, 49, 52 Mo' do
expect(OpeningHoursConverter::OpeningHoursBuilder.new.build(OpeningHoursConverter::OpeningHoursParser.new.parse('2024 Week 3, 6, 9, 12-44/2, 47, 50 Mo; 2025 Week 1, 4, 7, 10, 13-43/2, 46, 49, 52 Mo'))).to eql('2024 week 3,6,9,12-44/2,47,50 Mo 00:00-23:59; 2025 week 1,4,7,10,13-43/2,46,49,52 Mo 00:00-23:59')
end
it '2024 Week 1-52/2 Tu; 2025 Week 1-52/2 Tu; 2026 Week 1 Tu' do
expect(OpeningHoursConverter::OpeningHoursBuilder.new.build(OpeningHoursConverter::OpeningHoursParser.new.parse('2024 Week 1-52/2 Tu; 2025 Week 1-52/2 Tu; 2026 Week 1 Tu'))).to eql('2024-2025 week 1-52/2 Tu 00:00-23:59; 2026 week 1 Tu 00:00-23:59')
end
end

0 comments on commit 6d4731c

Please sign in to comment.