From 3a1790933f457d0b9217ed4a49f61a94eec0aa3f Mon Sep 17 00:00:00 2001 From: ahmard Date: Mon, 16 Sep 2024 19:56:00 +0100 Subject: [PATCH 1/2] feat(string): uc_first and uc_words functions --- CHANGELOG.md | 3 +++ Cargo.lock | 2 +- Cargo.toml | 2 +- src/helpers/string.rs | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ed4cd4..daf49ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Medullah Changelog medullah-web changelog file +## 0.20.3 (2024-09-16) +* feat(string): uc_first and uc_words helper functions + ## 0.20.2 (2024-09-16) * bump(deadpool-redis): to version 0.17.0 diff --git a/Cargo.lock b/Cargo.lock index 84ec0fb..ba75445 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1903,7 +1903,7 @@ dependencies = [ [[package]] name = "medullah-web" -version = "0.20.2" +version = "0.20.3" dependencies = [ "base64 0.22.1", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 1303fc3..6a94afe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "medullah-web" -version = "0.20.2" +version = "0.20.3" edition = "2021" license = "MIT" description = "Micro-Framework Base on Ntex" diff --git a/src/helpers/string.rs b/src/helpers/string.rs index c9953e9..8b6760e 100644 --- a/src/helpers/string.rs +++ b/src/helpers/string.rs @@ -3,6 +3,21 @@ use uuid::Uuid; pub struct Str; impl Str { + pub fn uc_first(s: &str) -> String { + let mut chars = s.chars(); + match chars.next() { + None => String::new(), + Some(first_char) => first_char.to_uppercase().collect::() + chars.as_str(), + } + } + + pub fn uc_words(s: &str) -> String { + s.split_whitespace() + .map(|word| Self::uc_first(word)) + .collect::>() + .join(" ") + } + #[cfg(feature = "feat-regex")] pub fn is_username_valid(name: String) -> Box> { let regex = fancy_regex::Regex::new(r"^[a-z][a-z\d\.]{0,37}$").unwrap(); @@ -19,6 +34,26 @@ impl Str { mod tests { use super::*; + #[test] + fn test_uc_first() { + assert_eq!(Str::uc_first("hello"), "Hello"); + assert_eq!(Str::uc_first("rust"), "Rust"); + assert_eq!(Str::uc_first(""), ""); // Test empty string + assert_eq!(Str::uc_first("a"), "A"); // Test single character + assert_eq!(Str::uc_first("hELLO"), "HELLO"); // Test capitalizing first char but not modifying others + assert_eq!(Str::uc_first("1world"), "1world"); // Test first character is non-alphabetic + } + + #[test] + fn test_uc_words() { + assert_eq!(Str::uc_words("hello world"), "Hello World"); + assert_eq!(Str::uc_words("rust programming language"), "Rust Programming Language"); + assert_eq!(Str::uc_words(""), ""); // Test empty string + assert_eq!(Str::uc_words("a b c"), "A B C"); // Test single characters + assert_eq!(Str::uc_words("multiple spaces"), "Multiple Spaces"); // Test multiple spaces + assert_eq!(Str::uc_words("123 hello"), "123 Hello"); // Test with non-alphabetic characters + } + #[cfg(feature = "feat-regex")] #[test] fn test_is_username_valid_valid_usernames() { From f1633cbef8d5e2e5dfa6fe31c0c2cb8324bb68d4 Mon Sep 17 00:00:00 2001 From: ahmard Date: Mon, 16 Sep 2024 20:01:00 +0100 Subject: [PATCH 2/2] replace the closure with the function itself: `Self::uc_first` --- src/helpers/string.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/string.rs b/src/helpers/string.rs index 8b6760e..2e0bf71 100644 --- a/src/helpers/string.rs +++ b/src/helpers/string.rs @@ -13,7 +13,7 @@ impl Str { pub fn uc_words(s: &str) -> String { s.split_whitespace() - .map(|word| Self::uc_first(word)) + .map(Self::uc_first) .collect::>() .join(" ") }