From 9b4272efc6d083144b006cd8526b9ff9ceacdcc6 Mon Sep 17 00:00:00 2001 From: Stefan Chrobot Date: Sat, 17 Feb 2024 22:01:21 +0000 Subject: [PATCH] Refactor: flatten the structure of the data --- assets/tree/src/App.js | 4 ++-- lib/kino/tree.ex | 19 ++++++++--------- test/kino/tree_test.exs | 45 +++++++++++++++++++++++++++-------------- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/assets/tree/src/App.js b/assets/tree/src/App.js index 17422516..20b999ee 100644 --- a/assets/tree/src/App.js +++ b/assets/tree/src/App.js @@ -44,7 +44,7 @@ function TreeNode({ node, level }) {
{node.children && isExpanded ? ( - + ) : ( )} @@ -60,7 +60,7 @@ function TreeNode({ node, level }) { ))}
- +
)} diff --git a/lib/kino/tree.ex b/lib/kino/tree.ex index 47879d62..80240784 100644 --- a/lib/kino/tree.ex +++ b/lib/kino/tree.ex @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 -> diff --git a/test/kino/tree_test.exs b/test/kino/tree_test.exs index 9c3e56da..0851955c 100644 --- a/test/kino/tree_test.exs +++ b/test/kino/tree_test.exs @@ -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} ] @@ -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} ] @@ -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} @@ -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} ] @@ -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} ] @@ -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} ] @@ -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} ] @@ -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} @@ -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} ] @@ -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: "user@example.com"), children: nil} ] @@ -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: [ @@ -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