-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.ex
40 lines (31 loc) · 1015 Bytes
/
helper.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
defmodule DoIt.Helper do
@moduledoc """
DoIt helper functions.
"""
@doc """
Validates if all allowed values are of the same type.
## Examples
iex> DoIt.Helper.validate_list_type([0, 1, 2, 3], :integer)
true
iex> DoIt.Helper.validate_list_type([0, 1, 2.1, 3], :integer)
false
iex> DoIt.Helper.validate_list_type([0.1, 1.1, 2.1, 3.1], :float)
true
iex> DoIt.Helper.validate_list_type([0.1, 1.1, 2, 3.1], :float)
false
iex> DoIt.Helper.validate_list_type(["warn", "error", "info", "debug"], :string)
true
iex> DoIt.Helper.validate_list_type(["warn", "error", "info", 0], :string)
false
iex> DoIt.Helper.validate_list_type(["warn", "error", "info", "debug"], :unknown)
false
"""
def validate_list_type(list, type) do
case type do
:string -> Enum.all?(list, &is_binary/1)
:integer -> Enum.all?(list, &is_integer/1)
:float -> Enum.all?(list, &is_float/1)
_ -> false
end
end
end