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