Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: list_tuple_to_unique_keys/1 #59 [release] #62

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Install by adding `useful` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:useful, "~> 1.13.1"}
{:useful, "~> 1.14.0"}
]
end
```
Expand Down Expand Up @@ -148,6 +148,20 @@ person_id = conn.assigns.person.id || 0
But `Elixir` "_Me no likey_" ...
So this is what we have.

### `list_tuple_to_unique_keys/1`

Turns a list of tuples with the _same_ key
into a list of tuples with _unique_ keys.
Useful when dealing with "multipart" forms
that upload multiple files. e.g:

```elixir
parts = [{"file", "pic1.png"}, {"file", "pic2.png"}]
Useful.list_tuples_to_unique_keys(parts)
[{"file-1", "pic1.png"}, {"file-2", "pic2.png"}]
```



### `remove_item_from_list/2`

Expand Down
49 changes: 24 additions & 25 deletions lib/useful.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,30 @@ defmodule Useful do
end
end

@doc """
`list_tuples_to_unique_keys/1` turns a list of tuples
with the same key into a list of tuples with unique keys.
Useful when dealing with "multipart" forms that upload multiple files.

## Example

iex> parts = [{"file", "header", "pic1.png"}, {"file", "header", "pic2.png"}]
iex> Useful.list_tuples_to_unique_keys(parts)
[{"file-1", "header", "pic1.png"}, {"file-2", "header", "pic2.png"}]
"""
def list_tuples_to_unique_keys(parts) do
key = parts |> hd() |> elem(0)
new_keys = Enum.map(1..length(parts), &(key <> "-#{&1}"))

Enum.zip_reduce([parts, new_keys], [], fn [elt, new_key], acc ->
[
elt |> Tuple.delete_at(0) |> Tuple.insert_at(0, new_key)
| acc
]
end)
|> Enum.sort()
end

@doc """
`remove_item_from_list/2` removes a given `item` from a `list` in any position.

Expand Down Expand Up @@ -286,31 +310,6 @@ defmodule Useful do
def typeof(x) when unquote(:"is_#{type}")(x), do: unquote(type)
end

@doc """
`list_tuples_to_unique_keys/1` turns a list of tuples with the same key into a list of tuples with a unique key.

Useful when you deal with "multipart".

## Example

iex> parts = [{"file", "header", "pic1.png"}, {"file", "header", "pic2.png"}]
iex> Useful.list_tuples_to_unique_keys(parts)
[{"file-1", "header", "pic1.png"}, {"file-2", "header", "pic2.png"}]
"""

def list_tuples_to_unique_keys(parts) do
key = parts |> hd() |> elem(0)
new_keys = Enum.map(1..length(parts), &(key <> "-#{&1}"))

Enum.zip_reduce([parts, new_keys], [], fn [elt, new_key], acc ->
[
elt |> Tuple.delete_at(0) |> Tuple.insert_at(0, new_key)
| acc
]
end)
|> Enum.sort()
end

# No idea how to test this. Do you? ¯\_(ツ)_/¯
# coveralls-ignore-start
def typeof(_) do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Useful.MixProject do
[
app: :useful,
description: "A collection of useful functions",
version: "1.13.1",
version: "1.14.0",
elixir: "~> 1.10",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down
2 changes: 1 addition & 1 deletion test/useful_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ defmodule UsefulTest do
assert Useful.typeof(:atom) == "atom"
end

#  recap: https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html
# recap: https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html
test "returns \"binary\" when variable is a binary" do
string = "hello"
assert Useful.typeof(string) == "binary"
Expand Down
Loading