summaryrefslogtreecommitdiff
path: root/utils/string_proc/src/format_processer.rs
blob: bac84c0ba2b7b911cee64f35bced02d7a57b867e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
pub struct FormatProcesser {
    content: Vec<String>,
}

impl From<String> for FormatProcesser {
    fn from(value: String) -> Self {
        Self {
            content: Self::process_string(value),
        }
    }
}

impl From<&str> for FormatProcesser {
    fn from(value: &str) -> Self {
        Self {
            content: Self::process_string(value.to_string()),
        }
    }
}

impl FormatProcesser {
    /// Process the string into an intermediate format
    fn process_string(input: String) -> Vec<String> {
        let mut result = String::new();
        let mut prev_space = false;

        for c in input.chars() {
            match c {
                'a'..='z' | 'A'..='Z' | '0'..='9' => {
                    result.push(c);
                    prev_space = false;
                }
                '_' | ',' | '.' | '-' | ' ' => {
                    if !prev_space {
                        result.push(' ');
                        prev_space = true;
                    }
                }
                _ => {}
            }
        }

        let mut processed = String::new();
        let mut chars = result.chars().peekable();

        while let Some(c) = chars.next() {
            processed.push(c);
            if let Some(&next) = chars.peek()
                && c.is_lowercase()
                && next.is_uppercase()
            {
                processed.push(' ');
            }
        }

        processed
            .to_lowercase()
            .split_whitespace()
            .map(|s| s.to_string())
            .collect()
    }

    /// Convert to camelCase format (brewCoffee)
    ///
    /// # Examples
    ///
    /// ```
    /// # use string_proc::format_processer::FormatProcesser;
    /// let processor = FormatProcesser::from("brew_coffee");
    /// assert_eq!(processor.to_camel_case(), "brewCoffee");
    /// ```
    pub fn to_camel_case(&self) -> String {
        let mut result = String::new();
        for (i, word) in self.content.iter().enumerate() {
            if i == 0 {
                result.push_str(&word.to_lowercase());
            } else {
                let mut chars = word.chars();
                if let Some(first) = chars.next() {
                    result.push_str(&first.to_uppercase().collect::<String>());
                    result.push_str(&chars.collect::<String>().to_lowercase());
                }
            }
        }
        result
    }

    /// Convert to PascalCase format (BrewCoffee)
    ///
    /// # Examples
    ///
    /// ```
    /// # use string_proc::format_processer::FormatProcesser;
    /// let processor = FormatProcesser::from("brew_coffee");
    /// assert_eq!(processor.to_pascal_case(), "BrewCoffee");
    /// ```
    pub fn to_pascal_case(&self) -> String {
        let mut result = String::new();
        for word in &self.content {
            let mut chars = word.chars();
            if let Some(first) = chars.next() {
                result.push_str(&first.to_uppercase().collect::<String>());
                result.push_str(&chars.collect::<String>().to_lowercase());
            }
        }
        result
    }

    /// Convert to kebab-case format (brew-coffee)
    ///
    /// # Examples
    ///
    /// ```
    /// # use string_proc::format_processer::FormatProcesser;
    /// let processor = FormatProcesser::from("brew_coffee");
    /// assert_eq!(processor.to_kebab_case(), "brew-coffee");
    /// ```
    pub fn to_kebab_case(&self) -> String {
        self.content.join("-").to_lowercase()
    }

    /// Convert to snake_case format (brew_coffee)
    ///
    /// # Examples
    ///
    /// ```
    /// # use string_proc::format_processer::FormatProcesser;
    /// let processor = FormatProcesser::from("brewCoffee");
    /// assert_eq!(processor.to_snake_case(), "brew_coffee");
    /// ```
    pub fn to_snake_case(&self) -> String {
        self.content.join("_").to_lowercase()
    }

    /// Convert to dot.case format (brew.coffee)
    ///
    /// # Examples
    ///
    /// ```
    /// # use string_proc::format_processer::FormatProcesser;
    /// let processor = FormatProcesser::from("brew_coffee");
    /// assert_eq!(processor.to_dot_case(), "brew.coffee");
    /// ```
    pub fn to_dot_case(&self) -> String {
        self.content.join(".").to_lowercase()
    }

    /// Convert to Title Case format (Brew Coffee)
    ///
    /// # Examples
    ///
    /// ```
    /// # use string_proc::format_processer::FormatProcesser;
    /// let processor = FormatProcesser::from("brew_coffee");
    /// assert_eq!(processor.to_title_case(), "Brew Coffee");
    /// ```
    pub fn to_title_case(&self) -> String {
        let mut result = String::new();
        for word in &self.content {
            let mut chars = word.chars();
            if let Some(first) = chars.next() {
                result.push_str(&first.to_uppercase().collect::<String>());
                result.push_str(&chars.collect::<String>().to_lowercase());
            }
            result.push(' ');
        }
        result.pop();
        result
    }

    /// Convert to lower case format (brew coffee)
    ///
    /// # Examples
    ///
    /// ```
    /// # use string_proc::format_processer::FormatProcesser;
    /// let processor = FormatProcesser::from("BREW COFFEE");
    /// assert_eq!(processor.to_lower_case(), "brew coffee");
    /// ```
    pub fn to_lower_case(&self) -> String {
        self.content.join(" ").to_lowercase()
    }

    /// Convert to UPPER CASE format (BREW COFFEE)
    ///
    /// # Examples
    ///
    /// ```
    /// # use string_proc::format_processer::FormatProcesser;
    /// let processor = FormatProcesser::from("brew coffee");
    /// assert_eq!(processor.to_upper_case(), "BREW COFFEE");
    /// ```
    pub fn to_upper_case(&self) -> String {
        self.content.join(" ").to_uppercase()
    }
}

#[cfg(test)]
mod tests {
    use crate::format_processer::FormatProcesser;

    #[test]
    fn test_processer() {
        let test_cases = vec![
            ("brew_coffee", "brewCoffee"),
            ("brew, coffee", "brewCoffee"),
            ("brew-coffee", "brewCoffee"),
            ("Brew.Coffee", "brewCoffee"),
            ("bRewCofFee", "bRewCofFee"),
            ("brewCoffee", "brewCoffee"),
            ("b&rewCoffee", "brewCoffee"),
            ("BrewCoffee", "brewCoffee"),
            ("brew.coffee", "brewCoffee"),
            ("Brew_Coffee", "brewCoffee"),
            ("BREW COFFEE", "brewCoffee"),
        ];

        for (input, expected) in test_cases {
            let processor = FormatProcesser::from(input);
            assert_eq!(
                processor.to_camel_case(),
                expected,
                "Failed for input: '{}'",
                input
            );
        }
    }

    #[test]
    fn test_conversions() {
        let processor = FormatProcesser::from("brewCoffee");

        assert_eq!(processor.to_upper_case(), "BREW COFFEE");
        assert_eq!(processor.to_lower_case(), "brew coffee");
        assert_eq!(processor.to_title_case(), "Brew Coffee");
        assert_eq!(processor.to_dot_case(), "brew.coffee");
        assert_eq!(processor.to_snake_case(), "brew_coffee");
        assert_eq!(processor.to_kebab_case(), "brew-coffee");
        assert_eq!(processor.to_pascal_case(), "BrewCoffee");
        assert_eq!(processor.to_camel_case(), "brewCoffee");
    }
}