Skip to content

Commit

Permalink
Merge pull request #57 from dwyl/remove_item_from_list-#55
Browse files Browse the repository at this point in the history
PR: `remove_item_from_list/2`
  • Loading branch information
LuchoTurtle authored Sep 14, 2023
2 parents 370a032 + a2c5135 commit eefeb4f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
31 changes: 30 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.12.1"}
{:useful, "~> 1.13.1"}
]
end
```
Expand Down Expand Up @@ -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`

Expand Down
26 changes: 22 additions & 4 deletions lib/useful.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
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.12.1",
version: "1.13.1",
elixir: "~> 1.10",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down
30 changes: 28 additions & 2 deletions test/useful_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit eefeb4f

Please sign in to comment.