Packages
surgex
3.2.7
6.0.1
6.0.0
5.1.1
5.1.0
5.0.0
5.0.0-git-015aa39a
4.15.2
4.15.1
4.15.1-git-98b3
4.15.0
4.15.0-git-943e
4.15.0-git-5806
4.14.1
4.14.0
4.13.1
4.13.0
4.12.0
4.11.0
4.11.0-git-97d4
4.11.0-git-14c8
4.10.0
4.10.0-git-37f2
4.9.0
4.9.0-git-9d5f
4.8.1-git-80a7
4.8.0
4.8.0-git-fbda
4.8.0-git-e058
4.7.0
4.7.0-git-5414
4.6.1
4.6.0
4.5.0
4.4.0
4.3.0
4.2.1
4.2.0
4.1.1
4.1.1-git-9f6b
4.1.1-git-6f0cd
4.1.0
4.1.0-git-e934
4.1.0-git-8c28
4.1.0-git-56a9
4.1.0-git-55fd
4.0.0
3.2.8
3.2.7
3.2.6
3.2.5
3.2.4
3.2.3
3.2.2
3.2.1
3.2.0
retired
3.1.0
3.0.0
2.24.1
2.24.0
retired
2.23.0
2.22.0
2.21.0
2.20.1
2.20.0
2.19.0
2.18.1
2.18.0
2.17.0
2.16.0
2.15.0
2.14.0
2.13.0
2.12.1
2.12.0
2.11.0
2.10.0
2.9.0
2.8.0
2.7.0
2.6.0
2.5.1
2.5.0
2.4.0
retired
2.3.0
2.2.1
2.2.0
2.1.1
2.1.0
2.0.0
1.6.0
1.5.2
1.5.1
1.5.0
1.4.0
1.3.1
retired
1.3.0
retired
1.2.1
1.2.0
1.1.0
1.0.0
0.5.2
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.0
0.1.0
All Things Elixir @ Surge Ventures Inc, the creators of Fresha
Current section
Files
Jump to
Current section
Files
lib/surgex/parser/parsers/string_parser.ex
defmodule Surgex.Parser.StringParser do
@moduledoc """
Available options:
- **trim** is trimming whitespaces from the string, takes priority over min and max options
- **min** is a minimal length of the string, returns :too_short error symbol
- **max** is a maximal length of the string, returns :too_long error symbol
"""
@type errors :: :too_short | :too_long
@opts [:trim, :min, :max]
@spec call(nil, any) :: {:ok, nil}
@spec call(String.t(), list) :: {:ok, String.t()} | {:error, errors}
def call(input, opts \\ [])
def call(nil, _opts), do: {:ok, nil}
def call(input, opts) when is_binary(input) do
validate_opts!(opts)
output =
%{value: input, error: nil, opts: opts}
|> process_opts()
|> nullify_empty_output()
case output do
%{value: output, error: nil} -> {:ok, output}
%{error: error} -> {:error, error}
end
end
@spec validate_opts!(list) :: :ok
defp validate_opts!([]), do: :ok
defp validate_opts!(opts) do
invalid_opts = Keyword.keys(opts) -- @opts
if Enum.any?(invalid_opts) do
raise ArgumentError, message: "opts: #{invalid_opts} are invalid for string parser"
end
end
defp process_opts(%{opts: []} = input), do: input
defp process_opts(input) do
input
|> trim()
|> validate_min()
|> validate_max()
end
@spec trim(%{opts: list, value: String.t(), error: nil}) :: %{
opts: list,
value: String.t(),
error: nil
}
def trim(%{opts: opts, value: value} = input) do
trim_value = Keyword.get(opts, :trim)
cond do
is_nil(trim_value) -> input
true -> %{input | value: String.trim(value)}
end
end
@spec validate_min(%{opts: list, value: String.t(), error: nil}) :: %{
opts: list,
value: String.t(),
error: nil | :too_short
}
def validate_min(%{opts: opts, value: value} = input) do
min_value = Keyword.get(opts, :min)
cond do
is_nil(min_value) -> input
String.length(value) >= min_value -> input
true -> %{input | error: :too_short}
end
end
@spec validate_max(%{opts: list, value: String.t(), error: nil}) :: %{
opts: list,
value: String.t(),
error: nil | :too_long
}
def validate_max(%{opts: opts, value: value} = input) do
max_value = Keyword.get(opts, :max)
cond do
is_nil(max_value) -> input
String.length(value) <= max_value -> input
true -> %{input | error: :too_long}
end
end
defp nullify_empty_output(%{value: ""} = input), do: %{input | value: nil}
defp nullify_empty_output(input), do: input
end