From b285f74c1bee1b77112543ec5c0606470cb73e83 Mon Sep 17 00:00:00 2001 From: Fabio Montefuscolo Date: Thu, 3 Apr 2025 00:08:23 +0200 Subject: [PATCH] reorganize things --- src/lib.rs | 2 ++ src/main.rs | 20 +---------- src/slugify.rs | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 src/lib.rs create mode 100644 src/slugify.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..93a1cbf --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,2 @@ +pub mod slugify; +pub use crate::slugify::slugify; diff --git a/src/main.rs b/src/main.rs index 440667a..9376c65 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,29 +1,11 @@ use clap::Parser; +use slugify::slugify; #[derive(Parser)] struct Cli { strings: Vec, } -fn slugify(input: String) -> String { - let mut result = input.to_lowercase(); - - result = result.replace(' ', "-"); - result = result - .chars() - .filter(|&c| c.is_alphanumeric() || c == '_' || c == '-') - .collect(); - - while result.contains("--") { - result = result.replace("--", "-"); - } - - result = result - .trim_matches(|c| c == ' ' || c == '-' || c == '_') - .to_string(); - result -} - fn main() { let args = Cli::parse(); let combined = args.strings.join(" "); diff --git a/src/slugify.rs b/src/slugify.rs new file mode 100644 index 0000000..0b706e7 --- /dev/null +++ b/src/slugify.rs @@ -0,0 +1,91 @@ +pub fn slugify(input: String) -> String { + let mut result = input.to_lowercase(); + + result = result.replace(' ', "-"); + result = result + .chars() + .filter(|&c| c.is_alphanumeric() || c == '_' || c == '-') + .collect(); + + while result.contains("--") { + result = result.replace("--", "-"); + } + + result = result + .trim_matches(|c| c == ' ' || c == '-' || c == '_') + .to_string(); + result +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_lowercase_conversion() { + assert_eq!(slugify("Hello".to_string()), "hello"); + assert_eq!(slugify("WORLD".to_string()), "world"); + assert_eq!(slugify("MiXeD".to_string()), "mixed"); + } + + #[test] + fn test_space_to_hyphen() { + assert_eq!(slugify("hello world".to_string()), "hello-world"); + assert_eq!(slugify("multiple spaces".to_string()), "multiple-spaces"); + } + + #[test] + fn test_trim_trailing_spaces() { + assert_eq!( + slugify(" trailing spaces ".to_string()), + "trailing-spaces" + ); + } + + #[test] + fn test_special_character_removal() { + assert_eq!(slugify("hello!@#$%^&*()world".to_string()), "helloworld"); + assert_eq!( + slugify("keep_underscore-hyphen".to_string()), + "keep_underscore-hyphen" + ); + assert_eq!( + slugify("remove.comma,semicolon;".to_string()), + "removecommasemicolon" + ); + } + + #[test] + fn test_consecutive_hyphen_replacement() { + assert_eq!(slugify("hello--world".to_string()), "hello-world"); + assert_eq!( + slugify("multiple---hyphens".to_string()), + "multiple-hyphens" + ); + } + + #[test] + fn test_trim_special_characters() { + assert_eq!(slugify("-hello-".to_string()), "hello"); + assert_eq!(slugify("_world_".to_string()), "world"); + assert_eq!(slugify(" spaces ".to_string()), "spaces"); + assert_eq!(slugify("-_-mixed_-_".to_string()), "mixed"); + } + + #[test] + fn test_complex_cases() { + assert_eq!( + slugify(" Hello, World! This is a Test ".to_string()), + "hello-world-this-is-a-test" + ); + assert_eq!( + slugify("__Special-CHARS-123!@#".to_string()), + "special-chars-123" + ); + assert_eq!( + slugify("Multiple spaces and---hyphens".to_string()), + "multiple-spaces-and-hyphens" + ); + assert_eq!(slugify("".to_string()), ""); + } +}