Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix snakeCase + DartFrogGenerator #1267

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1176,14 +1176,36 @@ private static String getTypeOfSchema(Schema schema) {
}

/**
* Return the snake-case of the string
* Return the snake_case of the string
*
* @param name string to be snake-cased
* @return snake-cased string
* @param name string to be snake_cased
* @return snake_cased string
*/
@SuppressWarnings("static-method")
public String snakeCase(String name) {
return (name.length() > 0) ? (Character.toLowerCase(name.charAt(0)) + name.substring(1)) : "";
public String snakeCase(String camelCase) {
if (camelCase.isEmpty()) {
return "";
}
StringBuilder snakeCase = new StringBuilder();
boolean previousCharWasUpperCase = false;

for (int i = 0; i < camelCase.length(); i++) {
char currentChar = camelCase.charAt(i);

if (Character.isUpperCase(currentChar)) {
if (i > 0 && !previousCharWasUpperCase) {
snakeCase.append('_');
} else if (previousCharWasUpperCase && i + 1 < camelCase.length() &&
Character.isLowerCase(camelCase.charAt(i + 1))) {
snakeCase.append('_');
}
snakeCase.append(Character.toLowerCase(currentChar));
previousCharWasUpperCase = true;
} else {
snakeCase.append(currentChar);
previousCharWasUpperCase = false;
}
}
return snakeCase.toString();
}

/**
Expand Down