1 /// This trait defines a mixed case conversion. 2 /// 3 /// In mixedCase, word boundaries are indicated by capital letters, excepting 4 /// the first word. 5 /// 6 /// ## Example: 7 /// 8 /// ```rust 9 /// extern crate heck; 10 /// fn main() { 11 /// 12 /// use heck::MixedCase; 13 /// 14 /// let sentence = "It is we who built these palaces and cities."; 15 /// assert_eq!(sentence.to_mixed_case(), "itIsWeWhoBuiltThesePalacesAndCities"); 16 /// } 17 /// ``` 18 pub trait MixedCase: ToOwned { 19 /// Convert this type to mixed case. to_mixed_case(&self) -> Self::Owned20 fn to_mixed_case(&self) -> Self::Owned; 21 } 22 23 impl MixedCase for str { to_mixed_case(&self) -> String24 fn to_mixed_case(&self) -> String { 25 ::transform(self, |s, out| { 26 if out.is_empty() { ::lowercase(s, out); } 27 else { ::capitalize(s, out) } 28 }, |_| {}) 29 } 30 } 31 32 #[cfg(test)] 33 mod tests { 34 use super::MixedCase; 35 36 macro_rules! t { 37 ($t:ident : $s1:expr => $s2:expr) => { 38 #[test] 39 fn $t() { 40 assert_eq!($s1.to_mixed_case(), $s2) 41 } 42 } 43 } 44 45 t!(test1: "CamelCase" => "camelCase"); 46 t!(test2: "This is Human case." => "thisIsHumanCase"); 47 t!(test3: "MixedUP CamelCase, with some Spaces" => "mixedUpCamelCaseWithSomeSpaces"); 48 t!(test4: "mixed_up_ snake_case, with some _spaces" => "mixedUpSnakeCaseWithSomeSpaces"); 49 t!(test5: "kebab-case" => "kebabCase"); 50 t!(test6: "SHOUTY_SNAKE_CASE" => "shoutySnakeCase"); 51 t!(test7: "snake_case" => "snakeCase"); 52 t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "thisContainsAllKindsOfWordBoundaries"); 53 t!(test9: "XΣXΣ baffle" => "xσxςBaffle"); 54 t!(test10: "XMLHttpRequest" => "xmlHttpRequest"); 55 // TODO unicode tests 56 } 57