vika_cli/generator/
utils.rs

1pub fn to_pascal_case(s: &str) -> String {
2    let mut result = String::new();
3    let mut capitalize_next = true;
4
5    for c in s.chars() {
6        if c == '_' || c == '-' || c == ' ' {
7            capitalize_next = true;
8        } else if capitalize_next {
9            result.push(c.to_uppercase().next().unwrap_or(c));
10            capitalize_next = false;
11        } else {
12            result.push(c);
13        }
14    }
15
16    result
17}
18
19pub fn to_camel_case(s: &str) -> String {
20    let pascal = to_pascal_case(s);
21    if pascal.is_empty() {
22        return pascal;
23    }
24
25    let mut chars = pascal.chars();
26    let first = chars.next().unwrap().to_lowercase().next().unwrap();
27    format!("{}{}", first, chars.as_str())
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn test_to_pascal_case_simple() {
36        assert_eq!(to_pascal_case("hello"), "Hello");
37        assert_eq!(to_pascal_case("world"), "World");
38    }
39
40    #[test]
41    fn test_to_pascal_case_with_underscore() {
42        assert_eq!(to_pascal_case("hello_world"), "HelloWorld");
43        assert_eq!(to_pascal_case("user_name"), "UserName");
44    }
45
46    #[test]
47    fn test_to_pascal_case_with_hyphen() {
48        assert_eq!(to_pascal_case("hello-world"), "HelloWorld");
49        assert_eq!(to_pascal_case("api-key"), "ApiKey");
50    }
51
52    #[test]
53    fn test_to_pascal_case_with_space() {
54        assert_eq!(to_pascal_case("hello world"), "HelloWorld");
55        assert_eq!(to_pascal_case("user name"), "UserName");
56    }
57
58    #[test]
59    fn test_to_pascal_case_mixed() {
60        assert_eq!(to_pascal_case("hello_world-test"), "HelloWorldTest");
61        assert_eq!(to_pascal_case("api_key-name"), "ApiKeyName");
62    }
63
64    #[test]
65    fn test_to_pascal_case_empty() {
66        assert_eq!(to_pascal_case(""), "");
67    }
68
69    #[test]
70    fn test_to_pascal_case_single_word() {
71        assert_eq!(to_pascal_case("test"), "Test");
72    }
73
74    #[test]
75    fn test_to_camel_case_simple() {
76        assert_eq!(to_camel_case("hello"), "hello");
77        assert_eq!(to_camel_case("world"), "world");
78    }
79
80    #[test]
81    fn test_to_camel_case_with_underscore() {
82        assert_eq!(to_camel_case("hello_world"), "helloWorld");
83        assert_eq!(to_camel_case("user_name"), "userName");
84    }
85
86    #[test]
87    fn test_to_camel_case_with_hyphen() {
88        assert_eq!(to_camel_case("hello-world"), "helloWorld");
89        assert_eq!(to_camel_case("api-key"), "apiKey");
90    }
91
92    #[test]
93    fn test_to_camel_case_empty() {
94        assert_eq!(to_camel_case(""), "");
95    }
96
97    #[test]
98    fn test_to_camel_case_single_word() {
99        assert_eq!(to_camel_case("test"), "test");
100    }
101
102    #[test]
103    fn test_to_camel_case_mixed() {
104        assert_eq!(to_camel_case("hello_world-test"), "helloWorldTest");
105    }
106}