-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Mayo.List module and add atom, list validation methods to Mayo.Any
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
defmodule Mayo.List do | ||
@doc """ | ||
Checks the minimum number of elements in the list. | ||
iex> Mayo.List.min([1, 2, 3], 3) | ||
[1, 2, 3] | ||
iex> Mayo.List.min([], 3) | ||
{:error, %Mayo.Error{type: "list.min"}} | ||
""" | ||
def min(value, len) when is_list(value) and length(value) == len, do: value | ||
|
||
def min(value, _) when is_list(value) do | ||
{:error, %Mayo.Error{ | ||
type: "list.min" | ||
}} | ||
end | ||
|
||
def min(value, _), do: value | ||
|
||
@doc """ | ||
Checks the maximum number of elements in the list. | ||
iex> Mayo.List.max([1, 2, 3], 3) | ||
[1, 2, 3] | ||
iex> Mayo.List.max([1, 2, 3, 4], 3) | ||
{:error, %Mayo.Error{type: "list.max"}} | ||
""" | ||
def max(value, len) when is_list(value) and length(value) == len, do: value | ||
|
||
def max(value, _) when is_list(value) do | ||
{:error, %Mayo.Error{ | ||
type: "list.max" | ||
}} | ||
end | ||
|
||
def max(value, _), do: value | ||
|
||
@doc """ | ||
Checks the number of elements in the list. | ||
iex> Mayo.List.length([1, 2, 3], 3) | ||
[1, 2, 3] | ||
iex> Mayo.List.length([1], 3) | ||
{:error, %Mayo.Error{type: "list.length"}} | ||
""" | ||
def length(value, len) when is_list(value) and length(value) == len, do: value | ||
|
||
def length(value, _) when is_list(value) do | ||
{:error, %Mayo.Error{ | ||
type: "list.length" | ||
}} | ||
end | ||
|
||
def length(value, _), do: value | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
defmodule Mayo.ListTest do | ||
use ExUnit.Case | ||
doctest Mayo.List | ||
end |