# paulfioravanti's solution

## to Phone Number in the Elm Track

Published at Jul 27 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Clean up user-entered phone numbers so that they can be sent SMS messages.

The North American Numbering Plan (NANP) is a telephone numbering system used by many countries in North America like the United States, Canada or Bermuda. All NANP-countries share the same international country code: `1`.

NANP numbers are ten-digit numbers consisting of a three-digit Numbering Plan Area code, commonly known as area code, followed by a seven-digit local number. The first three digits of the local number represent the exchange code, followed by the unique four-digit number which is the subscriber number.

The format is usually represented as

``````(NXX)-NXX-XXXX
``````

where `N` is any digit from 2 through 9 and `X` is any digit from 0 through 9.

Your task is to clean up differently formatted telephone numbers by removing punctuation and the country code (1) if present.

For example, the inputs

• `+1 (613)-995-0253`
• `613-995-0253`
• `1 613 995 0253`
• `613.995.0253`

should all produce the output

`6139950253`

Note: As this exercise only deals with telephone numbers used in NANP-countries, only 1 is considered a valid country code.

## Elm Installation

Refer to the Installing Elm page for information about installing elm.

## Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

``````\$ elm-test
``````

Automatically run tests again when you save changes:

``````\$ elm-test --watch
``````

As you work your way through the test suite, be sure to remove the `skip <|` calls from each test until you get them all passing!

## Source

Event Manager by JumpstartLab http://tutorials.jumpstartlab.com/projects/eventmanager.html

## Submitting Incomplete Solutions

It is possible to submit an incomplete solution so you can see how others have completed the exercise.

### Tests.elm

``````module Tests exposing (tests)

import Expect
import PhoneNumber exposing (getNumber)
import Test exposing (..)

tests : Test
tests =
describe "PhoneNumber"
[ test "cleans the number" <|
\() -> Expect.equal (Just "2234567890") (getNumber "(223) 456-7890")
, skip <|
test "cleans numbers with dots" <|
\() -> Expect.equal (Just "2234567890") (getNumber "223.456.7890")
, skip <|
test "cleans numbers with multiple spaces" <|
\() -> Expect.equal (Just "2234567890") (getNumber "223 456   7890   ")
, skip <|
test "invalid when 9 digits" <|
\() -> Expect.equal Nothing (getNumber "223456789")
, skip <|
test "invalid when 11 digits does not start with a 1" <|
\() -> Expect.equal Nothing (getNumber "22234567890")
, skip <|
test "valid when 11 digits and starting with 1" <|
\() -> Expect.equal (Just "2234567890") (getNumber "12234567890")
, skip <|
test "valid when 11 digits and starting with 1 even with punctuation" <|
\() -> Expect.equal (Just "2234567890") (getNumber "+1 (223) 456-7890")
, skip <|
test "invalid when more than 11 digits" <|
\() -> Expect.equal Nothing (getNumber "321234567890")
, skip <|
test "invalid with letters" <|
\() -> Expect.equal Nothing (getNumber "123-abc-7890")
, skip <|
test "invalid with punctuations" <|
\() -> Expect.equal Nothing (getNumber "123-@:!-7890")
, skip <|
test "invalid if area code starts with 0" <|
\() -> Expect.equal Nothing (getNumber "(023) 456-7890")
, skip <|
test "invalid if area code starts with 1" <|
\() -> Expect.equal Nothing (getNumber "(123) 456-7890")
, skip <|
test "invalid if exchange code starts with 0" <|
\() -> Expect.equal Nothing (getNumber "(223) 056-7890")
, skip <|
test "invalid if exchange code starts with 1" <|
\() -> Expect.equal Nothing (getNumber "(223) 156-7890")
]``````
``````module PhoneNumber exposing (getNumber)

import Parser exposing ((|.), (|=), Parser)

{-| North American Numbering Plan (NANP)
-}
type alias NANPNumber =
{ areaCode : String
, exchangeCode : String
, subscriberNumber : String
}

getNumber : String -> Maybe String
getNumber phoneNumber =
let
nanpNumber =
Parser.run nanpNumberParser phoneNumber
in
case nanpNumber of
Ok { areaCode, exchangeCode, subscriberNumber } ->
Just (areaCode ++ exchangeCode ++ subscriberNumber)

Err _ ->
Nothing

-- PRIVATE

nanpNumberParser : Parser NANPNumber
nanpNumberParser =
Parser.succeed NANPNumber
|. countryCode
|. whitespace
|. leftParenthesis
|= nxxComponent
|. rightParenthesis
|. divider
|= nxxComponent
|. divider
|= xxxxComponent
|. whitespace
|. Parser.end

countryCode : Parser ()
countryCode =
Parser.chompWhile (\char -> char == '+' || char == '1')

whitespace : Parser ()
whitespace =
Parser.chompWhile (\char -> char == ' ')

leftParenthesis : Parser ()
leftParenthesis =
Parser.chompWhile (\char -> char == '(')

{-| NXX component of a NANP number covers the 3-digit pattern for the area code
and exchange code, where N is a number between 2-9, and Xs are any digit.
REF: <https://en.wikipedia.org/wiki/North_American_Numbering_Plan#Modern_plan>
-}
nxxComponent : Parser String
nxxComponent =
let
between2and9 char =
Char.isDigit char && char > '1' && char <= '9'
in
Parser.getChompedString
(Parser.succeed ()
|. Parser.chompIf between2and9
|. Parser.chompIf Char.isDigit
|. Parser.chompIf Char.isDigit
)

rightParenthesis : Parser ()
rightParenthesis =
Parser.chompWhile (\char -> char == ')')

divider : Parser ()
divider =
let
dividerCharacter char =
char == ' ' || char == '.' || char == '-'
in
Parser.chompWhile dividerCharacter

{-| XXXX component of a NANP number covers the 4-digit pattern for a subscriber
number.
REF: <https://en.wikipedia.org/wiki/North_American_Numbering_Plan#Modern_plan>
-}
xxxxComponent : Parser String
xxxxComponent =
Parser.getChompedString
(Parser.succeed ()
|. Parser.chompIf Char.isDigit
|. Parser.chompIf Char.isDigit
|. Parser.chompIf Char.isDigit
|. Parser.chompIf Char.isDigit
)``````