{"id":21564628,"url":"https://github.com/ruby2elixir/atomic_map","last_synced_at":"2025-08-21T10:31:29.742Z","repository":{"id":57479083,"uuid":"56599081","full_name":"ruby2elixir/atomic_map","owner":"ruby2elixir","description":"A small utility to convert deep Elixir maps with mixed string/atom keys to atom-only keyed maps.","archived":false,"fork":false,"pushed_at":"2018-09-07T15:49:11.000Z","size":23,"stargazers_count":46,"open_issues_count":1,"forks_count":7,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-09T19:51:48.597Z","etag":null,"topics":["elixir","helper","structs"],"latest_commit_sha":null,"homepage":"","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/ruby2elixir.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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-04-19T13:28:13.000Z","updated_at":"2025-06-08T11:36:23.000Z","dependencies_parsed_at":"2022-09-17T04:42:18.467Z","dependency_job_id":null,"html_url":"https://github.com/ruby2elixir/atomic_map","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ruby2elixir/atomic_map","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby2elixir%2Fatomic_map","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby2elixir%2Fatomic_map/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby2elixir%2Fatomic_map/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby2elixir%2Fatomic_map/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ruby2elixir","download_url":"https://codeload.github.com/ruby2elixir/atomic_map/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby2elixir%2Fatomic_map/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271462892,"owners_count":24764062,"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","status":"online","status_checked_at":"2025-08-21T02:00:08.990Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","helper","structs"],"created_at":"2024-11-24T10:16:36.390Z","updated_at":"2025-08-21T10:31:29.417Z","avatar_url":"https://github.com/ruby2elixir.png","language":"Elixir","readme":"# AtomicMap\n\n[![Build status](https://travis-ci.org/ruby2elixir/atomic_map.svg \"Build status\")](https://travis-ci.org/ruby2elixir/atomic_map)\n[![Hex version](https://img.shields.io/hexpm/v/atomic_map.svg \"Hex version\")](https://hex.pm/packages/atomic_map)\n![Hex downloads](https://img.shields.io/hexpm/dt/atomic_map.svg \"Hex downloads\")\n\n\nA small utility to convert Elixir maps with mixed string/atom keys to atom-only keyed maps. Optionally with a safe option, to prevent [atom space exhaustion of the Erlang VM](https://erlangcentral.org/wiki/index.php?title=String_Conversion_To_Atom). Since v0.8 it also supports conversion of keys from `CamelCase` to `under_score` format.\n\nAtomicMap can convert simple maps or nested structures such as lists of maps; see [Nested Structures](#nested-structures) below for examples.\n\n## Usage\n\n### Safety\n\nWith no options, it safely converts string keys in maps to atoms, using [`String.to_existing_atom/1`](https://hexdocs.pm/elixir/String.html#to_existing_atom/1).\n\n```elixir\niex\u003e AtomicMap.convert(%{\"a\" =\u003e \"a\", \"b\" =\u003e \"b\", c: \"c\"})\n%{a: \"a\", b: \"b\", c: \"c\"}\n```\n\nBecause safe conversion uses [`String.to_existing_atom/1`](https://hexdocs.pm/elixir/String.html#to_existing_atom/1), it will raise when the target atom does not exist.\n\n```elixir\niex\u003e AtomicMap.convert(%{\"abcdefg\" =\u003e \"a\", \"b\" =\u003e \"b\"})\n** (ArgumentError) argument error\n    :erlang.binary_to_existing_atom(\"abcdefg\", :utf8)\n```\n\nTo have safe conversion can ignore unsafe keys, leaving them as strings, pass `true` for the `ignore` option.\n\n```elixir\niex\u003e AtomicMap.convert(%{\"abcdefg\" =\u003e \"a\", \"b\" =\u003e \"b\"}, %{ignore: true})\n%{:b =\u003e \"b\", \"abcdefg\" =\u003e \"a\"}\n```\n\nTo disable safe conversion and allow new atoms to be created, pass `false` for the `safe:` option.\n(This makes the `ignore` option irrelevant.)\nIf the input is user-generated, converting only expected keys will prevent excessive atom creation.\n\n```elixir\niex\u003e map = %{\"expected_key\" =\u003e \"a\", \"b\" =\u003e \"b\", \"unexpected_key\" =\u003e \"c\"}\n%{\"expected_key\" =\u003e \"a\", \"b\" =\u003e \"b\", \"unexpected_key\" =\u003e \"c\"}\n\niex\u003e filtered_map = Map.take(map, [\"expected_key\", \"b\"])\n%{\"b\" =\u003e \"b\", \"expected_key\" =\u003e \"a\"}\n\niex\u003e AtomicMap.convert(filtered_map, %{safe: false})\n%{b: \"b\", expected_key: \"a\"}\n```\n\n### Underscoring\n\nBy default, `\"CamelCase\"` string keys will be converted to `under_score` atom keys.\n\n```\niex\u003e AtomicMap.convert(%{ \"CamelCase\" =\u003e \"hi\" })\n** (ArgumentError) argument error\n    :erlang.binary_to_existing_atom(\"camel_case\", :utf8)\n```\n\nNote that `\"camel_case\"` was the string that failed conversion.\nIf that atom is explicitly created first, the conversion will succeed.\n\n```elixir\niex\u003e :camel_case\n:camel_case\niex\u003e AtomicMap.convert(%{ \"CamelCase\" =\u003e \"hi\" })\n%{camel_case: \"hi\"}\n```\n\nAllowing unsafe conversions will also work.\nIf the input is user-generated, converting only expected keys will prevent excessive atom creation.\n\n```elixir\niex\u003e map = %{\"CamelCase\" =\u003e \"a\", \"b\" =\u003e \"b\", \"AnotherCamelCase\" =\u003e \"c\"}\n%{\"CamelCase\" =\u003e \"a\", \"b\" =\u003e \"b\", \"AnotherCamelCase\" =\u003e \"c\"}\n\niex\u003e filtered_map = Map.take(map, [\"CamelCase\", \"b\"])\n%{\"b\" =\u003e \"b\", \"CamelCase\" =\u003e \"a\"}\n\niex\u003e AtomicMap.convert(filtered_map, %{safe: false})\n%{b: \"b\", camel_case: \"a\"}\n```\n\n### Replacing Hyphens\n\n`\"hyphenated-string\"` keys will always be converted to `under_score` atom keys.\nThere is currently no way to disable this behavior.\n\n```elixir\niex\u003e AtomicMap.convert(%{\"some-key\" =\u003e \"a\", \"b\" =\u003e \"c\"})\n** (ArgumentError) argument error\n    :erlang.binary_to_existing_atom(\"some_key\", :utf8)\n```\n\nNote that `\"some_key\"` was the string that failed conversion.\nIf that atom is explicitly created first, the conversion will succeed.\n\n```elixir\niex\u003e :some_key\n:some_key\n\niex\u003e AtomicMap.convert(%{\"some-key\" =\u003e \"a\", \"b\" =\u003e \"c\"})\n%{b: \"c\", some_key: \"a\"}\n```\n\nAllowing unsafe conversions will also work.\nIf the input is user-generated, converting only expected keys will prevent excessive atom creation.\n\n```elixir\niex\u003e map = %{\"some-key\" =\u003e \"a\", \"b\" =\u003e \"b\", \"another-key\" =\u003e \"c\"}\n%{\"some-key\" =\u003e \"a\", \"b\" =\u003e \"b\", \"another-key\" =\u003e \"c\"}\n\niex\u003e filtered_map = Map.take(map, [\"some-key\", \"b\"])\n%{\"b\" =\u003e \"b\", \"some-key\" =\u003e \"a\"}\n\niex\u003e AtomicMap.convert(filtered_map, %{safe: false})\n%{b: \"b\", some_key: \"a\"}\n```\n\n### Nested Structures\n\n```elixir\n# works with nested maps\niex\u003e AtomicMap.convert(%{\"a\" =\u003e 2, \"b\" =\u003e %{\"c\" =\u003e 4}})\n%{a: 2, b: %{c: 4}}\n\n# works with nested maps + lists + mixed key types (atoms + binaries)\niex\u003e AtomicMap.convert([ %{\"c\" =\u003e 1}, %{:c =\u003e 2}, %{\"c\" =\u003e %{:b =\u003e 4}}], %{safe: true})\n[%{c: 1}, %{c: 2}, %{c: %{b: 4}}]\n```\n\n## Installation\n  1. Add atomic_map to your list of dependencies in `mix.exs`:\n\n        def deps do\n          [{:atomic_map, \"~\u003e 0.8\"}]\n        end\n\n## Todo:\n  - maybe allow direct conversion to a struct, like Poison does it: as: %SomeStruct{}...\n\n\n## Benchmark\n\n    $ mix bench\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruby2elixir%2Fatomic_map","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fruby2elixir%2Fatomic_map","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruby2elixir%2Fatomic_map/lists"}