1 /// This trait defines a kebab case conversion. 2 /// 3 /// In kebab-case, word boundaries are indicated by hyphens. 4 /// 5 /// ## Example: 6 /// 7 /// ```rust 8 /// extern crate heck; 9 /// fn main() { 10 /// 11 /// use heck::KebabCase; 12 /// 13 /// let sentence = "We are going to inherit the earth."; 14 /// assert_eq!(sentence.to_kebab_case(), "we-are-going-to-inherit-the-earth"); 15 /// } 16 /// ``` 17 pub trait KebabCase: ToOwned { 18 /// Convert this type to kebab case. to_kebab_case(&self) -> Self::Owned19 fn to_kebab_case(&self) -> Self::Owned; 20 } 21 22 impl KebabCase for str { to_kebab_case(&self) -> Self::Owned23 fn to_kebab_case(&self) -> Self::Owned { 24 ::transform(self, ::lowercase, |s| s.push('-')) 25 } 26 } 27 28 #[cfg(test)] 29 mod tests { 30 use super::KebabCase; 31 32 macro_rules! t { 33 ($t:ident : $s1:expr => $s2:expr) => { 34 #[test] 35 fn $t() { 36 assert_eq!($s1.to_kebab_case(), $s2) 37 } 38 } 39 } 40 41 t!(test1: "CamelCase" => "camel-case"); 42 t!(test2: "This is Human case." => "this-is-human-case"); 43 t!(test3: "MixedUP CamelCase, with some Spaces" => "mixed-up-camel-case-with-some-spaces"); 44 t!(test4: "mixed_up_ snake_case with some _spaces" => "mixed-up-snake-case-with-some-spaces"); 45 t!(test5: "kebab-case" => "kebab-case"); 46 t!(test6: "SHOUTY_SNAKE_CASE" => "shouty-snake-case"); 47 t!(test7: "snake_case" => "snake-case"); 48 t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "this-contains-all-kinds-of-word-boundaries"); 49 t!(test9: "XΣXΣ baffle" => "xσxς-baffle"); 50 t!(test10: "XMLHttpRequest" => "xml-http-request"); 51 } 52