• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use core::fmt;
2 
3 use alloc::{borrow::ToOwned, string::ToString};
4 
5 use crate::{lowercase, transform};
6 
7 /// This trait defines a kebab case conversion.
8 ///
9 /// In kebab-case, word boundaries are indicated by hyphens.
10 ///
11 /// ## Example:
12 ///
13 /// ```rust
14 /// use heck::ToKebabCase;
15 ///
16 /// let sentence = "We are going to inherit the earth.";
17 /// assert_eq!(sentence.to_kebab_case(), "we-are-going-to-inherit-the-earth");
18 /// ```
19 pub trait ToKebabCase: ToOwned {
20     /// Convert this type to kebab case.
to_kebab_case(&self) -> Self::Owned21     fn to_kebab_case(&self) -> Self::Owned;
22 }
23 
24 impl ToKebabCase for str {
to_kebab_case(&self) -> Self::Owned25     fn to_kebab_case(&self) -> Self::Owned {
26         AsKebabCase(self).to_string()
27     }
28 }
29 
30 /// This wrapper performs a kebab case conversion in [`fmt::Display`].
31 ///
32 /// ## Example:
33 ///
34 /// ```
35 /// use heck::AsKebabCase;
36 ///
37 /// let sentence = "We are going to inherit the earth.";
38 /// assert_eq!(format!("{}", AsKebabCase(sentence)), "we-are-going-to-inherit-the-earth");
39 /// ```
40 pub struct AsKebabCase<T: AsRef<str>>(pub T);
41 
42 impl<T: AsRef<str>> fmt::Display for AsKebabCase<T> {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result43     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44         transform(self.0.as_ref(), lowercase, |f| write!(f, "-"), f)
45     }
46 }
47 
48 #[cfg(test)]
49 mod tests {
50     use super::ToKebabCase;
51 
52     macro_rules! t {
53         ($t:ident : $s1:expr => $s2:expr) => {
54             #[test]
55             fn $t() {
56                 assert_eq!($s1.to_kebab_case(), $s2)
57             }
58         };
59     }
60 
61     t!(test1: "CamelCase" => "camel-case");
62     t!(test2: "This is Human case." => "this-is-human-case");
63     t!(test3: "MixedUP CamelCase, with some Spaces" => "mixed-up-camel-case-with-some-spaces");
64     t!(test4: "mixed_up_ snake_case with some _spaces" => "mixed-up-snake-case-with-some-spaces");
65     t!(test5: "kebab-case" => "kebab-case");
66     t!(test6: "SHOUTY_SNAKE_CASE" => "shouty-snake-case");
67     t!(test7: "snake_case" => "snake-case");
68     t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "this-contains-all-kinds-of-word-boundaries");
69     t!(test9: "XΣXΣ baffle" => "xσxς-baffle");
70     t!(test10: "XMLHttpRequest" => "xml-http-request");
71     t!(test11: "لِنَذْهَبْ إِلَى السِّيْنَمَا" => "لِنَذْهَبْ-إِلَى-السِّيْنَمَا");
72     // Japanese and Chinese do not have word separation.
73     t!(test12: "ファイルを読み込み" => "ファイルを読み込み");
74     t!(test13: "祝你一天过得愉快" => "祝你一天过得愉快");
75 }
76