diff --git a/lib/numerus/formatter.ex b/lib/numerus/formatter.ex index 0a50603..9742cc6 100644 --- a/lib/numerus/formatter.ex +++ b/lib/numerus/formatter.ex @@ -48,27 +48,60 @@ defmodule Numerus.Formatter do iex> Numerus.Formatter.normalize("12065551212") "+12065551212" + + iex> Numerus.Formatter.normalize("98655") # shortcode + "98655" + + iex> Numerus.Formatter.normalize("+12065551212", :one_npan) + "12065551212" ``` + + iex> Numerus.Formatter.normalize("98655", :e164) + {:error, :invalid_format} """ @spec normalize(did :: bitstring, format :: atom() | nil) :: bitstring() | {:error, :invalid_format} - def normalize(did, format) do + def normalize(did, format) when is_bitstring(did) and is_atom(format) do case format do :e164 -> to_e164(did) :npan -> to_npan(did) :one_npan -> to_1npan(did) :us_intl -> to_usintl(did) - :shortcode -> did + :shortcode -> + # we need to verify that the supplied number is a shortcode. If so, + # return the did, if not, then return an error as this conversion is + # not possible. + case Classifier.classify(did) do + {:ok, %{"region" => _, "format" => format}} -> + case format do + :shortcode -> did + _ -> {:error, :invalid_format} + end + end + _ -> {:error, :invalid_format} _ -> {:error, :invalid_format} end end - def normalize(did), do: normalize(did, @normal_format) + def normalize(_,_), do: {:error, :invalid_format} + + def normalize(did) when is_bitstring(did) do + case Classifier.classify(did) do + {:ok, %{"region" => _, "format" => format}} -> + if format == :shortcode do + did + else + normalize(did, @normal_format) + end + _ -> {:error, :invalid_format} + end + end + def normalize(_), do: {:error, :invalid_format} @doc """ Pretty format a telephone number. For NADP numbers. Other numbers will be returned with the country code separated from the main number. """ - @spec format(did :: bitstring()) :: bitstring() | :error + @spec format(did :: bitstring()) :: bitstring() | {:error, :invalid_format} def format(did) when is_bitstring(did) do case Classifier.classify(did) do {:ok, %{"region" => region, "format" => _}} -> @@ -90,14 +123,14 @@ defmodule Numerus.Formatter do _ -> did end end - def format(_), do: :error + def format(_), do: {:error, :invalid_format} # -- convert functions -- # @doc """ Convert the supplied did to E.164 """ - @spec to_e164(did :: bitstring()) :: bitstring() | :error + @spec to_e164(did :: bitstring()) :: bitstring() | {:error, :invalid_format} def to_e164(did) when is_bitstring(did) do case Classifier.classify(did) do {:ok, %{"region" => _, "format" => format}} -> @@ -106,7 +139,7 @@ defmodule Numerus.Formatter do :one_npan -> "+#{did}" :npan -> "+1#{did}" :us_intl -> String.replace(did, ~r/^011/, "+") - _ -> :error + _ -> {:error, :invalid_format} end _ -> # we do not convert other formats, so just return an error. @@ -119,7 +152,7 @@ defmodule Numerus.Formatter do @doc """ Convert the supplied did to npan """ - @spec to_npan(did :: bitstring()) :: bitstring() | :error + @spec to_npan(did :: bitstring()) :: bitstring() | {:error, :invalid_format} def to_npan(did) when is_bitstring(did) do case Classifier.classify(did) do {:ok, %{"region" => region, "format" => format}} -> @@ -133,19 +166,22 @@ defmodule Numerus.Formatter do :e164 -> String.replace(did, ~r/\+1/, "") :one_npan -> String.replace(did, ~r/^1/, "") :npan -> did + _ -> {:error, :invalid_format} end + + _ -> {:error, :invalid_format} end _ -> # we do not convert other regions. just return an error. :error end end - def to_npan(_), do: :error + def to_npan(_), do: {:error, :invalid_format} @doc """ Convert the supplied did to 1npan """ - @spec to_1npan(did :: bitstring()) :: bitstring() | :error + @spec to_1npan(did :: bitstring()) :: bitstring() | {:error, :invalid_format} def to_1npan(did) when is_bitstring(did) do case Classifier.classify(did) do {:ok, %{"region" => region, "format" => format}} -> @@ -159,14 +195,17 @@ defmodule Numerus.Formatter do :e164 -> String.replace(did, ~r/\+/, "") :one_npan -> did :npan -> "1#{did}" + _ -> {:error, :invalid_format} end + + _ -> {:error, :invalid_format} end _ -> # we do not convert other regions. just return an error. :error end end - def to_1npan(_), do: :error + def to_1npan(_), do: {:error, :invalid_format} @doc """ Convert the supplied did to us intl format. @@ -181,11 +220,13 @@ defmodule Numerus.Formatter do case format do :us_intl -> did :e164 -> String.replace(did, ~r/\+/, "011") + _ -> {:error, :invalid_format} end + _ -> {:error, :invalid_format} end end end - def to_usintl(_), do: :error + def to_usintl(_), do: {:error, :invalid_format} # -- format functions -- # end diff --git a/mix.exs b/mix.exs index c1b1081..7f5c416 100644 --- a/mix.exs +++ b/mix.exs @@ -26,7 +26,7 @@ defmodule Numerus.MixProject do def project do [ app: :numerus, - version: "0.1.3", + version: "0.1.4", elixir: "~> 1.14", start_permanent: Mix.env() == :prod, deps: deps(),