{"id":13507262,"url":"https://github.com/vic/indifferent","last_synced_at":"2025-04-09T18:04:21.196Z","repository":{"id":57506633,"uuid":"75168567","full_name":"vic/indifferent","owner":"vic","description":"Elixir Indifferent access on maps/lists/tuples with custom key transforms.","archived":false,"fork":false,"pushed_at":"2020-03-04T03:53:50.000Z","size":23,"stargazers_count":19,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-23T20:02:53.460Z","etag":null,"topics":["elixir","indifferent-access","parameters","phoenix"],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-30T08:49:20.000Z","updated_at":"2024-12-15T11:00:31.000Z","dependencies_parsed_at":"2022-08-29T20:00:56.610Z","dependency_job_id":null,"html_url":"https://github.com/vic/indifferent","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vic%2Findifferent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vic%2Findifferent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vic%2Findifferent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vic%2Findifferent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vic","download_url":"https://codeload.github.com/vic/indifferent/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248084165,"owners_count":21045123,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elixir","indifferent-access","parameters","phoenix"],"created_at":"2024-08-01T02:00:29.267Z","updated_at":"2025-04-09T18:04:21.169Z","avatar_url":"https://github.com/vic.png","language":"Elixir","funding_links":[],"categories":["Algorithms and Data structures"],"sub_categories":[],"readme":"# Indifferent\n[![Build Status](https://travis-ci.org/vic/indifferent.svg?branch=master)](https://travis-ci.org/vic/indifferent)\n[![help maintain this lib](https://img.shields.io/badge/looking%20for%20maintainer-DM%20%40vborja-663399.svg)](https://twitter.com/vborja)\n\n\nThis library provides a wrapper for indifferent access on maps, structs, lists and tuples.\n\nYou could use Indifferent to wrap a parameters hash or something without having to distingish\non the type of keys (atoms or binaries) akin to the features of Rails' HashWithIndifferentAccess.\n\n## Installation\n\n[Available in Hex](https://hex.pm/packages/indifferent), the package can be installed as:\n\n  1. Add `indifferent` to your list of dependencies in `mix.exs`:\n\n```elixir\n  def deps do\n    [{:indifferent, \"~\u003e 0.9\"}]\n  end\n```\n\n  2. Ensure `indifferent` is started before your application:\n\n```elixir\n  def application do\n    [applications: [:indifferent]]\n  end\n```\n\n## Usage\n\nBe sure to look at the [documentation](https://hexdocs.pm/indifferent) for more examples.\n\n### Indifferent module\n\n\n```elixir\n\n##\n# Wrapping a map and accesing it with an atom key\niex\u003e i = Indifferent.access(%{\"a\" =\u003e 1})\niex\u003e i[:a]\n1\n\n\n##\n# You can provide your custom list of key transformations\n# and specify how are keys indifferents for your use case.\niex\u003e i = Indifferent.access(%{\"a\" =\u003e 1},\n...\u003e   key_transforms: [fn x, _indifferent -\u003e {:ok, String.downcase(x)} end])\niex\u003e i[\"A\"]\n1\n\n##\n# Indifferent is compatible with Elixir's Access\niex\u003e Kernel.get_in(%{\"a\" =\u003e 1}, [Indifferent.at(:a)])\n1\niex\u003e Kernel.get_in(Indifferent.access(%{\"a\" =\u003e 1}), [:a])\n1\n\n##\n# Or you can auto wrap data by using Indifferent's version of\n# `get_in/2`, `get_and_update_in/3`, `pop_in/2`\niex\u003e Indifferent.get_and_update_in(%{\"a\" =\u003e %{\"x\" =\u003e 1}}, [:a, :x], fn x -\u003e {x * 2, x * 4} end)\n{2, %{\"a\" =\u003e %{\"x\" =\u003e 4}}}\n\n\n##\n# Indifferent `path/2` lets you use any syntax you want for\n# accessing the nested value you need.\niex\u003e %{\"b\" =\u003e %{\"c\" =\u003e %{\"d\" =\u003e %{\"e\" =\u003e 4}}}} |\u003e Indifferent.path(b[\"c\"][:d].e)\n4\n\n##\n# And works on lists, tuples, keywords\niex\u003e [9, %{\"c\" =\u003e {:ok, [e: 4]}}] |\u003e Indifferent.path(1.c[\"1\"].e)\n4\n\n##\n# Accessing nil yields nil\niex\u003e nil |\u003e Indifferent.path(foo.bar)\nnil\n\n##\n# Can access inside structs\niex\u003e %User{name: \"john\"} |\u003e Indifferent.path(name)\n\"john\"\n\n##\n# And can be used with Kernel methods.\niex\u003e Kernel.get_in(%{\"a\" =\u003e {0, 2}}, Indifferent.path(a[\"1\"]))\n2\n\n##\n# `path/2` can also take a Keyword of paths and returns a keyword of values\niex\u003e %{\"b\" =\u003e [1, 2]} |\u003e Indifferent.path(x: b[-1])\n[x: 2]\n\n##\n# The `read/1` macro is a convenience that reads an indifferent path on the first value\niex\u003e System.put_env(\"COLOR\", \"red\")\niex\u003e Indifferent.read(System.get_env.COLOR)\n\"red\"\n\n##\n# Just like `path/2`, the `read/1` macro can also take a Keyword of named things to read\niex\u003e System.put_env(\"COLOR\", \"red\")\niex\u003e Process.put(:color, \"blue\")\niex\u003e Indifferent.read(a: System.get_env.COLOR, b: Process.get.color)\n[a: \"red\", b: \"blue\"]\n\n```\n\n### Indifferent sigils\n\nThe `~i` and `~I` sigils are shortcuts for the API\npreviously described.\n\n```elixir\niex\u003e import Indifferent.Sigils\nIndifferent.Sigils\n\n##\n# `~i` is the sigil for `Indifferent.read/1`\niex\u003e data = %{\"a\" =\u003e [b: {10, 20}]}\niex\u003e ~i(data.a.b[1])\n20\n\n# When piped a value it will act just like `Indifferent.path/2`\niex\u003e data = %{\"a\" =\u003e [b: {10, 20}]}\niex\u003e data |\u003e ~i(a.b[1])\n20\n\n\n##\n# `~I` is the sigil version of `Indifferent.path/1`\n# for use with Kernel access functions.\n\niex\u003e data = %{\"a\" =\u003e [b: {10, 20}]}\niex\u003e Kernel.get_in(data, ~I(a.b[1]))\n20\n\niex\u003e data = %{\"a\" =\u003e [b: {10, 20}]}\niex\u003e Kernel.pop_in(data, ~I(a.b))\n{{10, 20}, %{\"a\" =\u003e []}}\n\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvic%2Findifferent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvic%2Findifferent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvic%2Findifferent/lists"}