Skip to content

Commit

Permalink
Implement phone-number in Dart
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgoettschkes committed Nov 12, 2023
1 parent 56594b5 commit 85b31f8
Show file tree
Hide file tree
Showing 7 changed files with 740 additions and 0 deletions.
38 changes: 38 additions & 0 deletions dart/phone-number/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Help

## Running the tests

To run the tests:

```sh
$ dart test
```

## Submitting your solution

You can submit your solution using the `exercism submit lib/phone_number.dart` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [Dart track's documentation](https://exercism.org/docs/tracks/dart)
- The [Dart track's programming category on the forum](https://forum.exercism.org/c/programming/dart)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

To get help if you're having trouble, you can use one of the following resources:

- [Dart API Documentation](https://api.dart.dev/)
- [Dart Gitter Chat](https://gitter.im/dart-lang/home)
- [Community Information](https://www.dart.dev/community)
- [/r/dartlang](https://www.reddit.com/r/dartlang) is the Dart subreddit.
- [StackOverflow](https://stackoverflow.com/questions/tagged/dart) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
75 changes: 75 additions & 0 deletions dart/phone-number/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Phone Number

Welcome to Phone Number on Exercism's Dart Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

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.

A function with a *return type* can only return data of that *type* and `null`.
However the function caller is only expecting one data type.

Example:
```dart
String hello(int a){
if ( a == 0){
return "a";
} else {
return null;
}
}
```
To make it more clear that this function can also return `null` or more data types, use `dynamic`.
```dart
dynamic hello(int a){
if ( a == 0){
return "a";
} else {
return null;
}
}
## Source
### Created by
- @devkabiir
### Contributed to by
- @kytrinyx
- @Stargator
### Based on
Exercise by the JumpstartLab team for students at The Turing School of Software and Design. - https://turing.edu
18 changes: 18 additions & 0 deletions dart/phone-number/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
analyzer:
strong-mode:
implicit-casts: false
implicit-dynamic: false
errors:
unused_element: error
unused_import: error
unused_local_variable: error
dead_code: error

linter:
rules:
# Error Rules
- avoid_relative_lib_imports
- avoid_types_as_parameter_names
- literal_only_boolean_expressions
- no_adjacent_strings_in_list
- valid_regexps
37 changes: 37 additions & 0 deletions dart/phone-number/lib/phone_number.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class PhoneNumber {
String clean(String number) {
String cleaned = number.replaceAll(RegExp('[ \-\.]'), '');

if (cleaned.contains(RegExp('[A-Za-z]'))) {
throw FormatException('letters not permitted');
}
if (cleaned.contains(RegExp('[^0-9]'))) {
throw FormatException('punctuations not permitted');
}
if (cleaned.length < 10) {
throw FormatException('must not be fewer than 10 digits');
}
if (cleaned.length > 11) {
throw FormatException('must not be greater than 11 digits');
}
if (cleaned.length == 11 && cleaned[0] != '1') {
throw FormatException('11 digits must start with 1');
}
if (cleaned.length == 11) {
cleaned = cleaned.substring(1);
}
if(cleaned[0] == '0') {
throw FormatException('area code cannot start with zero');
}
if(cleaned[0] == '1') {
throw FormatException('area code cannot start with one');
}
if(cleaned[3] == '0') {
throw FormatException('exchange code cannot start with zero');
}
if(cleaned[3] == '1') {
throw FormatException('exchange code cannot start with one');
}
return cleaned;
}
}
Loading

0 comments on commit 85b31f8

Please sign in to comment.