mirror of
https://github.com/correl/elm.git
synced 2025-03-31 01:07:34 -09:00
Add PhoneNumber example and test
This commit is contained in:
parent
e6fbf7f8a8
commit
40a69d61d0
3 changed files with 54 additions and 1 deletions
24
PhoneNumber/PhoneNumberExample.elm
Normal file
24
PhoneNumber/PhoneNumberExample.elm
Normal file
|
@ -0,0 +1,24 @@
|
|||
module PhoneNumberExample where
|
||||
|
||||
import String
|
||||
|
||||
nums = ['1','2','3','4','5','6','7','8','9','0']
|
||||
|
||||
getNumber : String -> String
|
||||
getNumber text = getValidNum (String.filter isDigit text)
|
||||
|
||||
isDigit : Char -> Bool
|
||||
isDigit char = List.any (\n -> n == char) nums
|
||||
|
||||
getValidNum : String -> String
|
||||
getValidNum num =
|
||||
if String.length num == 10 then num else
|
||||
if (String.length num == 11) && (String.left 1 num == "1") then String.dropLeft 1 num else
|
||||
String.repeat 10 "0"
|
||||
|
||||
printPretty : String -> String
|
||||
printPretty input = formatNumber (getNumber input)
|
||||
|
||||
|
||||
formatNumber : String -> String
|
||||
formatNumber input = String.concat ["(", (String.slice 0 3 input), ") ", (String.slice 3 6 input), "-", (String.slice 6 10 input)]
|
28
PhoneNumber/PhoneNumberTest.elm
Normal file
28
PhoneNumber/PhoneNumberTest.elm
Normal file
|
@ -0,0 +1,28 @@
|
|||
module PhoneNumberTest where
|
||||
|
||||
-- TODO - remove example inclusion once Problem sets are ready to go live or CI is set up.
|
||||
|
||||
import PhoneNumberExample exposing (getNumber, printPretty)
|
||||
|
||||
import ElmTest.Test exposing (test, Test, suite)
|
||||
import ElmTest.Assertion exposing (assert, assertEqual)
|
||||
import ElmTest.Runner.Element exposing (runDisplay)
|
||||
|
||||
tests : Test
|
||||
tests = suite "PhoneNumber test suite"
|
||||
[
|
||||
test "cleans number" (assertEqual "1234567890" (getNumber "(123) 456-7890")),
|
||||
test "cleans number with dots" (assertEqual "1234567890" (getNumber "123.456.7890")),
|
||||
test "valid when 11 digits and first is 1" (assertEqual "1234567890" (getNumber "11234567890")),
|
||||
test "invalid when 11 digits" (assertEqual "0000000000" (getNumber "21234567890")),
|
||||
test "invalid when 9 digits" (assertEqual "0000000000" (getNumber "123456789")),
|
||||
test "invalid when 12 digits" (assertEqual "0000000000" (getNumber "123456789012")),
|
||||
test "invalid when empty" (assertEqual "0000000000" (getNumber "")),
|
||||
test "invalid when no digits present" (assertEqual "0000000000" (getNumber " (-) ")),
|
||||
test "valid with leading characters" (assertEqual "1234567890" (getNumber "my number is 123 456 7890")),
|
||||
test "valid with trailing characters" (assertEqual "1234567890" (getNumber "123 456 7890 - bob")),
|
||||
test "pretty print" (assertEqual "(123) 456-7890" (printPretty "1234567890")),
|
||||
test "pretty print with full us phone number" (assertEqual "(123) 456-7890" (printPretty "11234567890"))
|
||||
]
|
||||
|
||||
main = runDisplay tests
|
|
@ -26,7 +26,8 @@
|
|||
"Strain",
|
||||
"SpaceAge",
|
||||
"Anagram",
|
||||
"NucleotideCount"
|
||||
"NucleotideCount",
|
||||
"PhoneNumber"
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
|
|
Loading…
Add table
Reference in a new issue