Skip to content

Commit

Permalink
Refactor: flatten the structure of the data
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanchrobot committed Feb 18, 2024
1 parent 3393d66 commit 9b4272e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
4 changes: 2 additions & 2 deletions assets/tree/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function TreeNode({ node, level }) {
</div>
<div>
{node.children && isExpanded ? (
<TextItems items={node.expanded.prefix} />
<TextItems items={node.expanded_before} />
) : (
<TextItems items={node.content} />
)}
Expand All @@ -60,7 +60,7 @@ function TreeNode({ node, level }) {
))}
</ol>
<div className="ml-[2ch]">
<TextItems items={node.expanded.suffix} />
<TextItems items={node.expanded_after} />
</div>
</>
)}
Expand Down
19 changes: 10 additions & 9 deletions lib/kino/tree.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ defmodule Kino.Tree do
kind: "tuple",
content: [black("{...}") | suffix],
children: children,
expanded: %{prefix: [black("{")], suffix: [black("}") | suffix]}
expanded_before: [black("{")],
expanded_after: [black("}") | suffix]
}
end

Expand All @@ -95,7 +96,8 @@ defmodule Kino.Tree do
kind: "list",
content: [black("[...]") | suffix],
children: children,
expanded: %{prefix: [black("[")], suffix: [black("]") | suffix]}
expanded_before: [black("[")],
expanded_after: [black("]") | suffix]
}
end

Expand All @@ -115,10 +117,8 @@ defmodule Kino.Tree do
kind: "struct",
content: [black("%"), blue(inspect(module)), black("{...}") | suffix],
children: children,
expanded: %{
prefix: [black("%"), blue(inspect(module)), black("{")],
suffix: [black("}") | suffix]
}
expanded_before: [black("%"), blue(inspect(module)), black("{")],
expanded_after: [black("}") | suffix]
}
end
end
Expand All @@ -135,7 +135,8 @@ defmodule Kino.Tree do
kind: "map",
content: [black("%{...}") | suffix],
children: children,
expanded: %{prefix: [black("%{")], suffix: [black("}") | suffix]}
expanded_before: [black("%{")],
expanded_after: [black("}") | suffix]
}
end

Expand All @@ -154,11 +155,11 @@ defmodule Kino.Tree do
end

case to_node(value, suffix) do
%{content: content, expanded: %{prefix: prefix} = expanded} = node ->
%{content: content, expanded_before: expanded_before} = node ->
%{
node
| content: [key_span, sep_span | content],
expanded: %{expanded | prefix: [key_span, sep_span | prefix]}
expanded_before: [key_span, sep_span | expanded_before]
}

%{content: content} = node ->
Expand Down
45 changes: 30 additions & 15 deletions test/kino/tree_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ defmodule Kino.TreeTest do
assert %{
kind: "tuple",
content: "{...}",
expanded: %{prefix: "{", suffix: "}"},
expanded_before: "{",
expanded_after: "}",
children: [
%{kind: "number", content: "1", children: nil}
]
Expand All @@ -32,12 +33,14 @@ defmodule Kino.TreeTest do
assert %{
kind: "tuple",
content: "{...}",
expanded: %{prefix: "{", suffix: "}"},
expanded_before: "{",
expanded_after: "}",
children: [
%{
kind: "tuple",
content: "{...}",
expanded: %{prefix: "{", suffix: "}"},
expanded_before: "{",
expanded_after: "}",
children: [
%{kind: "number", content: "1", children: nil}
]
Expand All @@ -50,13 +53,15 @@ defmodule Kino.TreeTest do
assert %{
kind: "tuple",
content: "{...}",
expanded: %{prefix: "{", suffix: "}"},
expanded_before: "{",
expanded_after: "}",
children: [
%{kind: "number", content: "1,", children: nil},
%{
kind: "tuple",
content: "{...},",
expanded: %{prefix: "{", suffix: "},"},
expanded_before: "{",
expanded_after: "},",
children: [
%{kind: "atom", content: ":x,", children: nil},
%{kind: "atom", content: ":y", children: nil}
Expand All @@ -71,7 +76,8 @@ defmodule Kino.TreeTest do
assert %{
kind: "list",
content: "[...]",
expanded: %{prefix: "[", suffix: "]"},
expanded_before: "[",
expanded_after: "]",
children: [
%{kind: "number", content: "1", children: nil}
]
Expand All @@ -82,7 +88,8 @@ defmodule Kino.TreeTest do
assert %{
kind: "list",
content: "[...]",
expanded: %{prefix: "[", suffix: "]"},
expanded_before: "[",
expanded_after: "]",
children: [
%{kind: "atom", content: "foo: :bar", children: nil}
]
Expand All @@ -93,7 +100,8 @@ defmodule Kino.TreeTest do
assert %{
kind: "map",
content: "%{...}",
expanded: %{prefix: "%{", suffix: "}"},
expanded_before: "%{",
expanded_after: "}",
children: [
%{kind: "atom", content: "foo: :bar", children: nil}
]
Expand All @@ -104,7 +112,8 @@ defmodule Kino.TreeTest do
assert %{
kind: "map",
content: "%{...}",
expanded: %{prefix: "%{", suffix: "}"},
expanded_before: "%{",
expanded_after: "}",
children: [
%{kind: "binary", content: ~s("foo" => "bar"), children: nil}
]
Expand All @@ -115,7 +124,8 @@ defmodule Kino.TreeTest do
assert %{
kind: "map",
content: "%{...}",
expanded: %{prefix: "%{", suffix: "}"},
expanded_before: "%{",
expanded_after: "}",
children: [
%{kind: "atom", content: "bar: :baz,", children: nil},
%{kind: "atom", content: "foo: :oof", children: nil}
Expand All @@ -127,7 +137,8 @@ defmodule Kino.TreeTest do
assert %{
kind: "map",
content: "%{...}",
expanded: %{prefix: "%{", suffix: "}"},
expanded_before: "%{",
expanded_after: "}",
children: [
%{kind: "atom", content: "{1, 2} => true", children: nil}
]
Expand All @@ -142,7 +153,8 @@ defmodule Kino.TreeTest do
assert %{
kind: "struct",
content: "%Kino.TreeTest.User{...}",
expanded: %{prefix: "%Kino.TreeTest.User{", suffix: "}"},
expanded_before: "%Kino.TreeTest.User{",
expanded_after: "}",
children: [
%{kind: "binary", content: ~s(email: "[email protected]"), children: nil}
]
Expand Down Expand Up @@ -183,7 +195,8 @@ defmodule Kino.TreeTest do
test "uses separate colors for keys and values" do
assert %{
content: [%{text: "[...]", color: nil}],
expanded: %{prefix: [%{text: "[", color: nil}], suffix: [%{text: "]", color: nil}]},
expanded_before: [%{text: "[", color: nil}],
expanded_after: [%{text: "]", color: nil}],
children: [
%{
content: [
Expand Down Expand Up @@ -225,14 +238,16 @@ defmodule Kino.TreeTest do
%{
content: content,
children: children,
expanded: %{prefix: prefix, suffix: suffix} = expanded
expanded_before: expanded_before,
expanded_after: expanded_after
} = node
) do
%{
node
| content: text_of(content),
children: Enum.map(children, &plaintext/1),
expanded: %{expanded | prefix: text_of(prefix), suffix: text_of(suffix)}
expanded_before: text_of(expanded_before),
expanded_after: text_of(expanded_after)
}
end

Expand Down

0 comments on commit 9b4272e

Please sign in to comment.