View Source Nested (Nested 2 v0.1.1)
Nested provides functions to access nested data in map and list with ease
Summary
Functions
Replace value in map and list with ***
if key match any of keywords
Clean nil value in map and list
Query data nested in map and list
Query data nested in map and list
Return: {:ok, data}
if found and :error
if not found
Query data nested in map and list, return default if path not exist
Merge two maps deeply, preferring values from the right map.
Convert struct to map by removing __struct__
and __meta__
keys
Traverse map and list, apply function to each item
Functions
Replace value in map and list with ***
if key match any of keywords
Examples
data = %{users:
[
%{name: "user1", is_active: true, skills: ["js", "html"]},
%{name: "user2", is_active: false, skills: ["elixir", "sql"]}]
}
Nested.censor(data, ["active", "skills"])
#> %{users:
[
%{name: "user1", is_active: "***", skills: "***"},
%{name: "user2", is_active: "***", skills: "***"}]
}
Clean nil value in map and list
Query data nested in map and list
Params:
object
: map or listpaths
: list of key or index, or function to compare*
match allinteger
match index or map keyatom
match keyword key or map keymap
match map item in listfunction
match list item with functionstring
match map string key
Example
data = %{users:
[
%{name: "user1", is_active: true, skills: ["js", "html"]},
%{name: "user2", is_active: false, skills: ["elixir", "sql"]}]
}
Nested.extract(data, [:users, "*", :skills ])
#> [["js", "html"], ["elixir", "sql"]]
Query data nested in map and list
Return: {:ok, data}
if found and :error
if not found
It support compare condition for list item which is a map
Example
data = %{users:
[
%{name: "user1", is_active: true, skills: ["js", "html"]},
%{name: "user2", is_active: false, skills: ["elixir", "sql"]}]
}
Nested.fetch(data, [:users, %{is_active: true}, :skills ])
#> {:ok, ["js", "html"]}
Query data nested in map and list, return default if path not exist
Example:
get(%{a: 1, b: [%{name: "test1"}, %{name: "test"2}]}, [:b, 0, :name]) #> "test1"
Merge two maps deeply, preferring values from the right map.
Convert struct to map by removing __struct__
and __meta__
keys
Traverse map and list, apply function to each item
Params:
object
: map or listfun/1
: function to apply to each item of map or list and return one of::discard
: discard item{:skip, item}
: skip traverse nested value{:next, item}
: traverse nested valueitem
: behavior as{:next, item}
Examples:
# remove all `is_active` key
data = %{users:
[
%{name: "user1", is_active: true, skills: ["js", "html"]},
%{name: "user2", is_active: false, skills: ["elixir", "sql"]}]
}
Nested.traverse(data, fn
{:is_active, _} -> :discard
item -> IO.inspect(item)
end)
#> %{users:
[
%{name: "user1", skills: ["js", "html"]},
%{name: "user2", skills: ["elixir", "sql"]}]
}