refactor: title camel and pascal into a single function

pull/12043/head
Nikita Revenco 2024-12-20 10:52:13 +00:00
parent 3414183ab6
commit 79d3e10c5c
1 changed files with 16 additions and 32 deletions

View File

@ -71,17 +71,18 @@ pub fn to_alternate_case_with(text: impl Iterator<Item = char>, buf: &mut Tendri
} }
} }
pub fn to_camel_or_pascal_case_with( pub fn to_camel_or_pascal_or_title_case_with(
text: impl Iterator<Item = char>, text: impl Iterator<Item = char>,
buf: &mut Tendril, buf: &mut Tendril,
is_pascal: bool, capitalize_first: bool,
separator: Option<char>,
) { ) {
let mut capitalize_next = is_pascal; let mut capitalize_next = capitalize_first;
let mut prev_is_lowercase = false; let mut prev: Option<char> = None;
for c in text.skip_while(|ch| ch.is_whitespace()) { for c in text.skip_while(|ch| ch.is_whitespace()) {
if c.is_alphanumeric() { if c.is_alphanumeric() {
if prev_is_lowercase && c.is_uppercase() { if prev.is_some_and(|p| p.is_lowercase()) && c.is_uppercase() {
capitalize_next = true; capitalize_next = true;
} }
if capitalize_next { if capitalize_next {
@ -90,37 +91,16 @@ pub fn to_camel_or_pascal_case_with(
} else { } else {
buf.extend(c.to_lowercase()); buf.extend(c.to_lowercase());
} }
prev_is_lowercase = c.is_lowercase();
} else { } else {
capitalize_next = true; capitalize_next = true;
prev_is_lowercase = false; if let Some(separator) = separator {
} if prev.is_some_and(|p| p != separator) {
} buf.push(separator);
} }
pub fn to_title_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
let mut capitalize_next = true;
let mut prev: Option<char> = None;
for c in text.skip_while(|ch| ch.is_whitespace()) {
if c.is_alphanumeric() {
if capitalize_next || (prev.is_some_and(|p| p.is_lowercase()) && c.is_uppercase()) {
buf.extend(c.to_uppercase());
capitalize_next = false;
} else {
buf.extend(c.to_lowercase());
}
} else {
capitalize_next = true;
// only if the previous char is not already space
if prev.is_some_and(|p| p != ' ') {
buf.push(' ');
} }
} }
prev = Some(c); prev = Some(c);
} }
*buf = buf.trim_end().into();
} }
pub fn to_case_with_separator( pub fn to_case_with_separator(
@ -159,12 +139,16 @@ pub fn to_snake_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
to_case_with_separator(text, buf, '_'); to_case_with_separator(text, buf, '_');
} }
pub fn to_title_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
to_camel_or_pascal_or_title_case_with(text, buf, true, Some(' '));
}
pub fn to_camel_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) { pub fn to_camel_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
to_camel_or_pascal_case_with(text, buf, false); to_camel_or_pascal_or_title_case_with(text, buf, false, None);
} }
pub fn to_pascal_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) { pub fn to_pascal_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
to_camel_or_pascal_case_with(text, buf, true); to_camel_or_pascal_or_title_case_with(text, buf, true, None);
} }
#[cfg(test)] #[cfg(test)]