ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - vic/indifferent: Elixir Indifferent access on maps/lists/tuples with custom key transforms.
Elixir Indifferent access on maps/lists/tuples with custom key transforms. - vic/indifferent
Visit Site

GitHub - vic/indifferent: Elixir Indifferent access on maps/lists/tuples with custom key transforms.

GitHub - vic/indifferent: Elixir Indifferent access on maps/lists/tuples with custom key transforms.

Indifferent

Build Status help maintain this lib

This library provides a wrapper for indifferent access on maps, structs, lists and tuples.

You could use Indifferent to wrap a parameters hash or something without having to distingish on the type of keys (atoms or binaries) akin to the features of Rails' HashWithIndifferentAccess.

Installation

Available in Hex, the package can be installed as:

  1. Add indifferent to your list of dependencies in mix.exs:
  def deps do
    [{:indifferent, "~> 0.9"}]
  end
  1. Ensure indifferent is started before your application:
  def application do
    [applications: [:indifferent]]
  end

Usage

Be sure to look at the documentation for more examples.

Indifferent module


##
# Wrapping a map and accesing it with an atom key
iex> i = Indifferent.access(%{"a" => 1})
iex> i[:a]
1


##
# You can provide your custom list of key transformations
# and specify how are keys indifferents for your use case.
iex> i = Indifferent.access(%{"a" => 1},
...>   key_transforms: [fn x, _indifferent -> {:ok, String.downcase(x)} end])
iex> i["A"]
1

##
# Indifferent is compatible with Elixir's Access
iex> Kernel.get_in(%{"a" => 1}, [Indifferent.at(:a)])
1
iex> Kernel.get_in(Indifferent.access(%{"a" => 1}), [:a])
1

##
# Or you can auto wrap data by using Indifferent's version of
# `get_in/2`, `get_and_update_in/3`, `pop_in/2`
iex> Indifferent.get_and_update_in(%{"a" => %{"x" => 1}}, [:a, :x], fn x -> {x * 2, x * 4} end)
{2, %{"a" => %{"x" => 4}}}


##
# Indifferent `path/2` lets you use any syntax you want for
# accessing the nested value you need.
iex> %{"b" => %{"c" => %{"d" => %{"e" => 4}}}} |> Indifferent.path(b["c"][:d].e)
4

##
# And works on lists, tuples, keywords
iex> [9, %{"c" => {:ok, [e: 4]}}] |> Indifferent.path(1.c["1"].e)
4

##
# Accessing nil yields nil
iex> nil |> Indifferent.path(foo.bar)
nil

##
# Can access inside structs
iex> %User{name: "john"} |> Indifferent.path(name)
"john"

##
# And can be used with Kernel methods.
iex> Kernel.get_in(%{"a" => {0, 2}}, Indifferent.path(a["1"]))
2

##
# `path/2` can also take a Keyword of paths and returns a keyword of values
iex> %{"b" => [1, 2]} |> Indifferent.path(x: b[-1])
[x: 2]

##
# The `read/1` macro is a convenience that reads an indifferent path on the first value
iex> System.put_env("COLOR", "red")
iex> Indifferent.read(System.get_env.COLOR)
"red"

##
# Just like `path/2`, the `read/1` macro can also take a Keyword of named things to read
iex> System.put_env("COLOR", "red")
iex> Process.put(:color, "blue")
iex> Indifferent.read(a: System.get_env.COLOR, b: Process.get.color)
[a: "red", b: "blue"]

Indifferent sigils

The ~i and ~I sigils are shortcuts for the API previously described.

iex> import Indifferent.Sigils
Indifferent.Sigils

##
# `~i` is the sigil for `Indifferent.read/1`
iex> data = %{"a" => [b: {10, 20}]}
iex> ~i(data.a.b[1])
20

# When piped a value it will act just like `Indifferent.path/2`
iex> data = %{"a" => [b: {10, 20}]}
iex> data |> ~i(a.b[1])
20


##
# `~I` is the sigil version of `Indifferent.path/1`
# for use with Kernel access functions.

iex> data = %{"a" => [b: {10, 20}]}
iex> Kernel.get_in(data, ~I(a.b[1]))
20

iex> data = %{"a" => [b: {10, 20}]}
iex> Kernel.pop_in(data, ~I(a.b))
{{10, 20}, %{"a" => []}}

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory