Skip to content

Commit

Permalink
Use HashSet, lowercase, and add emoji tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dsaxton committed Dec 18, 2024
1 parent 7b21d38 commit f6e0ec7
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions crates/notedeck_columns/src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ impl NewPost {
.expect("expected build to work")
}

fn extract_hashtags(content: &str) -> Vec<String> {
let mut hashtags = Vec::new();
fn extract_hashtags(content: &str) -> HashSet<String> {
let mut hashtags = HashSet::new();
for word in content.split_whitespace() {
if word.starts_with('#') && word.len() > 1 {
let tag = word[1..].trim_end_matches(|c: char| !c.is_alphanumeric())
.to_string();
.to_lowercase();
if !tag.is_empty() {
hashtags.push(tag);
hashtags.insert(tag);
}
}
}
Expand All @@ -166,13 +166,18 @@ mod tests {
("#tag1 with #tag2!", vec!["tag1", "tag2"]),
("Ignore # empty", vec![]),
("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 {
let result = NewPost::extract_hashtags(input);
let expected: HashSet<String> = expected.into_iter().map(String::from).collect();
assert_eq!(
result,
expected.into_iter().map(String::from).collect::<Vec<_>>(),
expected,
"Failed for input: {}",
input
);
Expand Down

0 comments on commit f6e0ec7

Please sign in to comment.