trans_case/
lib.rs

1mod util;
2
3use crate::util::{to_camel_case, to_lower_case, to_snake_case, to_title_case, to_upper_case};
4
5pub struct TransCase {
6    words: Vec<String>,
7}
8
9/// Case
10///
11pub enum Case {
12    /// Upper Case
13    Upper,
14    /// Lower Case
15    Lower,
16    /// Title Case
17    Title,
18    /// Snake Case
19    Snake,
20    /// Camel Case
21    Camel,
22}
23
24/// Trans Case
25///
26/// Transform case.
27///
28///
29/// ## Example
30//
31/// ```rs
32/// use trans_case::{TransCase, Case};
33///
34/// let sentence = TransCase::new("trans-case in rust");
35///
36/// println!("{}", sentence.case(Case::Upper)); // TRANS CASE IN RUST
37/// println!("{}", sentence.case(Case::Title)); // Trans Case In Rust
38/// println!("{}", sentence.case(Case::Camel)); // transCaseInRust
39/// ```
40///
41impl TransCase {
42    pub fn new(value: &str) -> Self {
43        let words = value
44            .replace("-", " ")
45            .replace("_", " ")
46            .split(" ")
47            .map(|word| word.to_string())
48            .collect::<Vec<String>>();
49
50        Self { words }
51    }
52
53    pub fn case(&self, case: Case) -> String {
54        use Case::*;
55
56        match case {
57            Upper => to_upper_case(&self.words.join(" ")),
58            Lower => to_lower_case(&self.words.join(" ")),
59            Title => to_title_case(&self.words.join(" ")),
60            Snake => to_snake_case(&self.words.join(" ")),
61            Camel => to_camel_case(&self.words.join(" ")),
62        }
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn test_trans_case() {
72        let sentence = TransCase::new("Tanveer_evan mashup");
73
74        assert_eq!(
75            sentence.case(Case::Upper),
76            String::from("TANVEER EVAN MASHUP")
77        );
78        assert_eq!(
79            sentence.case(Case::Lower),
80            String::from("tanveer evan mashup")
81        );
82        assert_eq!(
83            sentence.case(Case::Title),
84            String::from("Tanveer Evan Mashup")
85        );
86        assert_eq!(
87            sentence.case(Case::Snake),
88            String::from("tanveer_evan_mashup")
89        );
90        assert_eq!(
91            sentence.case(Case::Camel),
92            String::from("tanveerEvanMashup")
93        );
94    }
95}