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: remove_item_from_list/2 #57

Merged
merged 7 commits into from
Sep 14, 2023
Merged
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ Install by adding `useful` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:useful, "~> 1.12.1"}
{:useful, "~> 1.13.1"}
]
end
```
@@ -149,6 +149,35 @@ But `Elixir` "_Me no likey_" ...
So this is what we have.


### `remove_item_from_list/2`

Remove an `item` from a `list`.

With numbers:

```elixir
list = [1, 2, 3, 4]
Useful.remove_item_from_list(list, 3)
[1, 2, 4]
```

With a `List` of `Strings`:

```elixir
list = ["climate", "change", "is", "not", "real"]
Useful.remove_item_from_list(list, "not")
["climate", "change", "is", "real"]
```

The `list` is the first argument to the function
so it's easy to pipe:

```elixir
get_list_of_items(person_id)
|> Useful.remove_item_from_list("item_to_be_removed")
|> etc.
```


### `stringify_map/1`

26 changes: 22 additions & 4 deletions lib/useful.ex
Original file line number Diff line number Diff line change
@@ -119,23 +119,40 @@ defmodule Useful do
# https://hexdocs.pm/elixir/1.14/Kernel.html#get_in/2
# Enum.each(keys, )
try do
dbg(map)

case get_in(map, keys) do
nil -> default
result -> result
end
rescue
any ->
dbg(any)
_ ->
default
end
end

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

## Examples

iex> list = ["They'll", "never", "take", "our", "freedom!"]
iex> Useful.remove_item_from_list(list, "never")
["They'll", "take", "our", "freedom!"]

"""
def remove_item_from_list(list, item) do
if Enum.member?(list, item) do
i = Enum.find_index(list, fn it -> it == item end)
List.delete_at(list, i)
else
list
end
end

@doc """
`stringy_map/1` converts a `Map` of any depth/nesting into a string.
Deeply nested maps are denoted by "__" (double underscore). See flatten_map/1
for more details.
Alphabetizes the keys for consistency. See: github.com/dwyl/useful/issues/56

## Examples

@@ -149,6 +166,7 @@ defmodule Useful do
def stringify_map(map) when is_map(map) do
map
|> flatten_map()
|> Enum.sort()
|> Enum.map(&stringify_tuple/1)
|> Enum.join(", ")
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ defmodule Useful.MixProject do
[
app: :useful,
description: "A collection of useful functions",
version: "1.12.1",
version: "1.13.1",
elixir: "~> 1.10",
start_permanent: Mix.env() == :prod,
deps: deps(),
30 changes: 28 additions & 2 deletions test/useful_test.exs
Original file line number Diff line number Diff line change
@@ -186,6 +186,32 @@ defmodule UsefulTest do
end
end

describe "remove_item_from_list/2" do
test "remove_item_from_list/2 removes a numeric item from a list" do
list = [1, 2, 3, 4]
# tl/1 = "tail of list" hexdocs.pm/elixir/1.15.5/Kernel.html#tl/1
assert Useful.remove_item_from_list(list, 1) == tl(list)
end

test "remove_item_from_list/2 removes a numeric item in any position" do
list = [1, 2, 3, 4]
updated_list = [1, 2, 4]
assert Useful.remove_item_from_list(list, 3) == updated_list
end

test "remove_item_from_list/2 removes an item from the list" do
list = ["don't", "panic", "about", "climate", "change"]
# tl/1 = "tail of list" hexdocs.pm/elixir/1.15.5/Kernel.html#tl/1
assert Useful.remove_item_from_list(list, "don't") == tl(list)
end

test "attempt to remove_item_from_list/2 ignores item *not* in list" do
item = "save"
list = ["AI", "will", "destroy", "us"]
assert Useful.remove_item_from_list(list, item) == list
end
end

describe "stringfy_map/1" do
test "returns nil string when map is nil" do
assert Useful.stringify_map(nil) == "nil"
@@ -197,8 +223,8 @@ defmodule UsefulTest do
end

test "converts nested maps into strings" do
map = %{id: 1, data: %{name: "Vitor", other: %{data: "info"}}}
assert Useful.stringify_map(map) == "data__name: Vitor, data__other__data: info, id: 1"
map = %{id: 1, data: %{name: "Vitor", nested: %{data: "info"}}}
assert Useful.stringify_map(map) == "data__name: Vitor, data__nested__data: info, id: 1"
end

test "converts nested lists into strings" do