Skip to content

Fix and improve CamelCaseFunctionNameSanitizer #603

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

Merged
merged 2 commits into from
Oct 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,37 @@ public String sanitizeFunctionName(String functionName) {
sanitized.append(words[0].toLowerCase());

for (int i = 1; i < words.length; i++) {
sanitized.append(capitalize(words[i].toLowerCase()));
sanitized.append(sanitizeWord(words[i]));
}

return sanitized.toString();
}

private String capitalize(String line) {
return Character.toUpperCase(line.charAt(0)) + line.substring(1);
return line.length() > 0 ? Character.toUpperCase(line.charAt(0)) + line.substring(1) : "";
}

private boolean isUpperCaseAcronym(String word) {
if (word == null || word.length() < 2) {
return false;
}

for (char c : word.toCharArray()) {
if (Character.isLowerCase(c)) {
return false;
}
}

return true;
}

private String sanitizeWord(String word) {
if (word == null) {
return "";
} else if (word.length() == 2 && isUpperCaseAcronym(word)) {
return word;
} else {
return capitalize(isUpperCaseAcronym(word) ? word.toLowerCase() : word);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,31 @@ public void testSanitizeFunctionName() {
String functionName = "I am a function name";
String expected = "iAmAFunctionName";
String actual = generator.sanitizeFunctionName(functionName);
assertEquals(expected, actual);

functionName = "Function name with multiple spaces";
expected = "functionNameWithMultipleSpaces";
actual = generator.sanitizeFunctionName(functionName);
assertEquals(expected, actual);

functionName = "Function name with pascalCase word";
expected = "functionNameWithPascalCaseWord";
actual = generator.sanitizeFunctionName(functionName);
assertEquals(expected, actual);

functionName = "Function name with CamelCase word";
expected = "functionNameWithCamelCaseWord";
actual = generator.sanitizeFunctionName(functionName);
assertEquals(expected, actual);

functionName = "Function name with multi char acronym HTTP Server";
expected = "functionNameWithMultiCharAcronymHttpServer";
actual = generator.sanitizeFunctionName(functionName);
assertEquals(expected, actual);

functionName = "Function name with two char acronym US";
expected = "functionNameWithTwoCharAcronymUS";
actual = generator.sanitizeFunctionName(functionName);
assertEquals(expected, actual);
}
}