Unnamed repository; edit this file 'description' to name the repository.
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
use crate::Tendril;

// todo: should this be grapheme aware?

pub fn to_pascal_case(text: impl Iterator<Item = char>) -> Tendril {
    let mut res = Tendril::new();
    to_pascal_case_with(text, &mut res);
    res
}

pub fn to_pascal_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
    let mut at_word_start = true;
    for c in text {
        // we don't count _ as a word char here so case conversions work well
        if !c.is_alphanumeric() {
            at_word_start = true;
            continue;
        }
        if at_word_start {
            at_word_start = false;
            buf.extend(c.to_uppercase());
        } else {
            buf.push(c)
        }
    }
}

pub fn to_upper_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
    for c in text {
        for c in c.to_uppercase() {
            buf.push(c)
        }
    }
}

pub fn to_lower_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
    for c in text {
        for c in c.to_lowercase() {
            buf.push(c)
        }
    }
}

pub fn to_camel_case(text: impl Iterator<Item = char>) -> Tendril {
    let mut res = Tendril::new();
    to_camel_case_with(text, &mut res);
    res
}
pub fn to_camel_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
    let mut first = true;
    let mut at_word_start = false;

    for c in text {
        if !c.is_alphanumeric() {
            at_word_start = true;
            continue;
        }

        if first {
            buf.extend(c.to_lowercase());
            first = false;
        } else if at_word_start {
            at_word_start = false;
            buf.extend(c.to_uppercase());
        } else {
            buf.extend(c.to_lowercase());
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_camel_case_underscore() {
        let result = to_camel_case("otto_botto".chars());
        assert_eq!(result.as_ref() as &str, "ottoBotto");
    }

    #[test]
    fn test_camel_case_uppercase() {
        let result = to_camel_case("OTTO_BOTTO".chars());
        assert_eq!(result.as_ref() as &str, "ottoBotto");
    }

    #[test]
    fn test_camel_case_mixed_case() {
        let result = to_camel_case("OttO_boTTO".chars());
        assert_eq!(result.as_ref() as &str, "ottoBotto");
    }

    #[test]
    fn test_camel_case_includes_nums() {
        let result = to_camel_case("Ott0_b0TT0".chars());
        assert_eq!(result.as_ref() as &str, "ott0B0tt0");
    }

    #[test]
    fn test_camel_case_one_word_lower() {
        let result = to_camel_case("otto".chars());
        assert_eq!(result.as_ref() as &str, "otto");
    }

    #[test]
    fn test_camel_case_one_word_upper() {
        let result = to_camel_case("OTTO".chars());
        assert_eq!(result.as_ref() as &str, "otto");
    }

    #[test]
    fn test_camel_case_one_char_lower() {
        let result = to_camel_case("o".chars());
        assert_eq!(result.as_ref() as &str, "o");
    }

    #[test]
    fn test_camel_case_one_char_upper() {
        let result = to_camel_case("O".chars());
        assert_eq!(result.as_ref() as &str, "o");
    }

    #[test]
    fn test_camel_case_many_words_separators() {
        let result = to_camel_case("otto_botto_the_dog".chars());
        assert_eq!(result.as_ref() as &str, "ottoBottoTheDog");
    }

    #[test]
    fn test_camel_case_empty_string() {
        let result = to_camel_case("".chars());
        assert_eq!(result.as_ref() as &str, "");
    }
}