https://github.com/danielberkompas/destructure
Javascript-style destructuring for Elixir
https://github.com/danielberkompas/destructure
elixir hex
Last synced: 9 months ago
JSON representation
Javascript-style destructuring for Elixir
- Host: GitHub
- URL: https://github.com/danielberkompas/destructure
- Owner: danielberkompas
- License: mit
- Created: 2016-10-31T21:04:47.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2022-08-31T20:08:49.000Z (over 3 years ago)
- Last Synced: 2024-05-09T21:34:10.381Z (over 1 year ago)
- Topics: elixir, hex
- Language: Elixir
- Size: 20.5 KB
- Stars: 107
- Watchers: 6
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Destructure
[](https://hex.pm/packages/destructure)
[](https://danielberkompas.semaphoreci.com/projects/destructure)
Adds Javascript-style destructuring to Elixir. When working with a `map`, instead
of writing match operation like this:
```elixir
def full_name(%{first_name: first_name, last_name: last_name}) do
"#{first_name} #{last_name}"
end
```
You can write:
```elixir
import Destructure
def full_name(d%{first_name, last_name}) do
"#{first_name} #{last_name}"
end
```
It also works with `structs` and `keyword`.
```elixir
import Destructure
def full_name(d%Person{first_name, last_name}) do
"#{first_name} #{last_name}"
end
def full_name(d[first_name, last_name]) do
"#{first_name} #{last_name}"
end
```
You can also do it in a case statement.
```elixir
case post(url, data) do
{:ok, d%{body}} -> # instead of {:ok, %{body: body}}
# use body variable
_other ->
# ...
end
```
Unlike Javascript, you can still bind custom variables:
```elixir
d(%{first, last, email: mail}) = %{...}
```
See the [Hex Documentation](https://hexdocs.pm/destructure) for more details.
## Installation
Add `destructure` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[{:destructure, "~> 0.2.3"}]
end
```