Skip to content

Commit

Permalink
Add classification and normalize for N11 codes (#5)
Browse files Browse the repository at this point in the history
N11 codes are 3 digit dialing codes used in abbreviated dialing
for the North American Dial Plan.

  - N11 codes are classified as such in
    Numerus.Classifier.classify/1
  - Numerus.Formatter.normalize/1 returns the code when passed
    the N11 number.
  - Bump version to 0.1.5
  • Loading branch information
pramsky authored Feb 18, 2023
1 parent 592db04 commit c8d54d8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
26 changes: 25 additions & 1 deletion lib/numerus/classifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ defmodule Numerus.Classifier do
@npan ~r/\A^[2-9][0-9]{2}[2-9][0-9]{6}$\z/
@one_npan ~r/\A^1[2-9][0-9]{2}[2-9][0-9]{6}$\z/

@n11 ~r/\A^[2-9]11$\z/

# parser regex
# this regex splits the full did into country code + number
@parser ~r/(\+|011)(?<countrycode>9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)(?<number>\d{1,14})$/i
Expand All @@ -67,6 +69,7 @@ defmodule Numerus.Classifier do
# get the formatting
format =
cond do
is_n11?(did) -> :n11
is_e164?(did) -> :e164
is_1npan?(did) -> :one_npan
is_npan?(did) -> :npan
Expand All @@ -82,6 +85,7 @@ defmodule Numerus.Classifier do
true -> :world
false -> case format do
:shortcode -> :nadp
:n11 -> :nadp
_ -> :unknown
end
end
Expand All @@ -92,7 +96,27 @@ defmodule Numerus.Classifier do
end
def classify(_), do: {:error, :invalid_number}

# -- did testing functions -- #
# -- did classification functions -- #
@doc """
Return true if the did is an NXX dialing code. The NXX codes are part of
the North American Dial Plan used for special local services.
The services are:
211 - Community Services
311 - Municipal Government Services
411 - Directory Information
511 - Traffic Information
611 - Telco customer service and repair
711 - TDD and Relay
811 - Public Utility location
911 - Emergency services
"""
@spec is_n11?(did :: bitstring()) :: boolean()
def is_n11?(did) when is_bitstring(did) do
String.match?(did, @n11)
end
def is_n11?(_), do: false

@doc """
Returns true if the supplied did belongs to the North American Dial Plan.
"""
Expand Down
6 changes: 3 additions & 3 deletions lib/numerus/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ defmodule Numerus.Formatter do
{:ok, %{"region" => _, "format" => format}} ->
case format do
:shortcode -> did
:n11 -> did
_ -> {:error, :invalid_format}
end
end
_ -> {:error, :invalid_format}
_ -> {:error, :invalid_format}
_ -> {:error, :invalid_format}
end
end

Expand All @@ -87,7 +87,7 @@ defmodule Numerus.Formatter do
def normalize(did) when is_bitstring(did) do
case Classifier.classify(did) do
{:ok, %{"region" => _, "format" => format}} ->
if format == :shortcode do
if format == :shortcode or format == :n11 do
did
else
normalize(did, @normal_format)
Expand Down
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.4",
version: "0.1.5",
elixir: "~> 1.14",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down

0 comments on commit c8d54d8

Please sign in to comment.