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(string): uc_first and uc_words functions #72

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
35 changes: 35 additions & 0 deletions src/helpers/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>() + chars.as_str(),
}
}

pub fn uc_words(s: &str) -> String {
s.split_whitespace()
.map(Self::uc_first)
.collect::<Vec<_>>()
.join(" ")
}

#[cfg(feature = "feat-regex")]
pub fn is_username_valid(name: String) -> Box<fancy_regex::Result<bool>> {
let regex = fancy_regex::Regex::new(r"^[a-z][a-z\d\.]{0,37}$").unwrap();
Expand All @@ -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() {
Expand Down