mirror of
https://github.com/aljazceru/notedeck.git
synced 2025-12-18 17:14:21 +01:00
Use HashSet, lowercase, and add emoji tests
This commit is contained in:
@@ -138,14 +138,14 @@ impl NewPost {
|
|||||||
.expect("expected build to work")
|
.expect("expected build to work")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extract_hashtags(content: &str) -> Vec<String> {
|
fn extract_hashtags(content: &str) -> HashSet<String> {
|
||||||
let mut hashtags = Vec::new();
|
let mut hashtags = HashSet::new();
|
||||||
for word in content.split_whitespace() {
|
for word in content.split_whitespace() {
|
||||||
if word.starts_with('#') && word.len() > 1 {
|
if word.starts_with('#') && word.len() > 1 {
|
||||||
let tag = word[1..].trim_end_matches(|c: char| !c.is_alphanumeric())
|
let tag = word[1..].trim_end_matches(|c: char| !c.is_alphanumeric())
|
||||||
.to_string();
|
.to_lowercase();
|
||||||
if !tag.is_empty() {
|
if !tag.is_empty() {
|
||||||
hashtags.push(tag);
|
hashtags.insert(tag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -166,13 +166,18 @@ mod tests {
|
|||||||
("#tag1 with #tag2!", vec!["tag1", "tag2"]),
|
("#tag1 with #tag2!", vec!["tag1", "tag2"]),
|
||||||
("Ignore # empty", vec![]),
|
("Ignore # empty", vec![]),
|
||||||
("Keep #alphanumeric123", vec!["alphanumeric123"]),
|
("Keep #alphanumeric123", vec!["alphanumeric123"]),
|
||||||
|
("Testing emoji #🍌sfd", vec!["🍌sfd"]),
|
||||||
|
("Testing emoji with space #🍌 sfd", vec!["🍌"]),
|
||||||
|
("Duplicate #tag #tag #tag", vec!["tag"]),
|
||||||
|
("Mixed case #TaG #tag #TAG", vec!["tag"]),
|
||||||
];
|
];
|
||||||
|
|
||||||
for (input, expected) in test_cases {
|
for (input, expected) in test_cases {
|
||||||
let result = NewPost::extract_hashtags(input);
|
let result = NewPost::extract_hashtags(input);
|
||||||
|
let expected: HashSet<String> = expected.into_iter().map(String::from).collect();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
result,
|
result,
|
||||||
expected.into_iter().map(String::from).collect::<Vec<_>>(),
|
expected,
|
||||||
"Failed for input: {}",
|
"Failed for input: {}",
|
||||||
input
|
input
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user