Skip to content

Commit 431967e

Browse files
vinmasterpetertseng
authored andcommitted
phone-number 1.2.0: Conform to NANP, rm pretty_print, area_code
NANP (1.1.0): exercism/problem-specifications#745 Remove letters+numbers mixed test (1.2.0): https://github.com/exercism/problem-specifications/pull/772/files Closes #310
1 parent 3281160 commit 431967e

File tree

4 files changed

+50
-65
lines changed

4 files changed

+50
-65
lines changed

exercises/phone-number/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exercises/phone-number/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[package]
22
name = "phone-number"
3-
version = "0.0.0"
3+
version = "1.2.0"

exercises/phone-number/example.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
pub fn number(s: &str) -> Option<String> {
2-
let digits: String = s
3-
.chars()
4-
.filter(|&c| c.is_digit(10))
5-
.collect();
2+
let digits: String = s.chars().filter(|&c| c.is_digit(10)).collect();
63
match digits.len() {
7-
10 => Some(digits),
8-
11 => match digits.chars().nth(0) {
9-
Some('1') => Some(digits[1..].to_string()),
10-
_ => None
11-
},
12-
_ => None
4+
10 => {
5+
match (digits.chars().nth(0), digits.chars().nth(3)) {
6+
(Some('0'), _) => None,
7+
(Some('1'), _) => None,
8+
(_, Some('0')) => None,
9+
(_, Some('1')) => None,
10+
_ => Some(digits),
11+
}
12+
}
13+
11 => {
14+
match digits.chars().nth(0) {
15+
Some('1') => Some(digits[1..].to_string()),
16+
_ => None,
17+
}
18+
}
19+
_ => None,
1320
}
1421
}
15-
16-
pub fn area_code(s: &str) -> Option<String> {
17-
number(s).map(|n| n[..3].to_string())
18-
}
19-
20-
pub fn pretty_print(s: &str) -> String {
21-
number(s).map(|n|
22-
format!("({area}) {prefix}-{exchange}",
23-
area=&n[..3],
24-
prefix=&n[3..6],
25-
exchange=&n[6..])
26-
).unwrap_or("invalid".to_string())
27-
}

exercises/phone-number/tests/phone-number.rs

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ fn to_some_string(s: &str) -> Option<String> {
55
}
66

77
#[test]
8-
fn test_number_cleans() {
9-
assert_eq!(phone::number("(123) 456-7890"), to_some_string("1234567890"));
8+
fn test_cleans_the_number() {
9+
assert_eq!(
10+
phone::number("(223) 456-7890"),
11+
to_some_string("2234567890")
12+
);
1013
}
1114

1215
#[test]
1316
#[ignore]
14-
fn test_number_cleans_with_dots() {
15-
assert_eq!(phone::number("123.456.7890"), to_some_string("1234567890"));
17+
fn test_cleans_numbers_with_dots() {
18+
assert_eq!(phone::number("223.456.7890"), to_some_string("2234567890"));
1619
}
1720

1821
#[test]
1922
#[ignore]
20-
fn test_valid_when_11_digits_and_first_is_1() {
21-
assert_eq!(phone::number("11234567890"), to_some_string("1234567890"));
22-
}
23-
24-
#[test]
25-
#[ignore]
26-
fn test_invalid_when_11_digits() {
27-
assert_eq!(phone::number("21234567890"), None);
23+
fn test_cleans_numbers_with_multiple_spaces() {
24+
assert_eq!(
25+
phone::number("223 456 7890 "),
26+
to_some_string("2234567890")
27+
);
2828
}
2929

3030
#[test]
@@ -35,60 +35,51 @@ fn test_invalid_when_9_digits() {
3535

3636
#[test]
3737
#[ignore]
38-
fn test_invalid_when_empty() {
39-
assert_eq!(phone::number(""), None);
40-
}
41-
42-
#[test]
43-
#[ignore]
44-
fn test_invalid_when_no_digits_present() {
45-
assert_eq!(phone::number(" (-) "), None);
46-
}
47-
48-
#[test]
49-
#[ignore]
50-
fn test_valid_with_leading_characters() {
51-
assert_eq!(phone::number("my number is 123 456 7890"), to_some_string("1234567890"));
38+
fn test_invalid_when_11_digits_does_not_start_with_a_1() {
39+
assert_eq!(phone::number("22234567890"), None);
5240
}
5341

5442
#[test]
5543
#[ignore]
56-
fn test_valid_with_trailing_characters() {
57-
assert_eq!(phone::number("123 456 7890 - bob"), to_some_string("1234567890"));
44+
fn test_valid_when_11_digits_and_starting_with_1() {
45+
assert_eq!(phone::number("12234567890"), to_some_string("2234567890"));
5846
}
5947

6048
#[test]
6149
#[ignore]
62-
fn test_area_code() {
63-
assert_eq!(phone::area_code("1234567890"), to_some_string("123"));
50+
fn test_valid_when_11_digits_and_starting_with_1_even_with_punctuation() {
51+
assert_eq!(
52+
phone::number("+1 (223) 456-7890"),
53+
to_some_string("2234567890")
54+
);
6455
}
6556

6657
#[test]
6758
#[ignore]
68-
fn test_area_code_with_full_us_phone_number() {
69-
assert_eq!(phone::area_code("18234567890"), to_some_string("823"));
59+
fn test_invalid_when_more_than_11_digits() {
60+
assert_eq!(phone::number("321234567890"), None);
7061
}
7162

7263
#[test]
7364
#[ignore]
74-
fn test_area_code_with_invalid() {
75-
assert_eq!(phone::area_code("1234161567890"), None);
65+
fn test_invalid_with_letters() {
66+
assert_eq!(phone::number("123-abc-7890"), None);
7667
}
7768

7869
#[test]
7970
#[ignore]
80-
fn test_pretty_print() {
81-
assert_eq!(phone::pretty_print("1234567890"), "(123) 456-7890");
71+
fn test_invalid_with_punctuations() {
72+
assert_eq!(phone::number("123-@:!-7890"), None);
8273
}
8374

8475
#[test]
8576
#[ignore]
86-
fn test_pretty_print_with_full_us_phone_number() {
87-
assert_eq!(phone::pretty_print("11234567890"), "(123) 456-7890");
77+
fn test_invalid_if_area_code_does_not_start_with_2_9() {
78+
assert_eq!(phone::number("(123) 456-7890"), None);
8879
}
8980

9081
#[test]
9182
#[ignore]
92-
fn test_pretty_print_with_invalid() {
93-
assert_eq!(phone::pretty_print("1186234567890"), "invalid");
83+
fn test_invalid_if_exchange_code_does_not_start_with_2_9() {
84+
assert_eq!(phone::number("(223) 056-7890"), None);
9485
}

0 commit comments

Comments
 (0)