Skip to content

Commit

Permalink
split order_node function
Browse files Browse the repository at this point in the history
  • Loading branch information
sorax committed Jul 11, 2024
1 parent 9c99c5a commit a47e945
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ defmodule Radiator.Outline do
def order_child_nodes(%Node{} = node) do
node
|> get_all_child_nodes()
|> order_sibling_nodes()
end

def order_sibling_nodes(nodes) do
nodes
|> Enum.map(fn node -> {node.prev_id, node} end)
|> Map.new()
|> order_nodes(nil, [])
|> order_nodes_by_index(nil, [])
end

defp order_nodes(index, prev_id, collection) do
defp order_nodes_by_index(index, prev_id, collection) do
case index[prev_id] do
%{uuid: uuid} = node -> order_nodes(index, uuid, [node | collection])
%{uuid: uuid} = node -> order_nodes_by_index(index, uuid, [node | collection])
_ -> Enum.reverse(collection)
end
end
Expand Down

0 comments on commit a47e945

Please sign in to comment.