diff options
author | Matthew Maurer <mmaurer@google.com> | 2020-06-02 11:15:06 -0700 |
---|---|---|
committer | Matthew Maurer <mmaurer@google.com> | 2020-06-02 11:15:06 -0700 |
commit | 6967d67d623b869bfedf119d9cc79ea862d2175e (patch) | |
tree | 7ed27a047d3040f8d630e14a60ff1f094b758751 /src/camel.rs | |
parent | b02442cdf2a77b26212ccfca87278650c6240e82 (diff) | |
download | heck-6967d67d623b869bfedf119d9cc79ea862d2175e.tar.gz |
Import heck-0.3.1
Change-Id: I8c4cd8087a2addff7263aa57b1824e5f9007f300
Diffstat (limited to 'src/camel.rs')
-rw-r--r-- | src/camel.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/camel.rs b/src/camel.rs new file mode 100644 index 0000000..74bd741 --- /dev/null +++ b/src/camel.rs @@ -0,0 +1,52 @@ +/// This trait defines a camel case conversion. +/// +/// In CamelCase, word boundaries are indicated by capital letters, including +/// the first word. +/// +/// ## Example: +/// +/// ```rust +/// extern crate heck; +/// fn main() { +/// +/// use heck::CamelCase; +/// +/// let sentence = "We are not in the least afraid of ruins."; +/// assert_eq!(sentence.to_camel_case(), "WeAreNotInTheLeastAfraidOfRuins"); +/// } +/// ``` +pub trait CamelCase: ToOwned { + /// Convert this type to camel case. + fn to_camel_case(&self) -> Self::Owned; +} + +impl CamelCase for str { + fn to_camel_case(&self) -> String { + ::transform(self, ::capitalize, |_| {}) + } +} + +#[cfg(test)] +mod tests { + use super::CamelCase; + + macro_rules! t { + ($t:ident : $s1:expr => $s2:expr) => { + #[test] + fn $t() { + assert_eq!($s1.to_camel_case(), $s2) + } + } + } + + t!(test1: "CamelCase" => "CamelCase"); + t!(test2: "This is Human case." => "ThisIsHumanCase"); + t!(test3: "MixedUP_CamelCase, with some Spaces" => "MixedUpCamelCaseWithSomeSpaces"); + t!(test4: "mixed_up_ snake_case, with some _spaces" => "MixedUpSnakeCaseWithSomeSpaces"); + t!(test5: "kebab-case" => "KebabCase"); + t!(test6: "SHOUTY_SNAKE_CASE" => "ShoutySnakeCase"); + t!(test7: "snake_case" => "SnakeCase"); + t!(test8: "this-contains_ ALLKinds OfWord_Boundaries" => "ThisContainsAllKindsOfWordBoundaries"); + t!(test9: "XΣXΣ baffle" => "XσxςBaffle"); + t!(test10: "XMLHttpRequest" => "XmlHttpRequest"); +} |