Skip to content

Commit 991cc25

Browse files
committed
Adds Helpers
1 parent af1ee53 commit 991cc25

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

lib/util/helpers.ex

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
defmodule Util.Helpers do
2+
@moduledoc """
3+
Miscellaneous helpers
4+
"""
5+
6+
def non_empty_value_or_default(map, key, default) do
7+
case Map.get(map, key) do
8+
val when is_integer(val) and val > 0 -> {:ok, val}
9+
val when is_binary(val) and val != "" -> {:ok, val}
10+
val when is_list(val) and length(val) > 0 -> {:ok, val}
11+
_ -> {:ok, default}
12+
end
13+
end
14+
15+
def not_empty_string(map, key, error_atom \\ "") do
16+
case Map.get(map, key) do
17+
value when is_binary(value) and value != "" ->
18+
{:ok, value}
19+
error_val ->
20+
"'#{key}' - invalid value: '#{error_val}', it must be a not empty string."
21+
|> Util.ToTuple.error(error_atom)
22+
end
23+
end
24+
end

0 commit comments

Comments
 (0)