Skip to content

Commit

Permalink
feat: Implement sentence casing
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Jul 8, 2024
1 parent 77651ad commit 2cbb456
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/bin/decasify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ fn process<I: IntoIterator<Item = String>>(
let output = to_uppercase(&string, locale.clone());
println!("{output}")
}
_ => eprintln!("Target case {case:?} not implemented!"),
TargetCase::Sentence => {
let output = to_sentence(&string, locale.clone());
println!("{output}")
}
}
}
}
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ pub fn to_uppercase(string: &str, locale: InputLocale) -> String {
}
}

/// Convert a string to sentence case following typesetting conventions for a target locale
pub fn to_sentence(string: &str, locale: InputLocale) -> String {
let words: Vec<&str> = string.split_whitespace().collect();
match locale {
InputLocale::EN => to_sentence_en(words),
InputLocale::TR => to_sentence_tr(words),
}
}

fn to_titlecase_en(words: Vec<&str>, style: Option<StyleGuide>) -> String {
match style {
Some(StyleGuide::AssociatedPress) => to_titlecase_ap(words),
Expand Down Expand Up @@ -156,6 +165,22 @@ fn to_uppercase_tr(words: Vec<&str>) -> String {
output.join(" ")
}

fn to_sentence_en(words: Vec<&str>) -> String {
let mut output: Vec<String> = Vec::new();
for word in words {
output.push(word.to_uppercase());
}
output.join(" ")
}

fn to_sentence_tr(words: Vec<&str>) -> String {
let mut output: Vec<String> = Vec::new();
for word in words {
output.push(word.to_uppercase_tr_az());
}
output.join(" ")
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 2cbb456

Please sign in to comment.