Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement initials name #338

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions thaw/src/avatar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,20 @@ pub fn Avatar(
}
}

// TODO
fn initials_name(name: String) -> String {
if name.len() < 2 {
name.to_ascii_uppercase()
} else {
name.split_at(2).0.to_string().to_ascii_uppercase()
let initials: Vec<_> = name
.split_whitespace()
.filter_map(|word| {
word.chars()
.next()
.map(|c| c.to_uppercase().collect::<String>())
})
.collect();

match initials.as_slice() {
[first, .., last] => format!("{first}{last}"),
[first] => first.clone(),
[] => String::new(),
}
}

Expand All @@ -144,3 +152,16 @@ impl AvatarShape {
}
}
}

#[test]
fn test_initials_name() {
assert_eq!(initials_name("Jane Doe".into()), "JD".to_string());
assert_eq!(initials_name("Ben".into()), "B".to_string());
assert_eq!(
initials_name("ÇFoo Bar 1Name too ÉLong".into()),
"ÇÉ".to_string()
);
assert_eq!(initials_name("ffl ß".into()), "FFLSS".to_string());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be changed to return FS if wanted

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The string length should not exceed 2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed 👍

assert_eq!(initials_name("".into()), "".to_string());
assert_eq!(initials_name("山".into()), "山".to_string());
}