diff options
Diffstat (limited to 'raku/phone-number')
-rw-r--r-- | raku/phone-number/Phone.rakumod | 16 | ||||
-rw-r--r-- | raku/phone-number/README.md | 52 | ||||
-rw-r--r-- | raku/phone-number/phone-number.rakutest | 200 |
3 files changed, 268 insertions, 0 deletions
diff --git a/raku/phone-number/Phone.rakumod b/raku/phone-number/Phone.rakumod new file mode 100644 index 0000000..f2c828c --- /dev/null +++ b/raku/phone-number/Phone.rakumod @@ -0,0 +1,16 @@ +unit module Phone; + +constant @errors = ( + '11 digits must start with 1', + 'more than 11 digits', + 'incorrect number of digits', + 'letters not permitted', + 'punctuations not permitted', + 'area code cannot start with zero', + 'area code cannot start with one', + 'exchange code cannot start with zero', + 'exchange code cannot start with one', +); + +sub clean-number ($number) is export { +} diff --git a/raku/phone-number/README.md b/raku/phone-number/README.md new file mode 100644 index 0000000..ee01f5a --- /dev/null +++ b/raku/phone-number/README.md @@ -0,0 +1,52 @@ +# Phone Number + +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 + +```text +(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. + +## Resources + +Remember to check out the Raku [documentation](https://docs.raku.org/) and +[resources](https://raku.org/resources/) pages for information, tips, and +examples if you get stuck. + +## Running the tests + +There is a test suite and module included with the exercise. +The test suite (a file with the extension `.rakutest`) will attempt to run routines +from the module (a file with the extension `.rakumod`). +Add/modify routines in the module so that the tests will pass! You can view the +test data by executing the command `raku --doc *.rakutest` (\* being the name of the +test suite), and run the test suite for the exercise by executing the command +`prove6 .` in the exercise directory. + +## Source + +Event Manager by JumpstartLab [http://tutorials.jumpstartlab.com/projects/eventmanager.html](http://tutorials.jumpstartlab.com/projects/eventmanager.html) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/raku/phone-number/phone-number.rakutest b/raku/phone-number/phone-number.rakutest new file mode 100644 index 0000000..a8638ef --- /dev/null +++ b/raku/phone-number/phone-number.rakutest @@ -0,0 +1,200 @@ +#!/usr/bin/env raku +use Test; +use JSON::Fast; +use lib $?FILE.IO.dirname; +use Phone; +plan 18; + +my @test-cases = from-json($=pod.pop.contents).List; +for @test-cases -> %case { + given %case<expected> { + when Str { + is clean-number(%case<input><phrase>), + |%case<expected description>; + } + + when .<error>.so { + throws-like + { clean-number %case<input><phrase> }, + Exception, + :message(%case<expected><error>), + %case<description>; + } + } +} + +=head2 Test Cases +=begin code +[ + { + "description": "Cleanup user-entered phone numbers: cleans the number", + "expected": "2234567890", + "input": { + "phrase": "(223) 456-7890" + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: cleans numbers with dots", + "expected": "2234567890", + "input": { + "phrase": "223.456.7890" + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: cleans numbers with multiple spaces", + "expected": "2234567890", + "input": { + "phrase": "223 456 7890 " + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: invalid when 9 digits", + "expected": { + "error": "incorrect number of digits" + }, + "input": { + "phrase": "123456789" + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: invalid when 11 digits does not start with a 1", + "expected": { + "error": "11 digits must start with 1" + }, + "input": { + "phrase": "22234567890" + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: valid when 11 digits and starting with 1", + "expected": "2234567890", + "input": { + "phrase": "12234567890" + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: valid when 11 digits and starting with 1 even with punctuation", + "expected": "2234567890", + "input": { + "phrase": "+1 (223) 456-7890" + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: invalid when more than 11 digits", + "expected": { + "error": "more than 11 digits" + }, + "input": { + "phrase": "321234567890" + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: invalid with letters", + "expected": { + "error": "letters not permitted" + }, + "input": { + "phrase": "123-abc-7890" + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: invalid with punctuations", + "expected": { + "error": "punctuations not permitted" + }, + "input": { + "phrase": "123-@:!-7890" + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: invalid if area code starts with 0", + "expected": { + "error": "area code cannot start with zero" + }, + "input": { + "phrase": "(023) 456-7890" + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: invalid if area code starts with 1", + "expected": { + "error": "area code cannot start with one" + }, + "input": { + "phrase": "(123) 456-7890" + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: invalid if exchange code starts with 0", + "expected": { + "error": "exchange code cannot start with zero" + }, + "input": { + "phrase": "(223) 056-7890" + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: invalid if exchange code starts with 1", + "expected": { + "error": "exchange code cannot start with one" + }, + "input": { + "phrase": "(223) 156-7890" + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: invalid if area code starts with 0 on valid 11-digit number", + "expected": { + "error": "area code cannot start with zero" + }, + "input": { + "phrase": "1 (023) 456-7890" + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: invalid if area code starts with 1 on valid 11-digit number", + "expected": { + "error": "area code cannot start with one" + }, + "input": { + "phrase": "1 (123) 456-7890" + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: invalid if exchange code starts with 0 on valid 11-digit number", + "expected": { + "error": "exchange code cannot start with zero" + }, + "input": { + "phrase": "1 (223) 056-7890" + }, + "property": "clean" + }, + { + "description": "Cleanup user-entered phone numbers: invalid if exchange code starts with 1 on valid 11-digit number", + "expected": { + "error": "exchange code cannot start with one" + }, + "input": { + "phrase": "1 (223) 156-7890" + }, + "property": "clean" + } +] +=end code |