Skip to content

Commit

Permalink
Updated Normalizer (#2)
Browse files Browse the repository at this point in the history
- Added detection and normalization functions for international
  numbers.
- Bump version to 0.1.2
  • Loading branch information
pramsky authored Feb 16, 2023
1 parent a7668d7 commit 6a57055
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
18 changes: 12 additions & 6 deletions lib/numerus/classifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ defmodule Numerus.Classifier do
"""
@spec classify(did :: bitstring()) :: {:ok, map()} | {:error, term()}
def classify(did) when is_bitstring(did) do
# get the region for this did, :nadp, or :world
region = case is_nadp?(did) do
true -> :nadp
false -> :world
end

# get the formatting
format =
cond do
Expand All @@ -81,6 +75,18 @@ defmodule Numerus.Classifier do
true -> :unknown
end

# get the region for this did, :nadp, or :world
region = case is_nadp?(did) do
true -> :nadp
false -> case String.match?(did, @intl_n) or String.match?(did, @intl) do
true -> :world
false -> case format do
:shortcode -> :nadp
_ -> :unknown
end
end
end

classification = %{"region" => region, "format" => format}
{:ok, classification}
end
Expand Down
20 changes: 20 additions & 0 deletions lib/numerus/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ defmodule Numerus.Formatter do
:e164 -> to_e164(did)
:npan -> to_npan(did)
:one_npan -> to_1npan(did)
:us_intl -> to_usintl(did)
_ -> {:error, :invalid_format}
end
end
Expand Down Expand Up @@ -165,5 +166,24 @@ defmodule Numerus.Formatter do
end
def to_1npan(_), do: :error

@doc """
Convert the supplied did to us intl format.
"""
@spec to_usintl(did :: bitstring()) :: bitstring() | :error
def to_usintl(did) when is_bitstring(did) do
case Classifier.classify(did) do
{:ok, %{"region" => region, "format" => format}} ->
case region do
:nadp -> :error
:world ->
case format do
:us_intl -> did
:e164 -> String.replace(did, ~r/\+/, "011")
end
end
end
end
def to_usintl(_), do: :error

# -- 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.1",
version: "0.1.2",
elixir: "~> 1.14",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down

0 comments on commit 6a57055

Please sign in to comment.