2024-02-23 04:47:55 +08:00
|
|
|
use crate::Tendril;
|
|
|
|
|
|
|
|
// todo: should this be grapheme aware?
|
|
|
|
|
2024-12-20 19:32:14 +08:00
|
|
|
pub fn simple_case_conversion(
|
2024-12-20 19:25:27 +08:00
|
|
|
text: impl Iterator<Item = char>,
|
|
|
|
buf: &mut Tendril,
|
|
|
|
transform_char: impl Fn(&char) -> char,
|
|
|
|
) {
|
2024-12-20 15:44:22 +08:00
|
|
|
for c in text {
|
2024-12-20 19:25:27 +08:00
|
|
|
buf.push(transform_char(&c))
|
2024-12-20 15:44:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-20 19:32:14 +08:00
|
|
|
pub fn complex_case_conversion(
|
2024-12-20 18:43:31 +08:00
|
|
|
text: impl Iterator<Item = char>,
|
|
|
|
buf: &mut Tendril,
|
2024-12-20 18:52:13 +08:00
|
|
|
capitalize_first: bool,
|
2024-12-20 19:25:27 +08:00
|
|
|
capitalize_rest: bool,
|
2024-12-20 18:52:13 +08:00
|
|
|
separator: Option<char>,
|
2024-12-20 18:43:31 +08:00
|
|
|
) {
|
2024-12-20 18:52:13 +08:00
|
|
|
let mut capitalize_next = capitalize_first;
|
|
|
|
let mut prev: Option<char> = None;
|
2024-12-20 18:43:31 +08:00
|
|
|
|
|
|
|
for c in text.skip_while(|ch| ch.is_whitespace()) {
|
|
|
|
if c.is_alphanumeric() {
|
2024-12-20 18:52:13 +08:00
|
|
|
if prev.is_some_and(|p| p.is_lowercase()) && c.is_uppercase() {
|
2024-12-20 18:43:31 +08:00
|
|
|
capitalize_next = true;
|
|
|
|
}
|
2024-12-20 19:25:27 +08:00
|
|
|
if capitalize_next && capitalize_rest {
|
|
|
|
buf.push(if capitalize_rest {
|
|
|
|
c.to_ascii_uppercase()
|
|
|
|
} else {
|
|
|
|
c
|
|
|
|
});
|
2024-12-20 18:43:31 +08:00
|
|
|
capitalize_next = false;
|
|
|
|
} else {
|
|
|
|
buf.extend(c.to_lowercase());
|
|
|
|
}
|
2024-12-20 15:55:43 +08:00
|
|
|
} else {
|
2024-12-20 17:00:56 +08:00
|
|
|
capitalize_next = true;
|
2024-12-20 18:52:13 +08:00
|
|
|
if let Some(separator) = separator {
|
|
|
|
if prev.is_some_and(|p| p != separator) {
|
|
|
|
buf.push(separator);
|
|
|
|
}
|
2024-12-20 18:41:02 +08:00
|
|
|
}
|
2024-12-20 15:55:43 +08:00
|
|
|
}
|
2024-12-20 18:41:02 +08:00
|
|
|
prev = Some(c);
|
2024-12-20 15:55:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-20 19:32:14 +08:00
|
|
|
pub fn separator_case_conversion(
|
2024-12-20 17:00:56 +08:00
|
|
|
text: impl Iterator<Item = char>,
|
|
|
|
buf: &mut Tendril,
|
|
|
|
separator: char,
|
|
|
|
) {
|
2024-12-20 19:25:27 +08:00
|
|
|
let mut prev: Option<char> = None;
|
2024-12-20 17:00:56 +08:00
|
|
|
|
2024-12-20 18:33:59 +08:00
|
|
|
for c in text.skip_while(|ch| ch.is_whitespace()) {
|
2024-12-20 17:00:56 +08:00
|
|
|
if c.is_alphanumeric() {
|
2024-12-20 19:25:27 +08:00
|
|
|
if prev.is_some_and(|p| p.is_lowercase()) && c.is_uppercase()
|
|
|
|
|| !prev.is_some_and(|p| p.is_alphanumeric()) && !buf.is_empty()
|
|
|
|
{
|
2024-12-20 17:00:56 +08:00
|
|
|
buf.push(separator);
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.push(c.to_ascii_lowercase());
|
2024-12-20 15:55:43 +08:00
|
|
|
}
|
2024-12-20 19:25:27 +08:00
|
|
|
prev = Some(c);
|
2024-12-20 15:55:43 +08:00
|
|
|
}
|
2024-12-20 19:25:27 +08:00
|
|
|
}
|
2024-12-20 15:55:43 +08:00
|
|
|
|
2024-12-20 19:32:14 +08:00
|
|
|
pub fn into_alternate_case(text: impl Iterator<Item = char>, buf: &mut Tendril) {
|
|
|
|
simple_case_conversion(text, buf, |c| {
|
2024-12-20 19:25:27 +08:00
|
|
|
if c.is_uppercase() {
|
|
|
|
c.to_ascii_lowercase()
|
|
|
|
} else if c.is_lowercase() {
|
|
|
|
c.to_ascii_uppercase()
|
|
|
|
} else {
|
|
|
|
*c
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-12-20 19:32:14 +08:00
|
|
|
pub fn into_upper_case(text: impl Iterator<Item = char>, buf: &mut Tendril) {
|
|
|
|
simple_case_conversion(text, buf, char::to_ascii_uppercase);
|
2024-12-20 19:25:27 +08:00
|
|
|
}
|
|
|
|
|
2024-12-20 19:32:14 +08:00
|
|
|
pub fn into_lower_case(text: impl Iterator<Item = char>, buf: &mut Tendril) {
|
|
|
|
simple_case_conversion(text, buf, char::to_ascii_lowercase);
|
2024-12-20 15:47:13 +08:00
|
|
|
}
|
|
|
|
|
2024-12-20 19:32:14 +08:00
|
|
|
pub fn into_kebab_case(text: impl Iterator<Item = char>, buf: &mut Tendril) {
|
|
|
|
separator_case_conversion(text, buf, '-');
|
2024-12-20 17:00:56 +08:00
|
|
|
}
|
|
|
|
|
2024-12-20 19:32:14 +08:00
|
|
|
pub fn into_snake_case(text: impl Iterator<Item = char>, buf: &mut Tendril) {
|
|
|
|
separator_case_conversion(text, buf, '_');
|
2024-12-20 17:00:56 +08:00
|
|
|
}
|
|
|
|
|
2024-12-20 19:32:14 +08:00
|
|
|
pub fn into_title_case(text: impl Iterator<Item = char>, buf: &mut Tendril) {
|
|
|
|
complex_case_conversion(text, buf, true, true, Some(' '));
|
2024-12-20 18:52:13 +08:00
|
|
|
}
|
|
|
|
|
2024-12-20 19:32:14 +08:00
|
|
|
pub fn into_camel_case(text: impl Iterator<Item = char>, buf: &mut Tendril) {
|
|
|
|
complex_case_conversion(text, buf, false, true, None);
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
2024-12-20 19:32:14 +08:00
|
|
|
pub fn into_pascal_case(text: impl Iterator<Item = char>, buf: &mut Tendril) {
|
|
|
|
complex_case_conversion(text, buf, true, true, None);
|
2024-12-20 19:25:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn to_case<I>(text: I, to_case_with: fn(I, &mut Tendril)) -> Tendril
|
|
|
|
where
|
|
|
|
I: Iterator<Item = char>,
|
|
|
|
{
|
|
|
|
let mut res = Tendril::new();
|
|
|
|
to_case_with(text, &mut res);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_camel_case(text: impl Iterator<Item = char>) -> Tendril {
|
2024-12-20 19:32:14 +08:00
|
|
|
to_case(text, into_camel_case)
|
2024-12-20 19:25:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_lower_case(text: impl Iterator<Item = char>) -> Tendril {
|
2024-12-20 19:32:14 +08:00
|
|
|
to_case(text, into_lower_case)
|
2024-12-20 19:25:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_upper_case(text: impl Iterator<Item = char>) -> Tendril {
|
2024-12-20 19:32:14 +08:00
|
|
|
to_case(text, into_upper_case)
|
2024-12-20 19:25:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_pascal_case(text: impl Iterator<Item = char>) -> Tendril {
|
2024-12-20 19:32:14 +08:00
|
|
|
to_case(text, into_pascal_case)
|
2024-12-20 19:25:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_alternate_case(text: impl Iterator<Item = char>) -> Tendril {
|
2024-12-20 19:32:14 +08:00
|
|
|
to_case(text, into_alternate_case)
|
2024-12-20 19:25:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_title_case(text: impl Iterator<Item = char>) -> Tendril {
|
2024-12-20 19:32:14 +08:00
|
|
|
to_case(text, into_title_case)
|
2024-12-20 19:25:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_kebab_case(text: impl Iterator<Item = char>) -> Tendril {
|
2024-12-20 19:32:14 +08:00
|
|
|
to_case(text, into_kebab_case)
|
2024-12-20 19:25:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_snake_case(text: impl Iterator<Item = char>) -> Tendril {
|
2024-12-20 19:32:14 +08:00
|
|
|
to_case(text, into_snake_case)
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
fn case_tester<'a, F>(change_fn: F) -> impl Fn(&'a str, &'a str) + 'a
|
|
|
|
where
|
|
|
|
F: Fn(std::str::Chars<'a>) -> Tendril + 'a,
|
|
|
|
{
|
|
|
|
move |input: &str, expected: &str| {
|
|
|
|
let transformed = change_fn(input.chars());
|
|
|
|
let m = transformed.to_string();
|
|
|
|
dbg!(input);
|
|
|
|
assert_eq!(m.as_str(), expected)
|
2024-02-23 04:47:55 +08:00
|
|
|
}
|
|
|
|
}
|
2024-12-20 17:38:48 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_camel_case_conversion() {
|
|
|
|
let camel_test = case_tester(to_camel_case);
|
|
|
|
camel_test("hello world", "helloWorld");
|
|
|
|
camel_test("Hello World", "helloWorld");
|
|
|
|
camel_test("hello_world", "helloWorld");
|
|
|
|
camel_test("HELLO_WORLD", "helloWorld");
|
2024-12-20 17:50:35 +08:00
|
|
|
camel_test("hello-world", "helloWorld");
|
|
|
|
camel_test("hello world", "helloWorld");
|
2024-12-20 18:33:59 +08:00
|
|
|
camel_test(" hello world", "helloWorld");
|
2024-12-20 17:50:35 +08:00
|
|
|
camel_test("hello\tworld", "helloWorld");
|
|
|
|
camel_test("HELLO WORLD", "helloWorld");
|
|
|
|
camel_test("HELLO-world", "helloWorld");
|
|
|
|
camel_test("hello WORLD ", "helloWorld");
|
2024-12-20 18:33:59 +08:00
|
|
|
camel_test("helloWorld", "helloWorld");
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_lower_case_conversion() {
|
|
|
|
let lower_test = case_tester(to_lower_case);
|
|
|
|
lower_test("HelloWorld", "helloworld");
|
|
|
|
lower_test("HELLO WORLD", "hello world");
|
|
|
|
lower_test("hello_world", "hello_world");
|
|
|
|
lower_test("Hello-World", "hello-world");
|
2024-12-20 17:50:35 +08:00
|
|
|
lower_test("Hello", "hello");
|
|
|
|
lower_test("WORLD", "world");
|
|
|
|
lower_test("hello world", "hello world");
|
|
|
|
lower_test("HELLOworld", "helloworld");
|
|
|
|
lower_test("hello-world", "hello-world");
|
|
|
|
lower_test("hello_world_here", "hello_world_here");
|
|
|
|
lower_test("HELLO_world", "hello_world");
|
|
|
|
lower_test("MixEdCaseString", "mixedcasestring");
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_upper_case_conversion() {
|
|
|
|
let upper_test = case_tester(to_upper_case);
|
|
|
|
upper_test("helloWorld", "HELLOWORLD");
|
|
|
|
upper_test("hello world", "HELLO WORLD");
|
|
|
|
upper_test("hello_world", "HELLO_WORLD");
|
|
|
|
upper_test("Hello-World", "HELLO-WORLD");
|
2024-12-20 17:50:35 +08:00
|
|
|
upper_test("Hello", "HELLO");
|
|
|
|
upper_test("world", "WORLD");
|
|
|
|
upper_test("hello world", "HELLO WORLD");
|
|
|
|
upper_test("helloworld", "HELLOWORLD");
|
|
|
|
upper_test("hello-world", "HELLO-WORLD");
|
|
|
|
upper_test("hello_world_here", "HELLO_WORLD_HERE");
|
|
|
|
upper_test("hello_WORLD", "HELLO_WORLD");
|
|
|
|
upper_test("mixedCaseString", "MIXEDCASESTRING");
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pascal_case_conversion() {
|
|
|
|
let pascal_test = case_tester(to_pascal_case);
|
|
|
|
pascal_test("hello world", "HelloWorld");
|
|
|
|
pascal_test("Hello World", "HelloWorld");
|
|
|
|
pascal_test("hello_world", "HelloWorld");
|
|
|
|
pascal_test("HELLO_WORLD", "HelloWorld");
|
2024-12-20 17:50:35 +08:00
|
|
|
pascal_test("hello-world", "HelloWorld");
|
|
|
|
pascal_test("hello world", "HelloWorld");
|
|
|
|
pascal_test(" hello world", "HelloWorld");
|
|
|
|
pascal_test("hello\tworld", "HelloWorld");
|
|
|
|
pascal_test("HELLO WORLD", "HelloWorld");
|
|
|
|
pascal_test("HELLO-world", "HelloWorld");
|
|
|
|
pascal_test("hello WORLD ", "HelloWorld");
|
2024-12-20 18:31:09 +08:00
|
|
|
pascal_test("helloWorld", "HelloWorld");
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_alternate_case_conversion() {
|
|
|
|
let alternate_test = case_tester(to_alternate_case);
|
|
|
|
alternate_test("hello world", "HELLO WORLD");
|
|
|
|
alternate_test("Hello World", "hELLO wORLD");
|
|
|
|
alternate_test("helLo_woRlD", "HELlO_WOrLd");
|
2024-12-20 17:50:35 +08:00
|
|
|
alternate_test("HELLO_world", "hello_WORLD");
|
|
|
|
alternate_test("hello-world", "HELLO-WORLD");
|
|
|
|
alternate_test("Hello-world", "hELLO-WORLD");
|
|
|
|
alternate_test("hello", "HELLO");
|
|
|
|
alternate_test("HELLO", "hello");
|
|
|
|
alternate_test("hello123", "HELLO123");
|
|
|
|
alternate_test("hello WORLD", "HELLO world");
|
|
|
|
alternate_test("HELLO123 world", "hello123 WORLD");
|
|
|
|
alternate_test("world hello", "WORLD HELLO");
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_title_case_conversion() {
|
|
|
|
let title_test = case_tester(to_title_case);
|
|
|
|
title_test("hello world", "Hello World");
|
|
|
|
title_test("Hello World", "Hello World");
|
|
|
|
title_test("hello_world", "Hello World");
|
|
|
|
title_test("HELLO_WORLD", "Hello World");
|
2024-12-20 17:50:35 +08:00
|
|
|
title_test("hello-world", "Hello World");
|
2024-12-20 18:41:02 +08:00
|
|
|
|
|
|
|
title_test("hello world", "Hello World");
|
|
|
|
|
2024-12-20 17:50:35 +08:00
|
|
|
title_test(" hello world", "Hello World");
|
|
|
|
title_test("hello\tworld", "Hello World");
|
|
|
|
// title_test("HELLO WORLD", "Hello World");
|
|
|
|
title_test("HELLO-world", "Hello World");
|
|
|
|
// title_test("hello WORLD ", "Hello World");
|
|
|
|
// title_test("helloWorld", "Hello World");
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_kebab_case_conversion() {
|
|
|
|
let kebab_test = case_tester(to_kebab_case);
|
|
|
|
kebab_test("helloWorld", "hello-world");
|
|
|
|
kebab_test("HelloWorld", "hello-world");
|
|
|
|
kebab_test("hello_world", "hello-world");
|
|
|
|
kebab_test("HELLO_WORLD", "hello-world");
|
2024-12-20 17:50:35 +08:00
|
|
|
kebab_test("hello-world", "hello-world");
|
|
|
|
kebab_test("hello world", "hello-world");
|
|
|
|
kebab_test("hello\tworld", "hello-world");
|
|
|
|
kebab_test("HELLO WORLD", "hello-world");
|
|
|
|
kebab_test("HELLO-world", "hello-world");
|
|
|
|
kebab_test("hello WORLD ", "hello-world");
|
|
|
|
kebab_test("helloWorld", "hello-world");
|
|
|
|
kebab_test("HelloWorld123", "hello-world123");
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_snake_case_conversion() {
|
|
|
|
let snake_test = case_tester(to_snake_case);
|
|
|
|
snake_test("helloWorld", "hello_world");
|
|
|
|
snake_test("HelloWorld", "hello_world");
|
|
|
|
snake_test("hello world", "hello_world");
|
|
|
|
snake_test("HELLO WORLD", "hello_world");
|
2024-12-20 17:50:35 +08:00
|
|
|
snake_test("hello-world", "hello_world");
|
|
|
|
snake_test("hello world", "hello_world");
|
|
|
|
snake_test("hello\tworld", "hello_world");
|
|
|
|
snake_test("HELLO WORLD", "hello_world");
|
|
|
|
snake_test("HELLO-world", "hello_world");
|
|
|
|
snake_test("hello WORLD ", "hello_world");
|
|
|
|
snake_test("helloWorld", "hello_world");
|
2024-12-20 18:27:40 +08:00
|
|
|
snake_test("helloWORLD123", "hello_world123");
|
2024-12-20 17:38:48 +08:00
|
|
|
}
|
2024-02-23 04:47:55 +08:00
|
|
|
}
|