Skip to content

Commit

Permalink
Better handling of snake-ification of mixed input (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoijui authored Nov 25, 2021
1 parent 873bfed commit 13a7bf8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,21 @@ pub trait SnakeCaseExt {
impl SnakeCaseExt for String {
fn to_snake_case(&self) -> String {
let mut s = String::new();
let mut was_sep = false;
if let Some(c) = self.chars().nth(0) {
was_sep = SEPARATORS.contains(c);
s.push(c.to_lowercase().next().unwrap());
}
for c in self.chars().skip(1).into_iter() {
if c.is_lowercase() {
s.push(c)
was_sep = false;
s.push(c);
} else {
s.push('_');
if SEPARATORS.contains(c) {
if !was_sep {
s.push('_');
}
was_sep = SEPARATORS.contains(c);
if was_sep {
continue;
} else {
s.push(c.to_lowercase().next().unwrap())
Expand Down
18 changes: 18 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ fn it_works_to_snakecase() {
assert!(snake!(a_bC1)());
}

#[test]
fn it_works_to_snakecase_mixed() {
fn hello_world() -> bool {
true
}
fn hello_world_and_welcome() -> bool {
true
}
assert!(snake!(hello_world)());
assert!(snake!(hello_World)());
assert!(snake!(helloWorld)());
assert!(snake!(HelloWorld)());
assert!(snake!(hello_world_and_welcome)());
assert!(snake!(hello_world_andWelcome)());
assert!(snake!(hello_worldAndWelcome)());
assert!(snake!(hello_world_AndWelcome)());
}

#[test]
fn it_works_to_pascalcase() {
#[allow(non_snake_case)]
Expand Down

0 comments on commit 13a7bf8

Please sign in to comment.