All Projects → treble37 → nested_filter

treble37 / nested_filter

Licence: MIT license
Providing Map#drop (by key or value) and Map#take functionality for nested maps

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to nested filter

mix systemd
Library of mix tasks to generate a systemd unit file for an Elixir project
Stars: ✭ 48 (+50%)
Mutual labels:  elixir-library
emojix
Simple emoji library for Elixir 💩
Stars: ✭ 21 (-34.37%)
Mutual labels:  elixir-library
ex loader
Load a single beam file, apps (a set of beams), or an erlang release (a set of apps) to a node.
Stars: ✭ 12 (-62.5%)
Mutual labels:  elixir-library
director
Director is a production-ready supervisor and manager for Erlang/Elixir processes that focuses on speed, performance and flexibility.
Stars: ✭ 62 (+93.75%)
Mutual labels:  elixir-library
minerva
Elixir framework for easily writing koans.
Stars: ✭ 13 (-59.37%)
Mutual labels:  elixir-library
libsalty
Elixir bindings for libsodium (NIF)
Stars: ✭ 20 (-37.5%)
Mutual labels:  elixir-library
permission ex
No description or website provided.
Stars: ✭ 23 (-28.12%)
Mutual labels:  elixir-library
sftp client
An Elixir SFTP Client that wraps Erlang's ssh and ssh_sftp.
Stars: ✭ 29 (-9.37%)
Mutual labels:  elixir-library
fat ecto
Query mechanism for Ecto
Stars: ✭ 20 (-37.5%)
Mutual labels:  elixir-library
ecto autoslug field
Automatically create slugs for Ecto schemas.
Stars: ✭ 138 (+331.25%)
Mutual labels:  elixir-library
riemannx
A riemann client for elixir (TCP/UDP/TLS supported)
Stars: ✭ 23 (-28.12%)
Mutual labels:  elixir-library
pardall markdown
Reactive publishing framework, filesystem-based with support for Markdown, nested hierarchies, and instant content rebuilding. Written in Elixir.
Stars: ✭ 84 (+162.5%)
Mutual labels:  elixir-library
smppex
✉️ SMPP 3.4 protocol and framework implementation in Elixir
Stars: ✭ 86 (+168.75%)
Mutual labels:  elixir-library
simple graphql client
SimpleGraphqlClient is a graphql client, focused on simplicity and ease of use.
Stars: ✭ 17 (-46.87%)
Mutual labels:  elixir-library
ex spirit
No description or website provided.
Stars: ✭ 26 (-18.75%)
Mutual labels:  elixir-library
vim-ide-elixir
Highly opininated setup of vim plugins for Elixir development
Stars: ✭ 28 (-12.5%)
Mutual labels:  elixir-library
elixir-revisionair ecto
A Revisionair adapter based on Ecto. Allows you to persist and keep track of revisions of your data structures in any of Ecto's supported databases.
Stars: ✭ 18 (-43.75%)
Mutual labels:  elixir-library
sockerl
Sockerl is an advanced Erlang/Elixir socket framework for TCP protocols and provides fast, useful and easy-to-use API for implementing servers, clients and client connection pools.
Stars: ✭ 26 (-18.75%)
Mutual labels:  elixir-library
uef-lib
Useful Erlang Functions Library
Stars: ✭ 14 (-56.25%)
Mutual labels:  nested-maps

NestedFilter

Build Maintainability Test Coverage Coverage Status Hex.pm Hex.pm Downloads GitHub stars GitHub license

The Problems

  1. You have a nested map (or a struct that you converted to a nested map) and you want to remove ALL the keys with specific values such as nil.
  2. You want to do a Map#take on a nested map
Example: Remove all the map keys with nil values
nested_map = %{a: 1, b: %{c: nil, d: nil}, c: nil}

Map.drop(nested_map, [:c, :d])
# => %{a: 1, b: %{c: nil, d: nil}}

# But you actually wanted:
# => %{a: 1}

The Solution: NestedFilter

NestedFilter drills down into a nested map and can do any of the following:

  1. filters out keys according to user specified values.
  2. filters out values according to user specified keys.

Installation

If available in Hex, the package can be installed by adding nested_filter to your list of dependencies in mix.exs:

def deps do
  [{:nested_filter, "~> 1.2.2"}]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/nested_filter.

Usage

By default, when removing user specified values, empty values will be preserved (see Case 1 below). You can add empty values to the user specified values list if you wish those "empty values" (e.g., empty maps) to be removed.

NestedFilter.drop_by_value

# Case 1: Remove the nil values from a nested map, preserving empty map values

nested_map = %{a: 1, b: %{m: nil, n: 2}, c: %{p: %{q: nil, r: nil}, s: %{t: 2, u: 3}} }
NestedFilter.drop_by_value(nested_map, [nil])

# => %{a: 1, b: %{n: 2}, c: %{p: %{}, s: %{t: 2, u: 3}} }

# Case 2: Remove the nil values from a nested map, removing empty map values

nested_map = %{a: 1, b: %{m: nil, n: 2}, c: %{p: %{q: nil, r: nil}, s: %{t: 2, u: 3}} }
NestedFilter.drop_by_value(nested_map, [nil, %{}])
# => %{a: 1, b: %{n: 2}, c: %{s: %{t: 2, u: 3}} }

NestedFilter.drop_by_key

# Case 1: Remove values from a nested map by key

nested_map = %{a: 1, b: %{a: 2, b: 3}, c: %{a: %{a: 1, b: 2}, b: 2, c: %{d: 1, e: 2}}}
assert NestedFilter.drop_by_key(nested_map, [:a]) == %{b: %{b: 3},c: %{b: 2, c: %{d: 1, e: 2}}}

NestedFilter.take_by_key

# Case 1: Take values from a nested map by key

nested_map = %{a: %{b: 1}, c: 3, e: %{f: 4}}
assert NestedFilter.take_by_key(nested_map, [:b, :f]) == %{b: 1, f: 4 }

You can browse the tests for more usage examples.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].