From f6e0ec7f798cbb7df8daaf4fe42d15ac6e79507d Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Wed, 18 Dec 2024 15:02:22 -0600 Subject: [PATCH] Use HashSet, lowercase, and add emoji tests --- crates/notedeck_columns/src/post.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/crates/notedeck_columns/src/post.rs b/crates/notedeck_columns/src/post.rs index 534dae7..40d2bc3 100644 --- a/crates/notedeck_columns/src/post.rs +++ b/crates/notedeck_columns/src/post.rs @@ -138,14 +138,14 @@ impl NewPost { .expect("expected build to work") } - fn extract_hashtags(content: &str) -> Vec { - let mut hashtags = Vec::new(); + fn extract_hashtags(content: &str) -> HashSet { + 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); } } } @@ -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 = expected.into_iter().map(String::from).collect(); assert_eq!( result, - expected.into_iter().map(String::from).collect::>(), + expected, "Failed for input: {}", input );