Skip to content

Commit

Permalink
Add convert opt (#4)
Browse files Browse the repository at this point in the history
- Added the shortcode option to normalize/2
- shortcode conversion to anything other than a shortcode will
  return an error tuple.
- All conversion methods have an error tuple for unknown formats.
- Bump version to 0.1.4
  • Loading branch information
pramsky authored Feb 18, 2023
1 parent 982dff8 commit 592db04
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
65 changes: 53 additions & 12 deletions lib/numerus/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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" => _}} ->
Expand All @@ -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}} ->
Expand All @@ -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.
Expand All @@ -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}} ->
Expand All @@ -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}} ->
Expand All @@ -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.
Expand All @@ -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
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 592db04

Please sign in to comment.