From 6a57055002270e4989bb3c20e52229f70408273a Mon Sep 17 00:00:00 2001 From: Pramod Venugopal Date: Thu, 16 Feb 2023 10:38:38 -0800 Subject: [PATCH] Updated Normalizer (#2) - Added detection and normalization functions for international numbers. - Bump version to 0.1.2 --- lib/numerus/classifier.ex | 18 ++++++++++++------ lib/numerus/formatter.ex | 20 ++++++++++++++++++++ mix.exs | 2 +- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/numerus/classifier.ex b/lib/numerus/classifier.ex index c25724c..48252b0 100644 --- a/lib/numerus/classifier.ex +++ b/lib/numerus/classifier.ex @@ -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 @@ -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 diff --git a/lib/numerus/formatter.ex b/lib/numerus/formatter.ex index aacd26b..0ee5a41 100644 --- a/lib/numerus/formatter.ex +++ b/lib/numerus/formatter.ex @@ -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 @@ -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 diff --git a/mix.exs b/mix.exs index 073a282..3168c1b 100644 --- a/mix.exs +++ b/mix.exs @@ -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(),