Last active
April 4, 2018 20:53
-
-
Save coreyhaines/9eb8077137d2dff254dcdfb33a73b872 to your computer and use it in GitHub Desktop.
Revisions
-
coreyhaines revised this gist
Apr 4, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ defmodule FolditTest do use ExUnit.Case doctest Foldit def to_keyword([], keyword), do: keyword def to_keyword([val], keyword) do Keyword.update(keyword, nil, [val], fn existing -> [val | existing] end) -
coreyhaines revised this gist
Apr 4, 2018 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -51,11 +51,12 @@ defmodule FolditTest do assert to_tree(structure) == [nil: ["3.md"], a: [nil: ["4.md"]]] structure = [ "2.md", "3.md", "a/4.md", "a/5.md" ] assert to_tree(structure) == [nil: ["3.md", "2.md"], a: [nil: ["5.md", "4.md"]]] end end -
coreyhaines revised this gist
Apr 4, 2018 . 1 changed file with 11 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,9 @@ defmodule FolditTest do end def to_keyword([key | val], keyword) do Keyword.update(keyword, String.to_atom(key), to_keyword(val, []), fn existing -> to_keyword(val, existing) end) end def to_tree(list) do @@ -47,5 +49,13 @@ defmodule FolditTest do ] assert to_tree(structure) == [nil: ["3.md"], a: [nil: ["4.md"]]] structure = [ "3.md", "a/4.md", "a/5.md" ] assert to_tree(structure) == [nil: ["3.md"], a: [nil: ["5.md", "4.md"]]] end end -
coreyhaines revised this gist
Apr 4, 2018 . 1 changed file with 1 addition and 10 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,10 +2,6 @@ defmodule FolditTest do use ExUnit.Case doctest Foldit def to_keyword(empty_val, keyword) when length(empty_val) == 0, do: keyword def to_keyword([val], keyword) do @@ -18,15 +14,10 @@ defmodule FolditTest do def to_tree(list) do list |> Enum.map(&String.split(&1, ~r"/")) |> List.foldl([], &to_keyword/2) end test "moves array into nested keyword" do assert to_keyword(["3.md"], []) == [nil: ["3.md"]] assert to_keyword(["1", "3.md"], []) == ["1": [nil: ["3.md"]]] -
coreyhaines revised this gist
Apr 4, 2018 . 1 changed file with 10 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -6,22 +6,20 @@ defmodule FolditTest do String.split(string, ~r"/") end def to_keyword(empty_val, keyword) when length(empty_val) == 0, do: keyword def to_keyword([val], keyword) do Keyword.update(keyword, nil, [val], fn existing -> [val | existing] end) end def to_keyword([key | val], keyword) do Keyword.update(keyword, String.to_atom(key), to_keyword(val, []), & &1) end def to_tree(list) do list |> Enum.map(&split/1) |> List.foldl([], &to_keyword/2) end test "parses into array" do @@ -30,15 +28,15 @@ defmodule FolditTest do end test "moves array into nested keyword" do assert to_keyword(["3.md"], []) == [nil: ["3.md"]] assert to_keyword(["1", "3.md"], []) == ["1": [nil: ["3.md"]]] assert to_keyword(["a", "b", "3.md"], []) == [a: [b: [nil: ["3.md"]]]] end test "adds to existing" do assert to_keyword(["3.md"], nil: ["1.md"]) == [nil: ["3.md", "1.md"]] assert to_keyword(["3.md"], a: [nil: "extra.md"], nil: ["1.md"]) == [ a: [nil: "extra.md"], nil: ["3.md", "1.md"] ] -
coreyhaines created this gist
Apr 4, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,62 @@ defmodule FolditTest do use ExUnit.Case doctest Foldit def split(string) do String.split(string, ~r"/") end def to_keyword(keyword, empty_val) when length(empty_val) == 0, do: keyword def to_keyword(keyword, [val] = arr) do Keyword.update(keyword, nil, [val], fn existing -> [val | existing] end) end def to_keyword(keyword, [key | val] = arr) do Keyword.update(keyword, String.to_atom(key), to_keyword([], val), & &1) end def to_keyword2(arr, keyword), do: to_keyword(keyword, arr) def to_tree(list) do list |> Enum.map(&split/1) |> List.foldl([], &to_keyword2/2) end test "parses into array" do assert split("3.md") == ["3.md"] assert split("1/2/3.md") == ["1", "2", "3.md"] end test "moves array into nested keyword" do assert to_keyword([], ["3.md"]) == [nil: ["3.md"]] assert to_keyword([], ["1", "3.md"]) == ["1": [nil: ["3.md"]]] assert to_keyword([], ["a", "b", "3.md"]) == [a: [b: [nil: ["3.md"]]]] end test "adds to existing" do assert to_keyword([nil: ["1.md"]], ["3.md"]) == [nil: ["3.md", "1.md"]] assert to_keyword([a: [nil: "extra.md"], nil: ["1.md"]], ["3.md"]) == [ a: [nil: "extra.md"], nil: ["3.md", "1.md"] ] end test "does multiple" do structure = [ "3.md", "4.md" ] assert to_tree(structure) == [nil: ["4.md", "3.md"]] structure = [ "3.md", "a/4.md" ] assert to_tree(structure) == [nil: ["3.md"], a: [nil: ["4.md"]]] end end