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

Link to this function

censor(object, keywords)

View Source

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

@spec extract(map() | list(), list()) :: list()

Query data nested in map and list

Params:

  • object: map or list
  • paths: list of key or index, or function to compare
    • * match all
    • integer match index or map key
    • atom match keyword key or map key
    • map match map item in list
    • function match list item with function
    • string 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"]}
Link to this function

get(object, paths, default \\ nil)

View Source

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 list
  • fun/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 value
    • item: 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"]}]
    }