Skip to content

Commit

Permalink
fix: De-duplicate start and end locations when they're next to transi…
Browse files Browse the repository at this point in the history
…t segments (#2264)
  • Loading branch information
joshlarson authored Dec 12, 2024
1 parent 68fab92 commit dc2d8be
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/dotcom/trip_plan/leg_to_segment_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ defmodule Dotcom.TripPlan.LegToSegmentHelper do
{:transit_segment, leg}
end

defp prepend_start_location([{:transit_segment, _} | _] = segments) do
segments
end

defp prepend_start_location([{_, leg} | _] = segments) do
[
{:location_segment, %{time: leg.start, place: leg.from}}
| segments
]
end

defp append_end_location([{:transit_segment, _} = last_segment]) do
[last_segment]
end

defp append_end_location([{_, leg} = last_segment]) do
[last_segment, {:location_segment, %{time: leg.stop, place: leg.to}}]
end
Expand Down
33 changes: 33 additions & 0 deletions test/dotcom/trip_plan/leg_to_segment_helper_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,37 @@ defmodule Dotcom.TripPlan.LegToSegmentHelperTest do
%Leg{mode: %PersonalDetail{}}
])
end

test "does not prepend location if the first segment is transit" do
assert [
{:transit_segment, _},
{:walking_segment, _},
{:location_segment, _}
] =
LegToSegmentHelper.legs_to_segments([
%Leg{mode: %TransitDetail{}},
%Leg{mode: %PersonalDetail{}}
])
end

test "does not append location if the last segment is transit" do
assert [
{:location_segment, _},
{:walking_segment, _},
{:transit_segment, _}
] =
LegToSegmentHelper.legs_to_segments([
%Leg{mode: %PersonalDetail{}},
%Leg{mode: %TransitDetail{}}
])
end

test "works if there is just one transit leg" do
assert [
{:transit_segment, _}
] =
LegToSegmentHelper.legs_to_segments([
%Leg{mode: %TransitDetail{}}
])
end
end

0 comments on commit dc2d8be

Please sign in to comment.