mirror of https://github.com/helix-editor/helix
feat: correct implementation for pascal_case and camel_case
parent
ff8c1df71d
commit
75cd953c30
|
@ -71,23 +71,6 @@ pub fn to_alternate_case_with(text: impl Iterator<Item = char>, buf: &mut Tendri
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_pascal_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
|
|
||||||
let mut at_word_start = true;
|
|
||||||
for c in text {
|
|
||||||
// we don't count _ as a word char here so case conversions work well
|
|
||||||
if !c.is_alphanumeric() {
|
|
||||||
at_word_start = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if at_word_start {
|
|
||||||
at_word_start = false;
|
|
||||||
buf.extend(c.to_uppercase());
|
|
||||||
} else {
|
|
||||||
buf.push(c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn to_title_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
|
pub fn to_title_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
|
||||||
let mut capitalize_next = true;
|
let mut capitalize_next = true;
|
||||||
let mut prev_is_lowercase = false;
|
let mut prev_is_lowercase = false;
|
||||||
|
@ -150,24 +133,119 @@ 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_camel_case_with(mut text: impl Iterator<Item = char>, buf: &mut Tendril) {
|
pub fn to_camel_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
|
||||||
for c in &mut text {
|
to_camel_or_pascal_case_with(text, buf, false);
|
||||||
if c.is_alphanumeric() {
|
}
|
||||||
buf.extend(c.to_lowercase())
|
|
||||||
}
|
pub fn to_pascal_case_with(text: impl Iterator<Item = char>, buf: &mut Tendril) {
|
||||||
}
|
to_camel_or_pascal_case_with(text, buf, true);
|
||||||
let mut at_word_start = false;
|
}
|
||||||
|
|
||||||
|
pub fn to_camel_or_pascal_case_with(
|
||||||
|
text: impl Iterator<Item = char>,
|
||||||
|
buf: &mut Tendril,
|
||||||
|
is_pascal: bool,
|
||||||
|
) {
|
||||||
|
let mut capitalize_next = is_pascal;
|
||||||
|
|
||||||
for c in text {
|
for c in text {
|
||||||
// we don't count _ as a word char here so case conversions work well
|
if c.is_alphanumeric() {
|
||||||
if !c.is_alphanumeric() {
|
if capitalize_next {
|
||||||
at_word_start = true;
|
buf.extend(c.to_uppercase());
|
||||||
continue;
|
capitalize_next = false;
|
||||||
}
|
} else {
|
||||||
if at_word_start {
|
buf.extend(c.to_lowercase());
|
||||||
at_word_start = false;
|
}
|
||||||
buf.extend(c.to_uppercase());
|
|
||||||
} else {
|
} else {
|
||||||
buf.push(c)
|
capitalize_next = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue