Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compactlowercase and COMPACTUPPERCASE casing rules #50

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Add `no_std` support.
- Remove non-additive `unicode` feature. The library now uses `char::is_alphanumeric`
instead of the `uncode-segmentation` library to determine word boundaries in all cases.
- Add `compactlowercase` and `COMPACTUPPERCASE` casing rules.

# 0.4.1

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ indicators are dropped, except insofar as CamelCase capitalizes the first word.
6. Title Case
7. SHOUTY-KEBAB-CASE
8. Train-Case
9. compactlowercase
10. COMPACTUPPERCASE

## MSRV

Expand Down
87 changes: 87 additions & 0 deletions src/compact_lower.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use alloc::{
borrow::ToOwned,
fmt,
string::{String, ToString},
};

use crate::{lowercase, transform};

/// This trait defines a compact lower case conversion.
///
/// In compactlowercase, word boundaries are omitted.
///
/// ## Example:
///
/// ```rust
/// use heck::ToCompactLowercase;
///
/// let sentence = "We carry a new world here, in our hearts.";
/// assert_eq!(sentence.to_compact_lowercase(), "wecarryanewworldhereinourhearts");
/// ```
pub trait ToCompactLowercase: ToOwned {
/// Convert this type to compact lowercase.
fn to_compact_lowercase(&self) -> Self::Owned;
}

impl ToCompactLowercase for str {
fn to_compact_lowercase(&self) -> String {
AsCompactLowercase(self).to_string()
}
}

/// This wrapper performs a compact lowercase conversion in [`fmt::Display`].
///
/// ## Example:
///
/// ```
/// use heck::AsCompactLowercase;
///
/// let sentence = "We carry a new world here, in our hearts.";
/// assert_eq!(format!("{}", AsCompactLowercase(sentence)), "wecarryanewworldhereinourhearts");
/// ```
pub struct AsCompactLowercase<T: AsRef<str>>(pub T);

impl<T: AsRef<str>> fmt::Display for AsCompactLowercase<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
transform(self.0.as_ref(), lowercase, |_f| Ok(()), f)
}
}

#[cfg(test)]
mod tests {
use super::ToCompactLowercase;

macro_rules! t {
($t:ident : $s1:expr => $s2:expr) => {
#[test]
fn $t() {
assert_eq!($s1.to_compact_lowercase(), $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");
t!(test11: "FIELD_NAME11" => "fieldname11");
t!(test12: "99BOTTLES" => "99bottles");
t!(test13: "FieldNamE11" => "fieldname11");
t!(test14: "abc123def456" => "abc123def456");
t!(test16: "abc123DEF456" => "abc123def456");
t!(test17: "abc123Def456" => "abc123def456");
t!(test18: "abc123DEf456" => "abc123def456");
t!(test19: "ABC123def456" => "abc123def456");
t!(test20: "ABC123DEF456" => "abc123def456");
t!(test21: "ABC123Def456" => "abc123def456");
t!(test22: "ABC123DEf456" => "abc123def456");
t!(test23: "ABC123dEEf456FOO" => "abc123deef456foo");
t!(test24: "abcDEF" => "abcdef");
t!(test25: "ABcDE" => "abcde");
}
87 changes: 87 additions & 0 deletions src/compact_upper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use alloc::{
borrow::ToOwned,
fmt,
string::{String, ToString},
};

use crate::{transform, uppercase};

/// This trait defines a compact upper case conversion.
///
/// In COMPACTUPPERCASE, word boundaries are omitted.
///
/// ## Example:
///
/// ```rust
/// use heck::ToCompactUppercase;
///
/// let sentence = "We carry a new world here, in our hearts.";
/// assert_eq!(sentence.to_compact_uppercase(), "WECARRYANEWWORLDHEREINOURHEARTS");
/// ```
pub trait ToCompactUppercase: ToOwned {
/// Convert this type to compact lowercase.
fn to_compact_uppercase(&self) -> Self::Owned;
}

impl ToCompactUppercase for str {
fn to_compact_uppercase(&self) -> String {
AsCompactUppercase(self).to_string()
}
}

/// This wrapper performs a compact uppercase conversion in [`fmt::Display`].
///
/// ## Example:
///
/// ```
/// use heck::AsCompactUppercase;
///
/// let sentence = "We carry a new world here, in our hearts.";
/// assert_eq!(format!("{}", AsCompactUppercase(sentence)), "WECARRYANEWWORLDHEREINOURHEARTS");
/// ```
pub struct AsCompactUppercase<T: AsRef<str>>(pub T);

impl<T: AsRef<str>> fmt::Display for AsCompactUppercase<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
transform(self.0.as_ref(), uppercase, |_f| Ok(()), f)
}
}

#[cfg(test)]
mod tests {
use super::ToCompactUppercase;

macro_rules! t {
($t:ident : $s1:expr => $s2:expr) => {
#[test]
fn $t() {
assert_eq!($s1.to_compact_uppercase(), $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");
t!(test11: "FIELD_NAME11" => "FIELDNAME11");
t!(test12: "99BOTTLES" => "99BOTTLES");
t!(test13: "FieldNamE11" => "FIELDNAME11");
t!(test14: "abc123def456" => "ABC123DEF456");
t!(test16: "abc123DEF456" => "ABC123DEF456");
t!(test17: "abc123Def456" => "ABC123DEF456");
t!(test18: "abc123DEf456" => "ABC123DEF456");
t!(test19: "ABC123def456" => "ABC123DEF456");
t!(test20: "ABC123DEF456" => "ABC123DEF456");
t!(test21: "ABC123Def456" => "ABC123DEF456");
t!(test22: "ABC123DEf456" => "ABC123DEF456");
t!(test23: "ABC123dEEf456FOO" => "ABC123DEEF456FOO");
t!(test24: "abcDEF" => "ABCDEF");
t!(test25: "ABcDE" => "ABCDE");
}
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@
//! 6. Title Case
//! 7. SHOUTY-KEBAB-CASE
//! 8. Train-Case
//! 9. compactlowercase
//! 10. COMPACTUPPERCASE
#![deny(missing_docs)]
#![forbid(unsafe_code)]
#![no_std]

extern crate alloc;

mod compact_lower;
mod compact_upper;
mod kebab;
mod lower_camel;
mod shouty_kebab;
Expand All @@ -51,6 +55,8 @@ mod title;
mod train;
mod upper_camel;

pub use compact_lower::{AsCompactLowercase, ToCompactLowercase};
pub use compact_upper::{AsCompactUppercase, ToCompactUppercase};
pub use kebab::{AsKebabCase, ToKebabCase};
pub use lower_camel::{AsLowerCamelCase, ToLowerCamelCase};
pub use shouty_kebab::{AsShoutyKebabCase, ToShoutyKebabCase};
Expand Down