-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Convert itinerary legs to segments for easier rendering #2262
Merged
joshlarson
merged 10 commits into
main
from
jdl/refactor/convert-itinerary-legs-to-segments
Dec 11, 2024
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
683aa7e
refactor: Convert itinerary legs to segments for easier rendering
joshlarson abdfa43
refactor: Move "bg-gray-bordered-background" into <.transit_leg />
joshlarson bd7318e
refactor: Create LegToSegmentHelper and render segments instead of legs
joshlarson 3452b0d
refactor: Change start and end <.place />'s into segments, so that <.…
joshlarson 6db42ba
fix: Formatting
joshlarson 5caf9b7
refactor: Move LegToSegmentHelper into Dotcom.TripPlan
joshlarson b200b37
cleanup: Add a moduledoc for LegToSegmentHelper
joshlarson 37de8f1
cleanup: Rename some misleadingly-named variables
joshlarson 8052970
fix: Formatting (again)
joshlarson 6726e81
cleanup: Revert numItineraries change that I accidentally committed
joshlarson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule Dotcom.TripPlan.LegToSegmentHelper do | ||
@moduledoc """ | ||
A simple algorithm to convert legs as returned by Open Trip Planner | ||
into segments to be displayed by our trip planner tool. | ||
""" | ||
|
||
alias Dotcom.TripPlan.{PersonalDetail, TransitDetail} | ||
|
||
def legs_to_segments(legs) do | ||
legs | ||
|> raw_convert_to_segments() | ||
|> prepend_start_location() | ||
|> append_end_location() | ||
end | ||
|
||
defp raw_convert_to_segments(legs) do | ||
Enum.map(legs, &to_segment/1) | ||
end | ||
|
||
defp to_segment(%{mode: %PersonalDetail{}} = leg) do | ||
{:walking_segment, leg} | ||
end | ||
|
||
defp to_segment(%{mode: %TransitDetail{}} = leg) do | ||
{:transit_segment, leg} | ||
end | ||
|
||
defp prepend_start_location([{_, leg} | _] = segments) do | ||
[ | ||
{:location_segment, %{time: leg.start, place: leg.from}} | ||
| segments | ||
] | ||
end | ||
|
||
defp append_end_location([{_, leg} = last_segment]) do | ||
[last_segment, {:location_segment, %{time: leg.stop, place: leg.to}}] | ||
end | ||
|
||
defp append_end_location([first_segment | rest_of_segments]) do | ||
[first_segment | append_end_location(rest_of_segments)] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Dotcom.TripPlan.LegToSegmentHelperTest do | ||
@moduledoc false | ||
|
||
use ExUnit.Case, async: true | ||
|
||
alias Dotcom.TripPlan.LegToSegmentHelper | ||
alias Dotcom.TripPlan.{Leg, PersonalDetail, TransitDetail} | ||
|
||
test "works for a typical walking-transit-walking itinerary and puts a location on either end" do | ||
assert [ | ||
{:location_segment, _}, | ||
{:walking_segment, _}, | ||
{:transit_segment, _}, | ||
{:walking_segment, _}, | ||
{:location_segment, _} | ||
] = | ||
LegToSegmentHelper.legs_to_segments([ | ||
%Leg{mode: %PersonalDetail{}}, | ||
%Leg{mode: %TransitDetail{}}, | ||
%Leg{mode: %PersonalDetail{}} | ||
]) | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kinda wanna let this slide, but 40 seems kinda excessive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh oops - didn't mean to include this change. I've been using that to get more itineraries so that the one I'm looking for is more likely to actually show up.