mirror of
https://github.com/correl/elm.git
synced 2024-12-28 11:14:16 +00:00
Revise phone-number to use Maybe
This commit is contained in:
parent
6b79102bac
commit
90e7d88374
2 changed files with 44 additions and 24 deletions
exercises/phone-number
|
@ -2,23 +2,43 @@ module PhoneNumber (..) where
|
||||||
|
|
||||||
import String
|
import String
|
||||||
|
|
||||||
nums = ['1','2','3','4','5','6','7','8','9','0']
|
|
||||||
|
|
||||||
getNumber : String -> String
|
nums =
|
||||||
getNumber text = getValidNum (String.filter isDigit text)
|
[ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' ]
|
||||||
|
|
||||||
|
|
||||||
|
getNumber : String -> Maybe String
|
||||||
|
getNumber text =
|
||||||
|
getValidNum (String.filter isDigit text)
|
||||||
|
|
||||||
|
|
||||||
isDigit : Char -> Bool
|
isDigit : Char -> Bool
|
||||||
isDigit char = List.any (\n -> n == char) nums
|
isDigit char =
|
||||||
|
List.any (\n -> n == char) nums
|
||||||
|
|
||||||
getValidNum : String -> String
|
|
||||||
|
getValidNum : String -> Maybe String
|
||||||
getValidNum num =
|
getValidNum num =
|
||||||
if String.length num == 10 then num else
|
if String.length num == 10 then
|
||||||
if (String.length num == 11) && (String.left 1 num == "1") then String.dropLeft 1 num else
|
Just num
|
||||||
String.repeat 10 "0"
|
else if (String.length num == 11) && (String.left 1 num == "1") then
|
||||||
|
Just (String.dropLeft 1 num)
|
||||||
|
else
|
||||||
|
Nothing
|
||||||
|
|
||||||
printPretty : String -> String
|
|
||||||
printPretty input = formatNumber (getNumber input)
|
prettyPrint : String -> Maybe String
|
||||||
|
prettyPrint input =
|
||||||
|
Maybe.map formatNumber (getNumber input)
|
||||||
|
|
||||||
|
|
||||||
formatNumber : String -> String
|
formatNumber : String -> String
|
||||||
formatNumber input = String.concat ["(", (String.slice 0 3 input), ") ", (String.slice 3 6 input), "-", (String.slice 6 10 input)]
|
formatNumber input =
|
||||||
|
String.concat
|
||||||
|
[ "("
|
||||||
|
, (String.slice 0 3 input)
|
||||||
|
, ") "
|
||||||
|
, (String.slice 3 6 input)
|
||||||
|
, "-"
|
||||||
|
, (String.slice 6 10 input)
|
||||||
|
]
|
||||||
|
|
|
@ -3,7 +3,7 @@ module Main (..) where
|
||||||
import Task
|
import Task
|
||||||
import Console
|
import Console
|
||||||
import ElmTest exposing (..)
|
import ElmTest exposing (..)
|
||||||
import PhoneNumber exposing (getNumber, printPretty)
|
import PhoneNumber exposing (getNumber, prettyPrint)
|
||||||
|
|
||||||
|
|
||||||
tests : Test
|
tests : Test
|
||||||
|
@ -12,40 +12,40 @@ tests =
|
||||||
"PhoneNumber"
|
"PhoneNumber"
|
||||||
[ test
|
[ test
|
||||||
"cleans number"
|
"cleans number"
|
||||||
(assertEqual "1234567890" (getNumber "(123) 456-7890"))
|
(assertEqual (Just "1234567890") (getNumber "(123) 456-7890"))
|
||||||
, test
|
, test
|
||||||
"cleans number with dots"
|
"cleans number with dots"
|
||||||
(assertEqual "1234567890" (getNumber "123.456.7890"))
|
(assertEqual (Just "1234567890") (getNumber "123.456.7890"))
|
||||||
, test
|
, test
|
||||||
"valid when 11 digits and first is 1"
|
"valid when 11 digits and first is 1"
|
||||||
(assertEqual "1234567890" (getNumber "11234567890"))
|
(assertEqual (Just "1234567890") (getNumber "11234567890"))
|
||||||
, test
|
, test
|
||||||
"invalid when 11 digits"
|
"invalid when 11 digits"
|
||||||
(assertEqual "0000000000" (getNumber "21234567890"))
|
(assertEqual Nothing (getNumber "21234567890"))
|
||||||
, test
|
, test
|
||||||
"invalid when 9 digits"
|
"invalid when 9 digits"
|
||||||
(assertEqual "0000000000" (getNumber "123456789"))
|
(assertEqual Nothing (getNumber "123456789"))
|
||||||
, test
|
, test
|
||||||
"invalid when 12 digits"
|
"invalid when 12 digits"
|
||||||
(assertEqual "0000000000" (getNumber "123456789012"))
|
(assertEqual Nothing (getNumber "123456789012"))
|
||||||
, test
|
, test
|
||||||
"invalid when empty"
|
"invalid when empty"
|
||||||
(assertEqual "0000000000" (getNumber ""))
|
(assertEqual Nothing (getNumber ""))
|
||||||
, test
|
, test
|
||||||
"invalid when no digits present"
|
"invalid when no digits present"
|
||||||
(assertEqual "0000000000" (getNumber " (-) "))
|
(assertEqual Nothing (getNumber " (-) "))
|
||||||
, test
|
, test
|
||||||
"valid with leading characters"
|
"valid with leading characters"
|
||||||
(assertEqual "1234567890" (getNumber "my number is 123 456 7890"))
|
(assertEqual (Just "1234567890") (getNumber "my number is 123 456 7890"))
|
||||||
, test
|
, test
|
||||||
"valid with trailing characters"
|
"valid with trailing characters"
|
||||||
(assertEqual "1234567890" (getNumber "123 456 7890 - bob"))
|
(assertEqual (Just "1234567890") (getNumber "123 456 7890 - bob"))
|
||||||
, test
|
, test
|
||||||
"pretty print"
|
"pretty print"
|
||||||
(assertEqual "(123) 456-7890" (printPretty "1234567890"))
|
(assertEqual (Just "(123) 456-7890") (prettyPrint "1234567890"))
|
||||||
, test
|
, test
|
||||||
"pretty print with full us phone number"
|
"pretty print with full us phone number"
|
||||||
(assertEqual "(123) 456-7890" (printPretty "11234567890"))
|
(assertEqual (Just "(123) 456-7890") (prettyPrint "11234567890"))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue