Skip to content

Commit

Permalink
fix: pascal case not parsing capitals full words correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
joshstevens19 committed Sep 17, 2024
1 parent 2add7dd commit 0c4cb9a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
23 changes: 19 additions & 4 deletions core/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,29 @@ pub fn camel_to_snake_advanced(s: &str, numbers_attach_to_last_word: bool) -> St
pub fn to_pascal_case(input: &str) -> String {
let mut result = String::new();
let mut capitalize_next = true;
let mut prev_is_lowercase = false;

for ch in input.chars() {
for (i, ch) in input.chars().enumerate() {
if ch == '_' {
capitalize_next = true;
prev_is_lowercase = false;
} else if capitalize_next {
result.push(ch.to_ascii_uppercase());
capitalize_next = false;
} else {
prev_is_lowercase = false;
} else if ch.is_ascii_uppercase() && prev_is_lowercase {
result.push(ch);
prev_is_lowercase = false;
} else if i > 0 &&
ch.is_ascii_uppercase() &&
!prev_is_lowercase &&
input.chars().nth(i + 1).map_or(false, |next| next.is_ascii_lowercase())
{
result.push(ch.to_ascii_lowercase());
prev_is_lowercase = true;
} else {
result.push(ch.to_ascii_lowercase());
prev_is_lowercase = ch.is_ascii_lowercase();
}
}

Expand Down Expand Up @@ -176,14 +190,15 @@ mod tests {

#[test]
fn test_with_acronyms() {
assert_eq!(to_pascal_case("ETH_USD_price"), "ETHUSDPrice");
assert_eq!(to_pascal_case("ETH_USD_price"), "EthUsdPrice");
assert_eq!(to_pascal_case("http_request_handler"), "HttpRequestHandler");
}

#[test]
fn test_single_word() {
assert_eq!(to_pascal_case("user"), "User");
assert_eq!(to_pascal_case("CONSTANT"), "CONSTANT");
assert_eq!(to_pascal_case("CONSTANT"), "Constant");
assert_eq!(to_pascal_case("URI"), "Uri");
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions documentation/docs/pages/docs/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
-------------------------------------------------
fix: throw error if contract names are not unique
fix: allow non camel case types in generated code
fix: pascal case not parsing capitals full words correctly

### Breaking changes
-------------------------------------------------
Expand Down

0 comments on commit 0c4cb9a

Please sign in to comment.