Giter Site home page Giter Site logo

nested_filter's Introduction

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.

nested_filter's People

Contributors

dependabot-support avatar dependabot[bot] avatar pinx avatar rsilvestre avatar treble37 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nested_filter's Issues

Filter object values in an array?

Given a struct Foo and a map I can't seem to be able to filter nil values in arrays.

defmodule Foo do defstruct [:id] end

@myMap %{ name: nil, random: [%Foo{id: 5}, %Foo{}] }

map
  |> NestedFilter.drop_by_value([nil)
  |> NestedFilter.drop_by_key([])

the output that I get is %{random: [%Foo{id: 5}, %Foo{id: nil}]}

Am I doing something wrong?

Cheers:)

Unable to handle DateTime values

> NestedFilter.drop_by_value(%{date: DateTime.utc_now, foo: nil}, [nil]) 
** (Protocol.UndefinedError) protocol Enumerable not implemented for #<DateTime(2017-07-13T13:27:40.232574Z Etc/UTC)>
    (elixir) lib/enum.ex:1: Enumerable.impl_for!/1
    (elixir) lib/enum.ex:116: Enumerable.reduce/3
    (elixir) lib/enum.ex:1767: Enum.reduce/3
             lib/nested_filter.ex:14: anonymous fn/3 in NestedFilter.drop_by/2
    (stdlib) lists.erl:1263: :lists.foldl/3

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.