2016-05-13 02:26:52 +00:00
|
|
|
module PhoneNumber exposing (..)
|
2015-10-13 23:05:05 +00:00
|
|
|
|
|
|
|
import String
|
|
|
|
|
|
|
|
|
2016-03-17 01:33:59 +00:00
|
|
|
nums =
|
2016-06-19 21:46:13 +00:00
|
|
|
[ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' ]
|
2016-03-17 01:33:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
getNumber : String -> Maybe String
|
|
|
|
getNumber text =
|
2016-06-19 21:46:13 +00:00
|
|
|
getValidNum (String.filter isDigit text)
|
2016-03-17 01:33:59 +00:00
|
|
|
|
2015-10-13 23:05:05 +00:00
|
|
|
|
|
|
|
isDigit : Char -> Bool
|
2016-03-17 01:33:59 +00:00
|
|
|
isDigit char =
|
2016-06-19 21:46:13 +00:00
|
|
|
List.any (\n -> n == char) nums
|
2015-10-13 23:05:05 +00:00
|
|
|
|
2016-03-17 01:33:59 +00:00
|
|
|
|
|
|
|
getValidNum : String -> Maybe String
|
2015-10-13 23:05:05 +00:00
|
|
|
getValidNum num =
|
2016-06-19 21:46:13 +00:00
|
|
|
if String.length num == 10 then
|
|
|
|
Just num
|
|
|
|
else if (String.length num == 11) && (String.left 1 num == "1") then
|
|
|
|
Just (String.dropLeft 1 num)
|
|
|
|
else
|
|
|
|
Nothing
|
2016-03-17 01:33:59 +00:00
|
|
|
|
2015-10-13 23:05:05 +00:00
|
|
|
|
2016-03-17 01:33:59 +00:00
|
|
|
prettyPrint : String -> Maybe String
|
|
|
|
prettyPrint input =
|
2016-06-19 21:46:13 +00:00
|
|
|
Maybe.map formatNumber (getNumber input)
|
2015-10-13 23:05:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
formatNumber : String -> String
|
2016-03-17 01:33:59 +00:00
|
|
|
formatNumber input =
|
2016-06-19 21:46:13 +00:00
|
|
|
String.concat
|
|
|
|
[ "("
|
|
|
|
, (String.slice 0 3 input)
|
|
|
|
, ") "
|
|
|
|
, (String.slice 3 6 input)
|
|
|
|
, "-"
|
|
|
|
, (String.slice 6 10 input)
|
|
|
|
]
|